![]() |
|
|
|
#1
|
||||
|
||||
|
The error occurs if there are fields in the footer and the field codes are not visible. Fix it with Code:
Sub Macro1()
Dim oSection As Section
Dim oFooter As HeaderFooter
Dim oRng As Range
Dim bCodes As Boolean
bCodes = ActiveWindow.ActivePane.View.ShowFieldCodes
ActiveWindow.ActivePane.View.ShowFieldCodes = True
For Each oSection In ActiveDocument.Sections
For Each oFooter In oSection.Footers
If oFooter.Exists Then
Set oRng = oFooter.Range
With oRng
.Collapse 0
.Fields.Add Range:=oRng, Type:=wdFieldIf, Text:="{PAGE} = {NUMPAGES} ""last page""", PreserveFormatting:=False
.Collapse 1
.MoveEndUntil "}"
.End = .End + 1
.MoveStartUntil "{"
oRng.Select
.Text = ""
.Fields.Add Range:=oRng, Type:=wdFieldPage, PreserveFormatting:=False
.Collapse 0
.MoveEndUntil "}"
.End = .End + 1
.MoveStartUntil "{"
.Text = ""
.Fields.Add Range:=oRng, Type:=wdFieldNumPages, PreserveFormatting:=False
.Fields.Update
End With
End If
Next oFooter
Next oSection
ActiveWindow.ActivePane.View.ShowFieldCodes = bCodes
lbl_Exit:
Set oSection = Nothing
Set oFooter = Nothing
Set oRng = Nothing
Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
#2
|
|||
|
|||
|
Quote:
Thank you Gmayor, I will adapt the code to my needs and send you back what I have modified and/or added. Maybe it will help someone a little bit too. If not, I only had hidden fields in the headers, not in the footers. It's amazing that it takes that into account. Best regards, |
|
#3
|
|||
|
|||
|
Quote:
The problem I have now is that when updating the fields, for each footer, the screen display changes, it goes from normal mode to draft mode or something like that, and it's not great for the user. I partly solve this problem with : Application.Visible = False ... Application.Visible = True ActiveWindow.View.Type = wdNormalView ActiveWindow.View.Type = wdPrintView Well, it's not great, since the application disappears and comes back once the job is done. Is there any solution to keep the screen from moving or changing, like "oFooter.Range.Delete" does so nicely? Best regards, David |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Distribute text in one cell across a range of cells (overcoming selection.range.cells.count bug) | slaycock | Word VBA | 0 | 02-18-2017 07:00 AM |
Working with Selection.range.
|
PRA007 | Word VBA | 2 | 02-19-2016 12:52 AM |
Search and reduce the range of a text selection
|
paik1002 | Word VBA | 1 | 12-17-2015 04:51 AM |
| Selection or Range | Tommes93 | Word VBA | 1 | 04-10-2014 02:50 AM |