Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-06-2015, 05:39 PM
jjfreedman jjfreedman is offline Referencing FieldCodes in VBA Windows 7 64bit Referencing FieldCodes in VBA Office 2013
Advanced Beginner
 
Join Date: May 2012
Location: https://jay-freedman.info
Posts: 39
jjfreedman is on a distinguished road
Default

Um, not like that!



I guess you know that you can't just type braces around a field code in a document, because that doesn't make a working field. You have to use Ctrl+F9 or the Insert > Quick Parts > Field dialog.

Similarly, in VBA you can't just assign a string to make a field. You must use the ActiveDocument.Fields.Add method, passing it parameters that say where to put it and what kind of field it will be.

For example, to insert an {AUTHOR} field at the current insertion point, you would write

Code:
    ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldAuthor
There are a couple of other optional parameters you can use. By default, a field inserted this way includes the \* MERGEFORMAT switch; if you don't want that, you include the parameter PreserveFormatting:=False. The other parameter is called Text, and it can contain any string you need to add to the field's keyword. For example, in an INCLUDETEXT field you need to insert the path\filename of the file to include, so you would write

Code:
ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldIncludeText, _
        Text:=Chr(34) & "D:\\Documents\\eq.docx" & Chr(34), PreserveFormatting:=False
(The Chr(34) represents the double-quote mark, which you need if the path or filename includes any spaces. It's a good idea to include the quotes event if there aren't any spaces. The explicit path\filename in this statement could be replaced with a string variable, and you might not know in advance whether there are spaces in its value.)
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using FieldCodes, AutoText, BuildingBlocks ptmuldoon Word 5 01-02-2015 01:33 PM
Auto referencing SharkAttack Word 5 02-17-2013 07:09 PM
Referencing darksupernova Word 4 11-08-2012 03:37 AM
Multiple referencing SPRITEtoo Word 3 01-29-2012 07:39 PM
Cross referencing maltesedog Word 0 02-13-2009 07:43 AM

Other Forums: Access Forums

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


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