PDA

View Full Version : [SOLVED:] How to use wdFieldAdvance? Can't get it to work



lkpederson
05-11-2015, 07:10 AM
Macro below is a modification of one Paul Edstein (macropod) posted. I'm trying to get the text "page left intentionally blank" to center on "blank page".


Sub InsertBlankPageField()
Dim Sctn As Section, Rng As Range

' see if there are any field codes first. If not, exit. If so, see if the
' field codes have the IF in them, trying to find code for adding page for
' documents ending with odd number of pages.
If ActiveDocument.Fields.Count = 0 Then
With ActiveDocument
For Each Sctn In .Sections
With Sctn.Range
' move to end of document, set rng at the end
' then collapse range to after end of last word
Selection.EndKey Unit:=wdStory

Set Rng = .Characters.Last.Previous
Rng.Collapse wdCollapseEnd
.Fields.Add Range:=Rng, Type:=wdFieldEmpty, _
PreserveFormatting:=False, Text:="IF = 1"

Set Rng = .Fields(1).Code
Rng.Start = Rng.Start + 3
Rng.Collapse wdCollapseStart
.Fields.Add Range:=Rng, Type:=wdFieldEmpty, _
PreserveFormatting:=False, Text:="=MOD(,2)"

Set Rng = .Fields(2).Code
Rng.Start = Rng.Start + 6
Rng.Collapse wdCollapseStart
.Fields.Add Range:=Rng, Type:=wdFieldPage, _
PreserveFormatting:=False

Set Rng = .Fields(1).Code
Rng.End = Rng.End
Rng.Collapse wdCollapseEnd
.Fields.Add Range:=Rng, Type:=wdFieldEmpty, _
PreserveFormatting:=False, Text:="QUOTE 12" & Chr(34) & Chr(13) & _
Chr(13) & "This page intentionally left blank." & Chr(34)

' Set Rng = .Fields(4).Code
' Rng.Start = Rng.Start + 9
' Rng.Collapse wdCollapseStart
' .Fields.Add Range:=Rng, Type:=wdFieldAdvance, Text:="\d 300"
.Fields.Update
End With
Next
End With
End Sub


I've tried various and sundry combinations and have had no luck. I have found that whenever I do use wdFieldAdvance, the text won't show up on the page.

Any takers?

/TIA
Lise

gmayor
05-11-2015, 09:28 PM
I have not tested the code, but to centre the text (between the margins) on the page, what you need is to add

Rng.ParagraphFormat.Alignment = wdAlignParagraphCenter
Rng.PageSetup.VerticalAlignment = wdAlignVerticalCenter


Before
.Fields.Update

Your code also has a missing
End If

before
End Sub

lkpederson
05-29-2015, 12:52 PM
Hi Graham,
Sorry for the belated response. Your solution moves ALL the text on the page and centers it. I just want to center the phrase "This page intentionally left blank".

gmayor
05-30-2015, 09:38 PM
As said - it was untested. Having tested, I think you could use


.Fields.Add Range:=Rng, Type:=wdFieldEmpty, _
PreserveFormatting:=False, _
Text:="QUOTE 12" & Chr(34) & _
Chr(13) & Chr(13) & Chr(13) & Chr(13) & Chr(13) & Chr(13) & _
Chr(13) & Chr(13) & Chr(13) & Chr(13) & Chr(13) & Chr(13) & _
"This page intentionally left blank." & Chr(34)
.Paragraphs.Last.Alignment = wdAlignParagraphCenter
.Fields.Update


The number of line breaks would depend on the style at the field position.

lkpederson
07-05-2017, 03:20 PM
Finally found a solution. Never say never.

The key is using select to move the cursor into the correct position.


For Each cSect In ActiveDocument.Sections 'Get the number of the current section
SectID = cSect.Index
' set Range
Set Rng = cSect.Range
' resize to NOT include Section break
Rng.MoveEnd Unit:=wdCharacter, Count:=-1
' get number of pages
Pgs = Rng.ComputeStatistics(wdStatisticPages)
'If pages/2 = 0 then even number, so do process
If Pgs Mod 2 <> 0 Then
With Rng
'Go to the end of the section
.Collapse (wdCollapseEnd)
'Delete the existing section break
'insert a page break
.InsertBreak Type:=wdPageBreak
.SetRange Start:=Rng.End - 1, End:=Rng.End '+ 1
.Fields.Add Range:=Rng, Type:=wdFieldAdvance, Text:="\d 300"
.Paragraphs.Alignment = wdAlignParagraphCenter
.Style = "_11_CSI END OF SECTION"
.Collapse (wdCollapseEnd)
End With
ActiveDocument.Fields(1).Select
Selection.InsertAfter Text:="This page left intentionally blank"

End If

Next cSect