I really appreciate if someone can help me with the answer. If UCase(Right(files(f), 4)) = ".DOC" ThenĪ (sourc & files(f)) If (GetAttr(sourc & MyName) And vbDirectory) = vbDirectory ThenĬall DoFandAndReplaceInDir(sourc & Subs(s) & "\") MyName = Dir(sourc & "*.*", vbNormal + vbDirectory + vbHidden)
Public Sub DoFandAndReplaceInDir(sourc As String, Optional blndosubdirs As Boolean = True)ĭim i As Integer, s As Integer, f As Integer Execute findtext:="DOCVARIABLE """ & strVal & """"", Forward:=True" If now I want to find FIELD(instead of text enclosed in bracket), how do I change this code? I try this: Private Sub ReplaceFields() "DOCVARIABLE """ & strVal & """", PreserveFormatting:=True Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _ If Left(strTemp, 4) "[IF " Then CheckIFReplace (strTemp)Īnd the replace fields macro as: Private Sub insertdocfield(strVal)
#Microsoft word find and replace fields code#
To find any field (but only if the field code display is toggled 'on'), and you'd then have to: (a) implement a loop to test what each found field contains, so you only process the intended field(s) and (b) use entirely different code for whatever the replacementīFldDisp = Ī = bFldDispĪnd all that code does is display the fields in a message box - no attempt is being made yet to evaluate the field contents or do anything with them.I am working to modify an existing macro finding text enclosed in brackets to replace with fields as this: Private Sub ReplaceFields() If you want to do something with fields via Find/Replace, you could use: However, you cannot do a Find or Replace for a field coded as:īecause you can't use such field coding in a Find expression - even if you're not using VBA. For other control characters you can use in the Find/Replace expression, see: The '^p' is the standard Find/replace expression for a paragraph break. Most controls offer a choice of using the look from the current theme or using a format that you specify directly.^p" You can also format text directly by using the other controls on the Home tab. You create pictures, charts, or diagrams, they also coordinate with your current document look.^pYou can easily change the formatting of selected text in the document text by choosing a look for the selected text from the Quick Styles gallery on the Home tab. You create pictures, charts, or diagrams, they also coordinate with your current document look.^p" You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. Text = "On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document. You can add whatever valid Find & Replace expressions you like to the code.
Simply replace the "Old String" and "New String" parameters with your paragraph's contents - you can even include the paragraph break (^p) to make sure you don't replace something that's only part of a paragraph. If (Not oFolder Is Nothing) Then GetFolder = Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0) Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False) If strFolder & "\" & strFile strDocNm Then StrFile = Dir(strFolder & "\*.doc", vbNormal)
The following macro allows you to browse to a folder containing the documents you want to process, then replace a given string in all documents in that folder automatically.ĭim strFolder As String, strFile As String, strDocNm As String, wdDoc as Document