![]() |
|
|
|
#1
|
|||
|
|||
|
Good morning,
I would like to insert a wdFieldIf or wdFieldExpression type field but I'm having a problem. When I write the text of the field (see below), the starting and ending brace are created and interpreted automatically and correctly by VBA, but the ones inside (around PAGE and NUMPAGES) are not , they are created as standard characters and are not interpreted. Here is my VBA code: ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:="{PAGE} = {NUMPAGES} ""last page""", PreserveFormatting:=True How to do "Ctrl+F9" in VBA code to generate interpretable braces for PAGE and NUMPAGES? Cordially, David |
|
#2
|
|||
|
|||
|
Those braces are not typed text. They are field delimiters that must be inserted as such. You are attempting to build a nested field.
I believe this can be done but do not know, off the top of my head, the construction to do it. I will research this but expect that one of the better coders here will be back with the code before I find the answer. Off hand, the way I would probably approach this would be to create the field in a document and save it as an AutoText entry. Then I would code to insert the entry. Using VBA to Insert an AutoText Entry or other Building Block That may not be the best way but it is the way I could create the field quickly using vba without a lot of research. I generally use AutoText/Building Block entries to hold anything requiring complex formatting to be inserted with vba. In the meantime, I am moving this thread to the Word VBA Forum from the general Word forum.
|
|
#3
|
||||
|
||||
|
You should change the range then add fields e.g.
Code:
Dim oRng As Range
Set oRng = Selection.Range
With oRng
.Fields.Add Range:=oRng, Type:=wdFieldIf, Text:="{PAGE} = {NUMPAGES} ""last page""", PreserveFormatting:=False
.Collapse 1
.MoveEndUntil "}"
.End = .End + 1
.MoveStartUntil "{"
.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
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
#4
|
|||
|
|||
|
Quote:
thanks i will try that too! Cordially |
|
#5
|
|||
|
|||
|
I just gave it a try and it seems to work great!
I will adapt it to my code to insert it in the footer Thank you so much ! |
|
#6
|
|||
|
|||
|
Thank you very much Gmayor !
|
|
#7
|
|||
|
|||
|
Thank you very much for your reply !
Yes I will look again at inserting a buildingbock with an autotext. However, I found this solution quite restrictive for updates Cordially, David |
|
#8
|
|||
|
|||
|
Quote:
I am curious as to how you are seeing the building block approach as restrictive for updates, though. (You need not answer, I am trying to learn myself.) |
|
#9
|
|||
|
|||
|
Yes thanks to Graham gmayor !!!
' first footer ActiveDocument.AttachedTemplate.BuildingBlockEntri es("PDPVF1").Insert Where:=ActiveDocument.Sections(1).Footers(2).Range , RichText:=True ' second footer, even-numbered pages ActiveDocument.AttachedTemplate.BuildingBlockEntri es("PDPVF1").Insert Where:=ActiveDocument.Sections(1).Footers(3).Range , RichText:=True ' third footer, odd-numbered pages ActiveDocument.AttachedTemplate.BuildingBlockEntri es("PDPVF1").Insert Where:=ActiveDocument.Sections(1).Footers(1).Range , RichText:=True ' PDPVF1 is a buildingblock constructed by having selected the desired formatted text written before in the document, including whole the content of IF field : ' PDPVF1 ==> {IF {PAGE} = {NUMPAGES} "my formatted text"} ' OR ' PDPVF1 ==> {IF {PAGE} = {NUMPAGES} {AUTOTEXT YOURAUTOTEXTFORMATTED}} |
|
#10
|
||||
|
||||
|
If you want the fields in the footer, then set the range to the footer. The following will put it in all the footer ranges:
Code:
Sub Macro1()
Dim oSection As Section
Dim oFooter As HeaderFooter
Dim oRng As Range
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 "{"
.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
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 |
|
#11
|
|||
|
|||
|
Great, thanks a lot gmayor!!!
|
|
#12
|
|||
|
|||
|
Quote:
Hi Gmayor! I just have a problem. The IF field is put only in the .Footers(2) the first page, and the.Footers(1) the odd pages. For the .Footers(3) the even pages, nothing is put in and it crashes on the first .Text = "" with runtime error "6028" impossible to delete the range I don't have any locked sections in the document. I have no idea where this can come from? Last edited by Ddadoo57; 02-10-2023 at 09:56 AM. |
|
#13
|
|||
|
|||
|
I added the following code, to manage my autotext PDPVF1
... .Fields.Add Range:=oRng, Type:=wdFieldIf, Text:="{PAGE} = {NUMPAGES} {AUTOTEXT}", PreserveFormatting:=False ... .Collapse 0 .MoveEndUntil "}" .End = .End + 1 .MoveStartUntil "{" .Text = "" .Fields.Add Range:=oRng, Type:=wdFieldAutoText, Text:="PDPVF1", PreserveFormatting:=False ... |
|
#14
|
||||
|
||||
|
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 |
|
#15
|
|||
|
|||
|
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, |
|
|
|
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 |