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

RE: DocVariable Fields by SeanP

SeanP
Mon May 09 12:42:09 CDT 2005

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

"SeanP" wrote:

> 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

RE: DocVariable Fields by SeanP

SeanP
Mon May 09 13:49:35 CDT 2005

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?

"SeanP" wrote:

> 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
>
> "SeanP" wrote:
>
> > 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

Re: DocVariable Fields by Greg

Greg
Mon May 09 13:50:51 CDT 2005

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


Re: DocVariable Fields by Greg

Greg
Mon May 09 15:11:15 CDT 2005

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


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Greg wrote:
> 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



Re: DocVariable Fields by Charles

Charles
Mon May 09 15:32:22 CDT 2005

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.

"SeanP" <SeanP@discussions.microsoft.com> wrote in message
news:97235624-B99B-4998-8D86-853F53DEF12A@microsoft.com...
> 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?
>
> "SeanP" wrote:
>
>> 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
>>
>> "SeanP" wrote:
>>
>> > 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



Re: DocVariable Fields by SeanP

SeanP
Mon May 09 15:51:12 CDT 2005

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?

Re: DocVariable Fields by Greg

Greg
Mon May 09 16:23:50 CDT 2005

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
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

SeanP wrote:
> 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?



Re: DocVariable Fields by SeanP

SeanP
Mon May 09 17:25:10 CDT 2005

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.

Re: DocVariable Fields by Greg

Greg
Mon May 09 18:19:52 CDT 2005

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.



--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

SeanP wrote:
> 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.



Re: DocVariable Fields by SeanP

SeanP
Tue May 10 13:40:04 CDT 2005

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.

Re: DocVariable Fields by Greg

Greg
Tue May 10 14:27:41 CDT 2005

Sean,

Great. Nice working with you.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

SeanP wrote:
> 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.