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!

VB variable sent to a Word Doc.

Status
Not open for further replies.

Jimreaper

MIS
May 22, 2001
21
US

Does anyone know how to pass a VB string variable into a Word Doc and print and close the Doc file?

Ex.
Dim strFirstName As String
strFirstName = "Jim"



**********Word Doc Ex.***
Dear {strFirstName},
Blah blah blah blah

Thanks
Blah
**********After strFirstName is passed, print then close***

If anyone can lead me in the right direction I would appreciate it.

Thanks
Newbie Jim


 
Hey Jim. You would need to do something like:

Dim AppWord As Word.Application

Set AppWord = New Word.Application

If AppWord.Documents.Count = 0 Then
AppWord.Documents.Add 'Creates a new document
End If

AppWord.Documents(1).Insert "Dear " & strFirstName

To print you would use the printout command something like:

Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, Collate:=True, Background:=True, PrintToFile:=False

This would write Dear Jim on your word document and print it.

If you want you can set word to be invisible using:
Appword.Visible=False

To close word just add:

Appword.quit
Set AppWord = Nothing

Hope this helps, if you need any more help let me know.
 
This is great stuff, Thanks. What I failed to mention is the Word Doc is and existing Document that needs to have fields throughout the paragraphs in the document.
Any suggestions on how to do that. Also, How to "mark" where the variable should go in the Word Doc?

****************Word Doc Ex.*********
Dear {variable1},

Blah blah {variable2} blah blah .
Thanks,
{variable3}
*************************************

Thanks again.
Jimreaper - newbie
 
To open an existing document just do:

AppWord.Documents.Open FileName:="Your_Document_Here"

Ok for what I understood you have the above template and want to replace the {variableX} by some string right?

If so, that's easy you can use for example something like:

AppWord.Selection.WholeStory

While a = True

With Selection.Find
.Text = "{variableX}" 'Looks for this string on the whole doc (Replace X by the respective number)
.Forward = True
.MatchWholeWord = False
.MatchWildcards = False
End With

a = Selection.Find.Execute 'If doesn't find on text a=0

If a = True Then
AppWord.Documents(1).Insert strFirstName 'Inserts your variable replacing {variableX}
End if

Wend

Another choice is to use the Instr method, but I think that for what you need this one is easier.
This should do the trick.
 
You need to make a reference to the Word Objects Library and pass the variable to the document. You will need instantiate a new document and take it from there.
 
hi,
But I don't find a Insert method in the Documents collection. Can you suggest an alternate ?


Thank you...
RR

 
I do not use a template. I use DocVariables on an "ordinaray" Word Doc. No "Find/Replace" necessary.
To insert a DocVariable, put the cursor in the Word Doc where the data should go.
Now use Insert / Field / Document Automation / DocVariable and fill in the name which you make up, "Customername" in this example. Use alt+F9 to display and hide all DocVariables. When hidden, an individual DocVaraible can be displayed by putting the cursor where the DocVariable field is, right-clicking and selecting Toggle Field.
Code:
[code]
' Module/Form level variable
Private objWord as Word.Application


Set objWord = CreateObject or Getobject
' Get your document as active document
SetDocVariable "Customername", strValue
UpdateFieldsAndPrintDocument 
objWord.ActiveFocument.Close false
objWord.Quit false
Set objWord = Nothing
End Sub
'========================================================
Public Sub UpdateFieldsAndPrintDocument(Optional strCopies = "1")
    '*****
    '* Update fields with DOCVARIABLES
    '*****
    Dim lngIndex As Long, _
        strDescription As String
    With objWord.ActiveDocument.Fields
        lngIndex = .Update
        If lngIndex > 0 Then
            strDescription = .Item(lngIndex).Code.Text & vbCrLf & _
                "Fields Update error"
            MsgBox strDescription, vbInformation, _
                objWord.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

Public Sub SetDocVariableFmt(ByVal strVariableName As String, _
                            ByVal strValue As Variant, _
                            ByVal strFormat As String)
    '*****
    '* format the value before doing Replace unless
    '* there is no value.
    '*****
    Dim strNew
    If Len(Trim(strValue)) = 0 Then ' No value
        strNew = " "
    Else
        strNew = Format(strValue, strFormat)
    End If
    SetDocVariable strVariableName, strNew
    
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
        objWord.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
        objWord.ActiveDocument.Variables.Add strVariableName, _
                                    strWValue
    End If
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top