PDA

View Full Version : VBA to toggle "Show field codes insted of their values"?



YossiD
10-21-2023, 11:55 PM
Is there a way to toggle the option "Show field codes instead of their values" in a macro? I could not find it in the Options object.

jdelano
10-22-2023, 01:10 AM
One option, though not very sexy, is using SendKeys; SendKeys statement (VBA) | Microsoft Learn (https://learn.microsoft.com/en-us/office/vba/Language/Reference/user-interface-help/sendkeys-statement). You can see which keys to use by pressing the Alt key and then they keyboard shortcuts to access each menu will display. Some items need a TAB and, like the Options menu, some need use of the ARROW keys. I did it just now and found that using this sequence brought me to that option:

F, T, 6 Down Arrows, TAB, F twice (you'll see that there are two options in the advanced settings that have an f underlined - denoting pressing f jumps to that setting) then pressing SPACE BAR to toggle the check mark.

There is also, just ALT + F9 to do it in the document that is open SendKeys "%{F9}" if you're not setting this for every document created in Word.

Chas Kenyon
10-25-2023, 12:35 PM
First, are you aware that there is already a bulit-in keyboard shortcut to do this. Windows Alt+F9 (Fn+Alt+F9 on many computers)

The following code will toggle the setting:

ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes


Note that although it is about the ActiveWindow object, it actually toggles the option setting.

I have the following macro in my Normal.dotm:


Sub AutoOpen()
' Charles Kenyon 2023-Oct-25
'Turns of display of field codes upon opening any document
ActiveWindow.View.ShowFieldCodes = False
End Sub

Note that toggling, changing, this setting does not change what is displayed in open documents other than the active document until they are closed and reopened.
If you have multiple documents open that you want changed do not apply it to each open document because you are changing a system, not a document setting. You might be able to do a screen refresh instead.

peevishscarf
11-20-2023, 03:03 AM
First, are you aware that there is already a bulit-in keyboard shortcut to do this. Windows Alt+F9 (Fn+Alt+F9 on many computers)

The following code will toggle the setting:

ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
dinosaur game (https://dinosaurgames.io)

Note that although it is about the ActiveWindow object, it actually toggles the option setting.

I have the following macro in my Normal.dotm:


Sub AutoOpen()
' Charles Kenyon 2023-Oct-25
'Turns of display of field codes upon opening any document
ActiveWindow.View.ShowFieldCodes = False
End Sub

Note that toggling, changing, this setting does not change what is displayed in open documents other than the active document until they are closed and reopened.
If you have multiple documents open that you want changed do not apply it to each open document because you are changing a system, not a document setting. You might be able to do a screen refresh instead.
Oh now I just learned about these shortcuts. Thank you.

gmaxey
11-20-2023, 05:50 AM
There is also a shortcut Shift+F9 that toggles the current field (or selected fields). Note: if this shortcut isn't working the you likely have another program which has taken over the Word shortcut.

Chas Kenyon
11-24-2023, 08:38 PM
Hi Greg,

Shift+F9 does not toggle the option, it is true.
That is a good thing in that you do not have to remember to toggle the settings back.

However, if the field is nested (one or more other fields inside the field) only the top (or outer) field layer is toggled. The internal nested fields just show their results.
Try Shift+F9 with one of Paul Edstein's calculated dates!

Also if you are trying to modify fields using vba, displaying the field coding usually works better, even with screen updating turned off.



Function Keys and Keyboard Shortcuts that Manipulate Fields
(https://www.addbalance.com/usersguide/fields.htm#Function)
How to Toggle the Display of Field Codes in Word (my page) (https://www.addbalance.com/usersguide/fields.htm#Display)
(https://www.addbalance.com/usersguide/fields.htm#Function)


P.S. I know that you, Greg, know this stuff. You've taught me more about fields and vba than just about anyone.

gmaxey
11-25-2023, 05:48 AM
Charles,
Like you, I'm learning everyday. Thanks for the enhancement on my post.

zepplin
03-11-2024, 06:46 PM
To change from the field code to the merge field, or vice versa, in a Microsoft Word document: Press ALT + F9 to toggle Field Codes on/off.