I open a Word template from Access using VBA. The Word template has form fields. One of the form fields called "Background" is only supposed to be displayed when the text box in the form in Access has something in it. If not, the form field and label should not be displayed in the Word doc. I am Using MS Access and Word 2010. I tried collapsing a bookmark but it did not work so instead, I create the Label called Background and the blank lines in VBA code in Access:
.Bookmarks("txtbackground").Range.Fields(1).Result .Text = vbCr + "Background" + vbCr + vbCr + CleanString(Me.Text466) + vbCr
I found this code online to bold and underline the word Background.
Sub BoldUnderline()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "<Background>"
.Replacement.Text = "^&"
.MatchWildcards = True
.Format = True
.Replacement.Font.Underline = wdUnderlineSingle
.Execute Replace:=wdReplaceAll
End With
End Sub
It worked the first time I used it. Every attempt since has failed. Then I changed it from a "Sub" to a "Function". First attempt it worked. Every subsequent attempt failed. I call the function from another function:
Call BoldUnderline()
The brackets disappear after I type them in. I've tried adding a string to pass but it doesn't work. Here is the function where I create the Word doc and where I call the BoldUnderline function.
Function Fillwordform()
Dim appword As Word.Application
Dim doc As Word.Document
Dim str1, str2 As String
On Error Resume Next
Error.Clear
Path = "C:\Users\....Template6.docm"
Set appword = GetObject(, "word.application")
If Err.Number <> 0 Then
Set appword = New Word.Application
appword.Visible = True
End If
Set doc = appword.Documents.Open(Path, , False)
With doc
.FormFields("txttopdate").Result = Me.Text423
.Bookmarks("txtdescription").Range.Fields(1).Resul t.Text = CleanString(Me.Description)
.
.
.
.Bookmarks("txtbackground").Range.Fields(1).Result .Text = vbCr + "Background" + vbCr + vbCr + CleanString(Me.Text466) + vbCr
Dim check As String
Call BoldUnderline(check)
.Visible = True
.Activate
End With
Set doc = Nothing
Set appword = Nothing
End Function
How do I get the function to run every time? Any help would be greatly appreciated. thanks