Auto fill word document using word vba

Auto fill word document using word vba

The following procedure shows, how to auto fill  word document using word vba. Book marks plays a major role in updating the fields. The text will be erased and a new text is placed in the same location.

Public Sub FillBM(strBMName As String, strValue As String)
'http://gmayor.com
Dim oRng As Range
    With ActiveDocument
        On Error GoTo lbl_Exit
        Set oRng = .Bookmarks(strBMName).Range
        oRng.Text = strValue
        oRng.Bookmarks.Add strBMName
    End With
lbl_Exit:
    Set oRng = Nothing
    Exit Sub
End Sub

Private Sub cmdOK_Click()
   'Hide the userform
   Hide
   'e.g. Assign the values of the three text boxes to the three bookmarks
   'Using the Function FillBM
   FillBM "bm1", TextBox1.Text
   FillBM "bm2", TextBox2.Text
   FillBM "bm3", TextBox3.Text
   'Unload the form
   Unload Me
End Sub

About the author: admin

You must be logged in to post a comment.