Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-09-2023, 05:58 AM
Ddadoo57 Ddadoo57 is online now ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Windows 11 ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Office 2021
Advanced Beginner
...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=...
 
Join Date: Feb 2023
Posts: 73
Ddadoo57 is on a distinguished road
Default ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=...

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
Reply With Quote
  #2  
Old 02-09-2023, 06:59 AM
Charles Kenyon Charles Kenyon is offline ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Windows 11 ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,125
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

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.
Reply With Quote
  #3  
Old 02-09-2023, 07:15 AM
gmayor's Avatar
gmayor gmayor is offline ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Windows 10 ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #4  
Old 02-09-2023, 07:25 AM
Ddadoo57 Ddadoo57 is online now ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Windows 11 ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Office 2021
Advanced Beginner
...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=...
 
Join Date: Feb 2023
Posts: 73
Ddadoo57 is on a distinguished road
Default

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
Reply With Quote
  #5  
Old 02-09-2023, 07:29 AM
Ddadoo57 Ddadoo57 is online now ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Windows 11 ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Office 2021
Advanced Beginner
...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=...
 
Join Date: Feb 2023
Posts: 73
Ddadoo57 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
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


thanks i will try that too!

Cordially
Reply With Quote
  #6  
Old 02-09-2023, 07:47 AM
Ddadoo57 Ddadoo57 is online now ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Windows 11 ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Office 2021
Advanced Beginner
...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=...
 
Join Date: Feb 2023
Posts: 73
Ddadoo57 is on a distinguished road
Default

Quote:
Originally Posted by Ddadoo57 View Post
thanks i will try that too!

Cordially
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 !
Reply With Quote
  #7  
Old 02-09-2023, 08:14 AM
Charles Kenyon Charles Kenyon is offline ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Windows 11 ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,125
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Quote:
Originally Posted by Ddadoo57 View Post
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
It appears that Graham has given you direct code. He has long been one of my heroes!

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.)
Reply With Quote
  #8  
Old 02-09-2023, 09:30 AM
Ddadoo57 Ddadoo57 is online now ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Windows 11 ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Office 2021
Advanced Beginner
...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=...
 
Join Date: Feb 2023
Posts: 73
Ddadoo57 is on a distinguished road
Default

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}}
Reply With Quote
  #9  
Old 02-09-2023, 09:31 AM
Ddadoo57 Ddadoo57 is online now ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Windows 11 ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Office 2021
Advanced Beginner
...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=...
 
Join Date: Feb 2023
Posts: 73
Ddadoo57 is on a distinguished road
Default

Thank you very much Gmayor !
Reply With Quote
  #10  
Old 02-09-2023, 10:34 PM
gmayor's Avatar
gmayor gmayor is offline ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Windows 10 ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #11  
Old 02-10-2023, 01:27 AM
Ddadoo57 Ddadoo57 is online now ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Windows 11 ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Office 2021
Advanced Beginner
...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=...
 
Join Date: Feb 2023
Posts: 73
Ddadoo57 is on a distinguished road
Thumbs up

Great, thanks a lot gmayor!!!
Reply With Quote
  #12  
Old 02-10-2023, 01:31 AM
Ddadoo57 Ddadoo57 is online now ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Windows 11 ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Office 2021
Advanced Beginner
...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=...
 
Join Date: Feb 2023
Posts: 73
Ddadoo57 is on a distinguished road
Default

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
...
Reply With Quote
  #13  
Old 02-10-2023, 06:49 AM
Ddadoo57 Ddadoo57 is online now ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Windows 11 ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Office 2021
Advanced Beginner
...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=...
 
Join Date: Feb 2023
Posts: 73
Ddadoo57 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
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
/*******************************************/

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.
Reply With Quote
  #14  
Old 02-10-2023, 10:19 PM
gmayor's Avatar
gmayor gmayor is offline ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Windows 10 ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #15  
Old 02-11-2023, 06:28 AM
Ddadoo57 Ddadoo57 is online now ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Windows 11 ...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Office 2021
Advanced Beginner
...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=...
 
Join Date: Feb 2023
Posts: 73
Ddadoo57 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
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


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,
Reply With Quote
Reply



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
...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... Working with Selection.range. PRA007 Word VBA 2 02-19-2016 12:52 AM
...Fields.Add Range:=Selection.Range, Type:=wdFieldIf, Text:=... 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

Other Forums: Access Forums

All times are GMT -7. The time now is 03:05 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft