HOWTO: Access Word for Windows Document Variables from VB |
Q117830
This article describes and demonstrates how to pass information from Word
for Windows to Visual Basic using Word document variables. The example
included in this article uses a WordBasic macro and a Visual Basic Sub
procedure.
You can pass information from Word to Visual Basic using any of the
following methods:
ole and automation and retrieving and settings and dialog
Using WordBasic (the Word for Windows macro language), you can retrieve
values stored in Word dialog boxes and use them in Visual Basic. The
example in this article uses OLE automation to launch a WordBasic macro
that retrieves all the values from the File Summary Info dialog box in Word
and displays them in a Visual Basic message box. (The File Summary Info
dialog box corresponds to the FileSummaryInfo statement in WordBasic.)
The WordBasic macro, named PassSummaryInfo, creates a ParamList document
variable, which is a comma-delimited string of all the document variables
to be extracted. Then, using OLE automation, Visual Basic extracts and
displays the document variables.
The advantage of using this method is that you can save the document
variables along with your document, where you can retrieve them at any
future time.
Sub File_Click ()
'Dimension variables:
Dim sReturn As String, Msg As String
Dim ParamArray() As String, ParamElement As String
Dim count As Integer, j As Integer
Dim wb As Object
'Run Word Macro Block:
Macro$ = "PassSummaryInfo"
On Error Resume Next
Set wb = CreateObject("Word.basic")
wb.FileNew
wb.ToolsMacro Macro$, True
If Err Then
MsgBox "Err #" & Str$(Err) & ", " & Error
End If
'Extract Document variables:
sReturn = wb.[getdocumentvar$]("ParamList")
If Err Then
MsgBox "Err #" & Str$(Err) & ", " & Error
Exit Sub
End If
'Parse Document variable list from ParamList:
Do While InStr(sReturn, ",")
j = InStr(sReturn, ",")
ParamElement = Left(sReturn, j - 1)
sReturn = Mid$(sReturn, j + 1)
ReDim Preserve ParamArray(count)
ParamArray(count) = ParamElement
count = count + 1
Loop
'Retrieve Last Document variable name:
ReDim Preserve ParamArray(count)
ParamArray(count) = sReturn
'Extract values from document variables:
For j = 0 To count
Msg = Msg & ParamArray(j) & " = "
Msg = Msg & wb.[getdocumentvar$]((ParamArray(j))) & Chr$(10)
Next
'wb.FileClose (2)
If Err Then
MsgBox "Err #" & Str$(Err) & ", " & Error
End If
'Display values in message box:
MsgBox Msg
End Sub
Sub MAIN
Dim dlg As FileSummaryInfo
GetCurValues dlg
ParamList$ = "Title,Subject,Author,KeyWords,Comments,FileName,Directory"
ParamList$ = ParamList$ + ",Template,CreateDate,LastSavedDate"
ParamList$ = ParamList$ + ",LastSavedBy,RevisionNumber,EditTime"
ParamList$ = ParamList$ + ",LastPrintedDate,NumPages,NumWords,NumChars"
ParamList$ = ParamList$ + ",NumParas,NumLines,FileSize"
SetDocumentVar "ParamList", ParamList$
SetDocumentVar "Title", dlg.Title
SetDocumentVar "Subject", dlg.Subject
SetDocumentVar "Author", dlg.Author
SetDocumentVar "Keywords", dlg.Keywords
SetDocumentVar "Comments", dlg.Comments
SetDocumentVar "FileName", dlg.FileName
SetDocumentVar "Directory", dlg.Directory
SetDocumentVar "Template", dlg.Template
SetDocumentVar "CreateDate", dlg.CreateDate
SetDocumentVar "LastSavedDate", dlg.LastSavedDate
SetDocumentVar "LastSavedBy", dlg.LastSavedBy
SetDocumentVar "RevisionNumber", dlg.RevisionNumber
SetDocumentVar "EditTime", dlg.EditTime
SetDocumentVar "LastPrintedDate", dlg.LastPrintedDate
SetDocumentVar "NumPages", dlg.NumPages
SetDocumentVar "NumWords", dlg.NumWords
SetDocumentVar "NumChars", dlg.NumChars
SetDocumentVar "NumParas", dlg.NumParas
SetDocumentVar "NumLines", dlg.NumLines
SetDocumentVar "FileSize", dlg.FileSize
End Sub
"Microsoft Word User's Guide," version 6.0, pages 216-217, 220-223, 749,
755
"Microsoft Word Developer's Kit," version 6.0, Microsoft Press, 1994,
pages 174-182, 446-447, 492, 679-680
Additional query words:
Keywords : kbinterop kbmacro kbAutomation kbVBp300 kbWord kbvbp200
Issue type : kbhowto
Technology :
|
Last Reviewed: March 7, 2000 © 2001 Microsoft Corporation. All rights reserved. Terms of Use. |