R
ryguy7272
I am trying to populate DocVariables in a Word document. Initially I tried
to do this with bookmarks, and with some help from the people in the Excel
DG, I came up with this routine, which works great for bookmarks:
Sub PushToWord()
Dim objWord As New Word.Application
Dim doc As Word.Document
Dim bkmk As Word.Bookmark
sWdFileName = Application.GetOpenFilename(, , , , False)
Set doc = objWord.Documents.Open(sWdFileName)
For Each bmk In doc.Bookmarks
If bmk.Name = "BrokerFirstName" Then bmk.Range.Text = Range("B1").Value
If bmk.Name = "BrokerLastName" Then bmk.Range.Text = Range("B2").Value
Next
objWord.Visible = True
End Sub
This works fine, but it appears that the bookmarks vanish after being filled
in with the Excel data. I’ve worked with Document Variables a bit in the
past, and had great success with those. I’m thinking now, maybe I should use
Document Variables again for the current project that I am working on. I am
not quite sure how to modify the code above to handle Document Variables.
Can anyone offer a suggestion as to how to do this?
This code below works excellent for loading Document Variables into a ListBox:
Private Sub CommandButton1_Click()
ListBox1.BoundColumn = 1
ActiveDocument.Variables("Tracking").Value = ListBox1.Value
ListBox1.BoundColumn = 2
ActiveDocument.Variables("Broker_First_Name").Value = ListBox1.Value
ListBox1.BoundColumn = 3
ActiveDocument.Variables("Broker_Last_Name").Value = ListBox1.Value
‘.etc
ActiveDocument.Fields.Update
UserForm1.Hide
End Sub
Basically, I just want to use the ‘PushToWord’ macro to get my variables
into the Document Variables in my Word template.
Regards,
Ryan---
to do this with bookmarks, and with some help from the people in the Excel
DG, I came up with this routine, which works great for bookmarks:
Sub PushToWord()
Dim objWord As New Word.Application
Dim doc As Word.Document
Dim bkmk As Word.Bookmark
sWdFileName = Application.GetOpenFilename(, , , , False)
Set doc = objWord.Documents.Open(sWdFileName)
For Each bmk In doc.Bookmarks
If bmk.Name = "BrokerFirstName" Then bmk.Range.Text = Range("B1").Value
If bmk.Name = "BrokerLastName" Then bmk.Range.Text = Range("B2").Value
Next
objWord.Visible = True
End Sub
This works fine, but it appears that the bookmarks vanish after being filled
in with the Excel data. I’ve worked with Document Variables a bit in the
past, and had great success with those. I’m thinking now, maybe I should use
Document Variables again for the current project that I am working on. I am
not quite sure how to modify the code above to handle Document Variables.
Can anyone offer a suggestion as to how to do this?
This code below works excellent for loading Document Variables into a ListBox:
Private Sub CommandButton1_Click()
ListBox1.BoundColumn = 1
ActiveDocument.Variables("Tracking").Value = ListBox1.Value
ListBox1.BoundColumn = 2
ActiveDocument.Variables("Broker_First_Name").Value = ListBox1.Value
ListBox1.BoundColumn = 3
ActiveDocument.Variables("Broker_Last_Name").Value = ListBox1.Value
‘.etc
ActiveDocument.Fields.Update
UserForm1.Hide
End Sub
Basically, I just want to use the ‘PushToWord’ macro to get my variables
into the Document Variables in my Word template.
Regards,
Ryan---