Populate DocVariable located in textbox in msword

Jwwplastic

New member
Joined
Nov 14, 2008
Messages
3
Programming Experience
Beginner
Hi, I have a template in Microsoft Word. I can pass variables from vb.net and fill the docVariables in word if they located in the main body, header and footer. I cannot fill the docvariable if it is located in a textbox. How do i access the textbox and populate the variables in it. Below is the code i have that sends the variables to the documents, and again it works, but not if the fields are in a textbox.

VB.NET:
  With oWordDoc 
            .Variables("Expedite").Value = Expedite 
            .Variables("CustName").Value = custname 
            .Variables("Address").Value = address 
            .Variables("CitystZip").Value = citystzip 
            .Variables("TrackingNum").Value = trackingnum & " "

any help would be appreiciated.

thanks.
 
Works.. though takes a while

Below is the code that makes this work. It has to look inside the textboxes and populate the variables.

it may not be the best way, and it is slow, but it works. I thought i better do my part and post what works incase someone is having the same trouble.

justin


VB.NET:
Dim oWordApp As New word.Application
       
        Dim fileName As Object = "c:\Shop order.dot"  'opens word template
        Dim [readOnly] As Object = True
        Dim isVisible As Object = True
        Dim oWordDoc As word.Document = oWordApp.Documents.Open(fileName, , [readOnly], , , , , , , , , isVisible, , , )
        On Error GoTo ErrHandler

        Dim rngStory As word.Range
        Dim oShp As word.Shape
        For Each rngStory In oWordDoc.StoryRanges
            Do
                On Error Resume Next
                rngStory.Fields.Update()
                Select Case rngStory.StoryType
                    Case 1, 5, 7, 9
                        If rngStory.ShapeRange.Count > 0 Then
                            For Each oShp In rngStory.ShapeRange
                                If oShp.TextFrame.HasText Then

                                    With oWordDoc ' fills reports with data
                                        .Variables("Expedite").Value = Expedite & " "
                                        .Variables("CustName").Value = custname & " "
                                        .Variables("Address").Value = address & " "
                                  
                                        .Range.Fields.Update()
                                    End With
                                End If


                            Next oShp
                        End If
                    Case Else
                        'Do Nothing
                End Select
                On Error GoTo 0
                'Get next linked story (if any)
                rngStory = rngStory.NextStoryRange
            Loop Until rngStory Is Nothing
        Next rngStory

        oWordDoc.PrintOut()  'prints order

        Dim savechanges As Object = False

        oWordDoc.Close(savechanges)
        oWordApp.Quit()
        oWordDoc = Nothing
        oWordApp = Nothing
     
           Exit Sub
ErrHandler:
        MsgBox("Unhanled Error: " & Err.Description)
        oWordDoc.Close(savechanges)
        oWordApp.Quit()
        oWordDoc = Nothing
        oWordApp = Nothing

    End Sub
 
Better Fix

Ok, I feel like i'm talking to myself. If thats the case, good job jwwplastic.

Bookmarks are working much better AND Faster:

VB.NET:
    With oWordDoc

            .Bookmarks.Item("Expedite").Range.Text = Expedite & " "
            .Bookmarks.Item("CustName").Range.Text = custname & " "
            .Bookmarks.Item("Address").Range.Text = address & " "
           

            .Range.Fields.Update()

        End With


seem to work a heck of a lot better. AND you do not need to run throuh all the textboxes. it finds all bookmarks in the entire document without having to look in each section/object.

thanks for the help Jwwplastic. Good work. Just hope someone else can find this useful if they have the problem.

ha.
 
Back
Top