Search code examples
excelvbams-worduserform

Excel Userform TextBox.Value Populate MS Word Bookmarks


Private Sub CommandButton3_Click()

 Dim wApp As Object
 Dim wDoc As Object

 Set wApp = CreateObject("Word.Application")
 wApp.Visible = True

 Set wDoc = wApp.Documents.Open(Filename:="C:\template1.dotm ", ReadOnly:=False)
    With wDoc.Selection
    .Bookmarks("bookmark1") = UserForm5.TextBox1.Value
    .Bookmarks("bookmark2") = UserForm5.TextBox2.Value
    End With

 WordDoc.Close
 WordApp.Quit
 Set WordDoc = Nothing
 Set WordApp = Nothing

End Sub

Hello All,

I was hoping that someone could help me on this one. As shown on the code above, I am trying to get the userform textbox value and populate it to MS Word using bookmarks.

The issue is that when I click command button 3 on the userform, the MS word opens but the bookmarks is still empty i.e. no value

I would appreciate the help as I have been trying to resolve this for hours. Many thanks.

Regards, Kevin


Solution

  • The bookmark commands need a Range and Text properties. Example

    .Bookmarks("bookmark1").Range.Text = UserForm5.TextBox1.Value