Populate DocVariables in a Word Document

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---
 
R

ryguy7272

Thanks for the info. Helmut. I actually saw this link yesterday. I didn’t
find it particularly helpful, because I didn’t know how to apply it to my
current situation. Can you do one of two things for me:

1) Show me how to apply that sample code to my current situation
2) Let me know how to use DocVariables instead of bookmarks in my Word
template

I'll fiddle around with it a little more and try to get it to work but I'm
not quite sure what to do at this point.

Regards,
Ryan---
 
H

Helmut Weber

Hi Ryan,

have a look at this one:

Sub Makro2()
ActiveWindow.View.ShowFieldCodes = False
ActiveDocument.Variables("Tracking").Value = "Track"
Selection.Fields.Add _
Range:=Selection.Range, _
Type:=wdFieldEmpty, _
Text:="DOCVARIABLE Tracking ", _
PreserveFormatting:=True
ActiveDocument.Variables("Tracking").Value = "Another Track"
ActiveDocument.Fields.Update
End Sub
 
R

ryguy7272

I got it to work with this logic:
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)

ActiveDocument.Variables("BrokerFirstName").Value = Range("B1").Value
ActiveDocument.Variables("BrokerLastName").Value = Range("B2").Value

ActiveDocument.Fields.Update

objWord.Visible = True

End Sub

That was significantly easier than I thought. Working with Word is usually
work; this time it was actually fun.

Thanks so much Helmut!
Ryan--
 
R

ryguy7272

Well, I guess I spoke too soon. It worked in a test file that I set up, but
now it will not work in the actual Excel workbook that I normally use. Here
is the code:
Sub ControlWordFromXL()

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)
'On Error Resume Next

Sheets("LOOKUP").Activate
ActiveDocument.Variables("Broker_First_Name").Value =
Range("Broker_First_Name").Value
ActiveDocument.Variables("Broker_Last_Name").Value =
Range("Broker_Last_Name").Value
ActiveDocument.Fields.Update

'On Error Resume Next
objWord.Visible = True

End Sub

The issue is this...
Run-time error '4248':
This command is not available because no document is open.

All the relevant data is stored on a sheet called 'Lookup'. This error
message is bizarre because the Word document is open...as far as I can tell.
Does anyone know what could cause this?

Regards,
Ryan--
 
H

Helmut Weber

Hi Ryan,

I wonder whether Excel will know "activedocument".

Try
doc.Variables("Broker_First_Name").Value = ...
or
objWord.activedocument.variables("Broker_First_Name")...
instead.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
F

fumei via OfficeKB.com

I think you were gently suggesting, but there is no wonder about it. You
know very well Excel will not. The object has to be fully qualified.
doc.Variables("Broker_First_Name").Value = ...
or
objWord.activedocument.variables("Broker_First_Name")...

is required.
 
H

Helmut Weber

Hi Fumei,
I think you were gently suggesting, but there is no wonder about it. You
know very well Excel will not. The object has to be fully qualified.

indeed, but I was taught british english long time ago...



--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
R

ryguy7272

You guys are funny. Thanks for making me laugh on a dreary day in New York
City, and thanks for pointing out the errors of my ways. I thought
ActiveDocument was appropriate. I implemented the second suggestion and now
it works!! I learned a few new things today!!
Thanks guys!!!
Ryan---
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top