Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB6 - pass a string variable to a existing Word document 1

Status
Not open for further replies.

Jimreaper

MIS
May 22, 2001
21
US
How can I make a reference to the Word Objects Library using VB6 and pass a string variable to a existing Word document? How do I instantiate a new document from an existing one.
Thanks
Jimreaper - lost
 
I have used this to print complicated documents for New Accounts Opened at a bank e.g. Signature Cards, CD Receipts, Letters of Agreement. I use VB only to collect all the info. I pass unloaded dictionaries but you can use variables or an XML object or file. Understand this, get this to work and you'll be on your way. The long way but the VBA code does all the work.
Include Word Object Library in VB for references to constants only. If you code specific AS Word.??? it may fail on future versions.
Code:
'Use GetObject
'GetObject([pathname] [, class])
#Const blnWordDebug = True
#if blnWordDebug then
    Dim objWord As Word.Application
    Dim objDoc  As Word.Document
#Else   ' Release Version
    Dim objWord As object ' Avoid specific WORD version
    Dim objDoc  As object
#End if
Set objWord = GetObject(,"Word.Application")
Set objDoc = objWord.Documents.Open(strDocumentFile,ReadOnly:=True)
 
or
Set objDoc = GetObject(strDocummentFile)
Set objWord = objDoc.Application

'Call a Procedure in VBA code under ThisDocument in Word
Dim strVars(1) as string
strVar0) = "My Name"
strVar(1) = "My Addr"
'etc.
objDoc.PrintDoc strVars()
objDoc.Close false ' Do not save changes
objWord.Quit false ' do not save changes
Set objDoc = Nothing
Set objWord = Nothing

'=======================
' Set up Document with DocVariables in proper location
' Use Insert / Filed /Document Automation / Documment Variable - fill in e.g. DOCVARIABLE "Name"
' Use ALT+F9 to toggle fields display on and off or
' right-click on a filed and toggle invidulally. 
' Use MSDN.MICROSOFT.COM documentation.
'Put this in a module in Word under ThisDocument Alt+F11 to get to VBA editor
Public Sub PrintDOC(strVars() as string)
   SetDocVariable "Name", strVars(0)
   SetDocVariable "Address", strVars(1)
   'etc.
    UpdateFieldsAndPrintDocument
End Sub
Public Sub SetDocVariable(ByVal strVariableName As String, _
                            ByVal strValue As String)
    Dim strDescription As String, _
        strWValue As String
        
    
    If Len(strValue) = 0 Then
        strWValue = Space(1)  ' Can not be zero length string
    Else
        strWValue = strValue
    End If
    '*****
    '* Place the value into the indicated Variable.
    '* Variables is a "Collection".
    '*****
       
    On Error Resume Next
        ActiveDocument.Variables(strVariableName) = strWValue
        strDescription = Err.Description
    On Error GoTo 0
    
    If Len(strDescription) > 0 Then
        strDescription = strDescription & vbCrLf & _
            "DocVariable=" & strVariableName & " not defined."
        MsgBox strDescription
        ActiveDocument.Variables.Add strVariableName, _
                                    strWValue
    End If
End Sub

Public Sub UpdateFieldsAndPrintDocument(Optional strCopies = "3")
    '*****
    '* Update fields with DOCVARIABLES
    '*****
    Dim lngIndex As Long, _
        strDescription As String
    With ActiveDocument.Fields
        lngIndex = .Update
        If lngIndex > 0 Then
            strDescription = .Item(lngIndex).Code.Text & vbCrLf & _
                "Fields Update error"
            MsgBox strDescription, vbInformation, _
                ActiveDocument.Name
        End If
    End With
    '*****
    '* Print Document
    '*****
    Application.PrintOut FileName:="", _
                        Range:=wdPrintAllDocument, Item:= _
                        wdPrintDocumentContent, _
                        Copies:=strCopies, _
                        Pages:="", _
                        PageType:=wdPrintAllPages, _
                        Collate:=True, _
                        Background:=False, _
                        PrintToFile:=False

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top