How to Access Word for Windows Document Variables from VB

Last reviewed: March 12, 1997
Article ID: Q117830
2.00 3.00 WINDOWS kbinterop kbole kbprg kbcode kbmacro

The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0
- Microsoft Word for Windows, versions 6.0 and 6.0a

SUMMARY

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:

  • Store the information in a text file.
  • Store the information in a Word AutoText entry.
  • Store the information in document variables.

This article demonstrates the document variable approach.

For more information about retrieving dialog box values in Word, query on the following words in the Microsoft Knowledge Base:

   ole and automation and retrieving and settings and dialog

MORE INFORMATION

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.

Visual Basic Sub Procedure

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Add the following code in the Form_Click event of the form:

    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
    
    

WordBasic Macro

Create the following WordBasic macro in Word and name it PassSummaryInfo. Store the macro in the global template (NORMAL.DOT).

  1. Start Word for Windows.

  2. Create the macro by choosing Macro from the Tools menu.

  3. Type in the name as PassSummaryInfo, and choose Create.

  4. Add the following code to the macro. When you are done, choose Close from the File menu and respond Yes to the "save" dialog box.

    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

REFERENCES

"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 reference words: 2.00 3.00 kbmacro wdk 6.00 6.00a
KBCategory: kbinterop kbole kbprg kbcode kbmacro
KBSubcategory: IAPOLE
Keywords : IAPOLE kbinterop kbmacro kbprg
Technology : kbole
Version : 2.0 3.0
Platform : WINDOWS
Resolution Type : kbcode


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 12, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.