DocVariables in Word

R

ryguy7272

I am running the following code in Excel:
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)

objWord.ActiveDocument.Variables("AllRuby").Value = Range("AllRuby").Value
objWord.ActiveDocument.Variables("AllianceRuby").Value =
Range("AllianceRuby").Value
objWord.ActiveDocument.Variables("EastRuby").Value = Range("EastRuby").Value
objWord.ActiveDocument.Variables("InsideSalesRuby").Value =
Range("InsideSalesRuby").Value
objWord.ActiveDocument.Variables("UnassignedRuby").Value =
Range("UnassignedRuby").Value
objWord.ActiveDocument.Variables("WestRuby").Value = Range("WestRuby").Value
objWord.ActiveDocument.Variables("TotalRuby").Value = Range("TotalRuby").Value

ActiveDocument.Fields.Update

objWord.Visible = True

End Sub


This is supposed to update DocVariables that I have already assigned in
Word. What it actually does is open a Word template, which I peppered with a
few DocVariables, but the template opens as 'Read Only'. When I try to
update the DocVariables, I receive a message that says 'Open a Read Only
Copy' and then 'Run-Time Error 4248: This Command is not available because no
document is open.' Any ideas as to what causes this? I've done this before
and never received this kind of error.

Thanks so much,
Ryan---
 
D

Doug Robbins - Word MVP

Assuming that sWdFileName contains the path and filename of the template
that you want to use,


Set doc = objWord.Documents.Add(sWdFileName
With doc
.Variables("AllRuby").Value = Range("AllRuby").Value
'etc
.Range.Fields.Update
End With

Then you are sure that you are working with a document created from the
template and not whatever Word might consider to be the active document at
the time.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

ryguy7272

Thanks for the suggestion Doug!! I am now trying to run the code below, but
I get a Run-Time Error ‘5825’ Object has been Deleted.

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.Add(sWdFileName)

With doc.Variables("AllRuby").Value = Range("AllRuby").Value
..Range.Fields.Update
End With

With doc.Variables("AllianceRuby").Value = Range("AllianceRuby").Value
..Range.Fields.Update
End With

With doc.Variables("EastRuby").Value = Range("EastRuby").Value
..Range.Fields.Update
End With

With doc.Variables("InsideSalesRuby").Value = Range("InsideSalesRuby").Value
..Range.Fields.Update
End With

With doc.Variables("UnassignedRuby").Value = Range("UnassignedRuby").Value
..Range.Fields.Update
End With

With doc.Variables("WestRuby").Value = Range("WestRuby").Value
..Range.Fields.Update
End With

With doc.Variables("TotalRuby").Value = Range("TotalRuby").Value
..Range.Fields.Update
End With

objWord.Visible = True

End Sub


I’m not sure what happed to my code. I’ve used this method of updating
DocVariables before, without incident. Now I get a ‘Read Only’ message or I
get a Run-Time Error. If someone can see the error and point out my mistake,
I would be most grateful.

Regards,
Ryan---
 
D

Doug Robbins - Word MVP

I do not think that .GetOpenFilename(, , , , False) is part of the Word
Object Model.

If you put a

MsgBox sWdFileName

what is displayed.

But another problem with your code is that a document Variable does not have
a Range so you are trying to update fields in something that does not exist.

Instead of

With doc.Variables("AllRuby").Value = Range("AllRuby").Value
.Range.Fields.Update
End With

With doc.Variables("AllianceRuby").Value = Range("AllianceRuby").Value
.Range.Fields.Update
End With

With doc.Variables("EastRuby").Value = Range("EastRuby").Value
.Range.Fields.Update
End With

With doc.Variables("InsideSalesRuby").Value =
Range("InsideSalesRuby").Value
.Range.Fields.Update
End With

With doc.Variables("UnassignedRuby").Value = Range("UnassignedRuby").Value
.Range.Fields.Update
End With

With doc.Variables("WestRuby").Value = Range("WestRuby").Value
.Range.Fields.Update
End With

With doc.Variables("TotalRuby").Value = Range("TotalRuby").Value
.Range.Fields.Update
End With

You should have

With doc
.Variables("AllRuby").Value = Range("AllRuby").Value
.Variables("AllianceRuby").Value = Range("AllianceRuby").Value
.Variables("EastRuby").Value = Range("EastRuby").Value
.Variables("InsideSalesRuby").Value = Range("InsideSalesRuby").Value
.Variables("UnassignedRuby").Value = Range("UnassignedRuby").Value
.Variables("WestRuby").Value = Range("WestRuby").Value
.Variables("TotalRuby").Value = Range("TotalRuby").Value
.Range.Fields.Update
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

ryguy7272

That's exactly it!! thank you so much Doug!!!
Hope you have a great day today and a better day tomorrow!!


Regards,
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