DocVariable Fields

S

SeanP

Hey I am pretty new to VBA and am trying to set up a macro that will update
all of the fields in a document that are DocVariables. Some are in the body
of the document and in the header/footer. I have one set up that will update
all fields, but that is doing more than it needs to as it does the TOC and
all the inclused text.

Also anyone know of a good reference for object naming conventions in Word
VBA, I program in other languages so the language structure is easy to pick
up, just shakey on accessing the objects I want to.

Thanks
 
S

SeanP

After searching (which I should have done a bit more of before posting) I
found code I could modify to do it, what would I do to specify a specific
variable?

Sub RefFieldUpdateAllStory()
Dim oField As Field
Dim oStory As Range
For Each oStory In ActiveDocument.StoryRanges
Do
For Each oField In oStory.Fields
If oField.Type = wdFieldRef Then
oField.Update
End If
Next oField
Set oStory = oStory.Next
Loop Until oStory Is Nothing
Next oStory
End Sub
 
S

SeanP

Whoops left the old code there

oField.Type = wdFieldDocVariable

was what it should have been.

I am noticing now though on some of my larger docs with more fields in it,
the DocVariable in my footer is not being updated. Yet it works in other
documents just fine ... anyone know why this would be?
 
G

Greg

Sean,

I only see this post and not your original question. If I understand
correctly you want code to update a specific DocVariable. Use
something like to evaluate the field type and the name of the variable
in the field code:

Sub UpdateSpecificDocVarialbe()

Dim oField As Field
Dim oStory As Range

ActiveDocument.Variables("One").Value = "Three"
ActiveDocument.Variables("Two").Value = "2"

For Each oStory In ActiveDocument.StoryRanges
Do
For Each oField In oStory.Fields
If oField.Type = wdFieldDocVariable And _
InStr(oField.Code.Text, "VariableName") > 0 Then
oField.Update
End If
Next oField
Set oStory = oStory.Next
Loop Until oStory Is Nothing
Next oStory
End Sub
 
G

Greg Maxey

Sean,

Leave out this part of course:
ActiveDocument.Variables("One").Value = "Three"
ActiveDocument.Variables("Two").Value = "2"
which I used for testing.

Change "VariableName" to the name of your variable
 
C

Charles Kenyon

Note that a DocVariable field in a footer will crash Word 97.

--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide

See also the MVP FAQ: http://www.mvps.org/word which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
S

SeanP

This answers my first question about updating a specific DocVariable but I am
still having the problem of this kind of code (specific or all DocVariables)
not updating the footer in my larger Docs, where as it works in the smaller
ones. Why would that be?
 
G

Greg Maxey

SeanP,

Could be you have a blank footer somewhere in the larger documents. Try
calling
MakeHFValid

Sub UpdateSpecificDocVarialbe()

Dim oField As Field
Dim oStory As Range
MakeHFValid
For Each oStory In ActiveDocument.StoryRanges
Do
For Each oField In oStory.Fields
If oField.Type = wdFieldDocVariable And _
InStr(oField.Code.Text, "VariableName") > 0 Then
oField.Update
End If
Next oField
Set oStory = oStory.Next
Loop Until oStory Is Nothing
Next oStory
End Sub

Public Sub MakeHFValid()
Dim lngJunk As Long
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
End Sub
 
S

SeanP

Hmm, still not working. Yeah I am figuring something is causeing oStory to
return nothing and ending the loop prematurely. A blank header can cause
that? Can any blank field cause that? If so is there a way to search for it?

Thanks a ton for the help so far, really helping.
 
G

Greg Maxey

Sean,

I feel like an idiot. I have led you astray with an error in my original
post about updating a REF field.

Change Set oStory = oStory.Next
to Set oStory = oStory.NextStoryRange

That, and the MakeHFValid call should hopefully clear up the issue.
 
S

SeanP

Yup that did it, thanks so much

Sorry about that, I entered the original code I modified for my application
instead of my own edited code. I see now though that they were a bit
different than I thought in execution.
 

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