Answered by:
Changing variable in header using VBA

Question
-
Hi,
I am having difficulty in getting the variable in the footer updated using VBA.
The following codes I quoted from Doug from my previous post plus the codes I added to it.
For i = 1 To numchapters ' Get the component name and put it into the document If fLine = True Then If Left(srange.Paragraphs(1).Range, 2) = "**" Then ' for example **Capacitor myComponentName = Mid(srange.Paragraphs(1).Range, 3, Len(srange.Paragraphs(1).Range)) ' this gives me Capacitor target.Variables("nl1").Value = myComponentName ' docvariable nl1 is in the body of the report document target.Variables("nl1-2").Value = myComponentName ' docvariable nl1-2 is in the header of the report target.Fields.Update 'target.PrintPreview fLine = False End If End If srange.Start = srange.Start + Len(srange.Paragraphs(1).Range) srange.End = srange.Start + InStr(srange, "+") - 1 target.Variables("Chapter" & i).Value = srange srange.Collapse Direction:=wdCollapseEnd srange.End = source.Range.End Next i
What happens after the code is run - it changes the variable nl1 located in the body of the report. The variable nl1-2 located in the header, on the other hand, does not change at all.
How do i do it so that the value of the variable in the header gets updated as well as the variables in the body of the document.
Thank you in advance
Friday, October 8, 2010 1:26 PM
Answers
-
The following will update the fields in the body of the document and then iterate through the all of the Headers and Footers in all of the Sections of the document and update the fields in the Range of each:
Dim i As Long, j As Long
With Target
.Range.Fields.Updatge
For i = 1 To .Sections.Count
With .Sections(i)
For j = 1 To .Headers.Count
.Headers(j).Range.Fields.Update
Next j
For j = 1 To .Footers.Count
.Footers(j).Range.Fields.Update
Next j
End With
Next i
End With
-- Hope this helps.Doug Robbins - Word MVP,
dkr[atsymbol]mvps[dot]org
Posted via the Community Bridge"associates" wrote in message news:e08531f5-abf2-47bb-8955-be203639d54c@communitybridge.codeplex.com...
Thank you Jay for your reply.
Sorry, it still does not work. I tried all sort of different ways but none works.
However, I found something bizarre happening. When I run the code, the fields in the header does not change but when i manually go to file, then click on print. It then works - I saw the fields in the header changed (which is what I wanted to see). I am not sure if I have applied your code (printpreview) to the right word document.
Basically I have two word documents open - one is the source document that have got texts or paragraphs of word that I wanted to copy and dump into another word document that derived from the template I created in Word.
I have the following codes as written by Doug
Set target = ActiveDocument Set source = Documents.Open("C:/sourceDoc.doc") Set srange = source.Range
I think the one that I need to do print preview is the ActiveDocument.
I tried your code many times. I then tried target.PrintPreview as shown below
' Get the component name and put it into the document If fLine = True Then If Left(srange.Paragraphs(1).Range, 2) = "**" Then ' for example **Capacitor myComponentName = Mid(srange.Paragraphs(1).Range, 3, Len(srange.Paragraphs(1).Range)) ' this gives me Capacitor target.Variables("nl1").Value = myComponentName ' docvariable nl1 is in the body of the report document target.Variables("nl1-2").Value = myComponentName ' docvariable nl1-2 is in the header of the report 'PrintPreview = True 'PrintPreview = False target.PrintPreview target.ClosePrintPreview fLine = False End If End If
This gives me a runtime error 4605 - this method or property is not available because preview mode is not active. The error points to "target.ClosePrintPreview".
I have checked the name of the fields in the header {DOCVARIABLE nl1-2} and it is correct. I have also checked the "Update fields before
printing" is checked.I wonder if there is anything else I can try out.
Thank you in advance
Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org- Marked as answer by Jennifer Zhan Monday, October 11, 2010 2:58 AM
Saturday, October 9, 2010 10:51 PM
All replies
-
You're confusing the value of the variable (which is stored invisibly in the document) with the display of one or more DocVariable fields that may be in the body of the document or in a header or footer. The value of the variable is being changed by your macro, and that applies to the entire document. The problem you're seeing is in the updating of the fields that display the value.
When you select the whole document and press F9 to update fields, or equivalently when your macro executes the statement
ActiveDocument.Fields.Update
that applies only to the body of the document. It doesn't update fields in headers, footers, text boxes, footnotes, or several other places that aren't part of the body.
A simple way to update all fields everywhere in the document is to go into Print Preview and back (assuming the option to update fields when printing is turned on). In code, use these two statements in place of the Fields.Update statement:
PrintPreview = True
PrintPreview = False
Jay Freedman
MS Word MVP FAQ: http://word.mvps.orgFriday, October 8, 2010 1:46 PM -
Thank you Jay for your reply.
I tried using PrintPreview as you suggested but strangely, the fields "nl1-2" in the header does not change at all.
Here is the code
For i = 1 To numchapters ' Get the component name and put it into the document If fLine = True Then If Left(srange.Paragraphs(1).Range, 2) = "**" Then ' for example **Capacitor myComponentName = Mid(srange.Paragraphs(1).Range, 3, Len(srange.Paragraphs(1).Range)) ' this gives me Capacitor target.Variables("nl1").Value = myComponentName ' docvariable nl1 is in the body of the report document target.Variables("nl1-2").Value = myComponentName ' docvariable nl1-2 is in the header of the report PrintPreview = True PrintPreview = False fLine = False End If End If srange.Start = srange.Start + Len(srange.Paragraphs(1).Range) srange.End = srange.Start + InStr(srange, "+") - 1 target.Variables("Chapter" & i).Value = srange srange.Collapse Direction:=wdCollapseEnd srange.End = source.Range.End Next i 'PrintPreview = True 'PrintPreview = False target.Range.Fields.Update
I have got no clue as to why it does not work with the printpreview.
Thank you in advance
Saturday, October 9, 2010 1:16 PM -
When I duplicate this in a document, I have no trouble updating the
fields in the header. However, there are a couple of places you should
verify that you've done things correctly:- Go to the Options dialog and verify that "Update fields before
printing" is checked. I did mention this before, but you may have
missed it. If necessary, the macro could be enhanced to turn that
option on.- Press Alt+F9 and verify that the field code in the header is
{DocVariable "nl1-2"}
The quote marks are optional but recommended.Finally, you should understand that you don't have to have two
different document variables with the same value, just to put them in
two different places. You can copy the field from the document body
and paste it into the header, replacing the one that's now there; and
remove the setting of "nl1-2". There can be many DocVariable fields
that all show the value of the same variable. You'd still have to go
through Print Preview to get the ones in the headers to update (or
expand the macro to update fields in the headers explicitly).
Jay Freedman
MS Word MVP FAQ: http://word.mvps.orgSaturday, October 9, 2010 5:13 PM -
Thank you Jay for your reply.
Sorry, it still does not work. I tried all sort of different ways but none works.
However, I found something bizarre happening. When I run the code, the fields in the header does not change but when i manually go to file, then click on print. It then works - I saw the fields in the header changed (which is what I wanted to see). I am not sure if I have applied your code (printpreview) to the right word document.
Basically I have two word documents open - one is the source document that have got texts or paragraphs of word that I wanted to copy and dump into another word document that derived from the template I created in Word.
I have the following codes as written by Doug
Set target = ActiveDocument Set source = Documents.Open("C:/sourceDoc.doc") Set srange = source.Range
I think the one that I need to do print preview is the ActiveDocument.
I tried your code many times. I then tried target.PrintPreview as shown below
' Get the component name and put it into the document
If fLine = True Then
If Left(srange.Paragraphs(1).Range, 2) = "**" Then ' for example **Capacitor
myComponentName = Mid(srange.Paragraphs(1).Range, 3, Len(srange.Paragraphs(1).Range)) ' this gives me Capacitor
target.Variables("nl1").Value = myComponentName ' docvariable nl1 is in the body of the report document
target.Variables("nl1-2").Value = myComponentName ' docvariable nl1-2 is in the header of the report
'PrintPreview = True
'PrintPreview = False
target.PrintPreview
target.ClosePrintPreview
fLine = False
End If
End IfThis gives me a runtime error 4605 - this method or property is not available because preview mode is not active. The error points to "target.ClosePrintPreview".
I have checked the name of the fields in the header {DOCVARIABLE nl1-2} and it is correct. I have also checked the "Update fields before
printing" is checked.I wonder if there is anything else I can try out.
Thank you in advance
Saturday, October 9, 2010 10:37 PM -
The following will update the fields in the body of the document and then iterate through the all of the Headers and Footers in all of the Sections of the document and update the fields in the Range of each:
Dim i As Long, j As Long
With Target
.Range.Fields.Updatge
For i = 1 To .Sections.Count
With .Sections(i)
For j = 1 To .Headers.Count
.Headers(j).Range.Fields.Update
Next j
For j = 1 To .Footers.Count
.Footers(j).Range.Fields.Update
Next j
End With
Next i
End With
-- Hope this helps.Doug Robbins - Word MVP,
dkr[atsymbol]mvps[dot]org
Posted via the Community Bridge"associates" wrote in message news:e08531f5-abf2-47bb-8955-be203639d54c@communitybridge.codeplex.com...
Thank you Jay for your reply.
Sorry, it still does not work. I tried all sort of different ways but none works.
However, I found something bizarre happening. When I run the code, the fields in the header does not change but when i manually go to file, then click on print. It then works - I saw the fields in the header changed (which is what I wanted to see). I am not sure if I have applied your code (printpreview) to the right word document.
Basically I have two word documents open - one is the source document that have got texts or paragraphs of word that I wanted to copy and dump into another word document that derived from the template I created in Word.
I have the following codes as written by Doug
Set target = ActiveDocument Set source = Documents.Open("C:/sourceDoc.doc") Set srange = source.Range
I think the one that I need to do print preview is the ActiveDocument.
I tried your code many times. I then tried target.PrintPreview as shown below
' Get the component name and put it into the document If fLine = True Then If Left(srange.Paragraphs(1).Range, 2) = "**" Then ' for example **Capacitor myComponentName = Mid(srange.Paragraphs(1).Range, 3, Len(srange.Paragraphs(1).Range)) ' this gives me Capacitor target.Variables("nl1").Value = myComponentName ' docvariable nl1 is in the body of the report document target.Variables("nl1-2").Value = myComponentName ' docvariable nl1-2 is in the header of the report 'PrintPreview = True 'PrintPreview = False target.PrintPreview target.ClosePrintPreview fLine = False End If End If
This gives me a runtime error 4605 - this method or property is not available because preview mode is not active. The error points to "target.ClosePrintPreview".
I have checked the name of the fields in the header {DOCVARIABLE nl1-2} and it is correct. I have also checked the "Update fields before
printing" is checked.I wonder if there is anything else I can try out.
Thank you in advance
Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org- Marked as answer by Jennifer Zhan Monday, October 11, 2010 2:58 AM
Saturday, October 9, 2010 10:51 PM -
I'll recommend that you use Doug's code, as it's more certain to work under all circumstances. So as not to leave an unanswered question dangling, here's an explanation of why you weren't getting the desired result with PrintPreview.
The code you showed in your latest post
Set target = ActiveDocument Set source = Documents.Open("C:/sourceDoc.doc") Set srange = source.Range
tells us that target is the document that was open when the macro started -- it was the ActiveDocument at that time. However, when you then open another document as source, that one becomes the ActiveDocument. In VBA, ActiveDocument refers to whichever document has the focus at the time the particular statement executes , and that can change in the middle of a macro for any of several reasons.
When you then come to set and unset PrintPreview, the source document is the one whose fields (if there are any) will be updated. Since the target document is inactive at that time, its fields won't be updated.
Doug's code, because it specifically calls Update on the fields of the target document, doesn't care whether target is active or not.
Jay Freedman
MS Word MVP FAQ: http://word.mvps.orgSaturday, October 9, 2010 11:51 PM -
Thank you Jay for your reply.
I am deeply grateful that you have tried to explain to me about why it just won't get updated. That's very helpful as I now know what's going on. There is always something new to learn everyday. Thank you very much.
I will keep trying to work with Doug's code. It's just that I have spent lots of time trying out everything that I could think of but none actually works so I am just kind of frustrated.
Thank you once again.
Sunday, October 10, 2010 1:21 AM -
Thank you Doug for your reply.
That is really really helpful. For it works now.
Sunday, October 10, 2010 1:42 AM