I have got the code working with 4 bookmarks. The 4th bookmark (bmRe) is working except that I want to add the contents of a userform textbox (txtProject). This has been commented out because the code won’t run if left in. The error says that that the variable is not defined. If I add txtProject to the Call code brackets then I get a compile error. Any help would be appreciated. (I am unsure if I should put this on a new thread?).
Code:
Sub UpdateBookmark(bmContact As String, bmCompany As String, bmAddress As String, bmRe As String, StrTxtContact As String, StrTxtCompany As String, StrTxtAddress As String)
Dim BkMkContact As Range
Dim BkMkCompany As Range
Dim BkMkAddress As Range
Dim BkMkRe As Range
With ActiveDocument
'Contact
If .Bookmarks.Exists(bmContact) Then
Set BkMkContact = .Bookmarks(bmContact).Range
BkMkContact.Text = StrTxtContact
.Bookmarks.Add bmContact, BkMkContact
End If
'Company
If .Bookmarks.Exists(bmCompany) Then
Set BkMkCompany = .Bookmarks(bmCompany).Range
BkMkCompany.Text = StrTxtCompany
.Bookmarks.Add bmCompany, BkMkCompany
End If
'Address
If .Bookmarks.Exists(bmAddress) Then
Set BkMkAddress = .Bookmarks(bmAddress).Range
BkMkAddress.Text = StrTxtAddress
.Bookmarks.Add bmAddress, BkMkAddress
End If
'Re
If .Bookmarks.Exists(bmRe) Then
Set BkMkRe = .Bookmarks(bmRe).Range
BkMkRe.Text = "Re: " '& txtProject
.Bookmarks.Add bmRe, BkMkRe
End If
.Fields.Update
End With
Set BkMkContact = Nothing
Set BkMkCompany = Nothing
Set BkMkAddress = Nothing
Set BkMkRe = Nothing
End Sub
Called with:
Code:
Private Sub CommandButton1_Click()
Call UpdateBookmark("bmContact", "bmCompany", "bmAddress", "bmRe", txtContact, txtCompany, txtAddress)
End Sub