Results 1 to 8 of 8
  1. #1
    New Lounger
    Join Date
    Apr 2002
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Replacing text with field codes (Word 2000/VBA)

    I haven't visited or posted in the lounge for over a year, so please bear with me if I don't get this quite right the first time back (and with a new format)! I have tried to find similar queries that might already be on the forum, but may have failed to search sufficiently accurately and/or thoroughly.

    I have a problem that raises the following issues:
    - A template with fields has the fields unlinked by some third party software when a document is created (the software can't be discarded or changed)
    - To get around this, I put text codes in place of the fields in the template
    - On creating the new document (first save, actually), I have code to carry out a number of procedures, including a routine to search for the text codes and replace them with field codes
    * Question 1: Can I use VBA to do the equivalent of a Replace All, using field codes rather than text as the replacement object?
    - If the answer to Question 1 is "No", then I have written a loop that finds each occurrence of the text code and replaces it with the field code (presumably less efficient than a Replace All would be)
    * Question 2: Is there any way to get this loop to automatically check the headers and footers as well as the document body?
    - At this stage, the only way I can find to get the search to check everything is to call the routine once each for the body, headers and footers, using nested loops to ensure each section/header/footer/type (ie 1st page, odd, even) is covered; this is similar to answers I have found in this forum for updating and unlinking field codes in headers and footers, so have a bad feeling that there's no better way to do it, but hope to be proven wrong!

    Thanks
    Ali

  2. #2
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    199
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: Replacing text with field codes (Word 2000/VBA)

    Hi Ali,

    This is an answer to the first of your questions.

    Checkout <A target="_blank" HREF=http://mvps.org/word/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm>this reference </A> on the Word MVP site.

    Sorry I can't help with the second question, but would find a solution most useful.

    Hope this helps.

  3. #3
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Replacing text with field codes (Word 2000/VBA)

    Hi Ali,

    I don't have a clean-cut easy answer, either.

    If a lot of the field codes are the same, you could create one field, cut it to the clipboard, and use the clipboard content ^c in a "Replace All".

    If the field codes usually are different, you might use a wildcard search to match the text codes. Then for each match you'd put the matched text in a string variable, remove the start-/end-tags from the string, delete the matched text and insert the field.

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

  4. #4
    New Lounger
    Join Date
    Apr 2002
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Replacing text with field codes (Word 2000/VBA)

    Thanks for responding. The link you gave me was useful because, although it essentially outlines the looping that I'd already resorted to to get it working, I can usually learn something new from the way different people code the same solution, and there are some efficiencies in the article that I can incorporate into my solution.

    Incidentally, my slightly more cumbersome code (looping through sections and specifying Main Text, Headers and Footers separately, rather than looping through all stories), didn't have the problem with blank headers and footers.

    One of the main things I've learned from this link is that I hadn't missed some magic formula or switch for getting a VBA search to automatically include all stories!

    Thanks again

    Ali

  5. #5
    New Lounger
    Join Date
    Apr 2002
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Replacing text with field codes (Word 2000/VBA)

    Thanks for responding. Any one of 5 codes might occur anything from 0 to many times, so I had tried cutting a code and pasting it into the Replace box (without success), but hadn't remembered about the ^c to use the Clipboard contents.

    Your second solution was essentially a variation of the process I've been using, so now I need to translate the Clipboard solution to VBA and combine it with the smarter header/footer looping, and an outer loop for each code, to improve the process and, hopefully, reduce the overhead and run time.

    Thanks again

    Ali

  6. #6
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    199
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: Replacing text with field codes (Word 2000/VBA)

    Hi Ali,

    Re-reading this post to find out what has happened over the weekend reminds me of what I have managed to do in the past to remove fields using a macro, which may or may not be of help, but here goes.

    After displaying fields I did a search to find the field name, and found that manually I could extend the selection from the fieldname to the whole field by backspacing two characters from the start of the fieldname and then selecting the whole field with Ctrl+Shift+Right Arrow and then deleting.

    At least it is a different approach to the standard Search and Replace given its limitations with field codes.

    Hope this helps.

  7. #7
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Replacing text with field codes (Word 2000/VBA)

    I'm not sure it makes sense to use the clipboard. In part, I'm not sure how to get a field onto the clipboard without first cutting it from the document. The DataObject seems limited to unformatted text.

    I think the original looping approach is a good one. I assume yours looks somewhat like this:
    <pre>Sub CodeToField()
    'replace codes with real fields in the current story
    'set up find criteria
    With Selection
    With .Find
    .ClearFormatting
    .Text = "{FakeDateField}"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    .HomeKey Unit:=wdStory, Extend:=wdMove
    While .Find.Execute() = True
    .Fields.Add Range:=Selection.Range, Type:=wdFieldDate, _
    Text:="@ ""MMMM d, yyyy""", PreserveFormatting:=True
    .Collapse Direction:=wdCollapseEnd
    Wend
    'next one
    .Find.Text = "{SomeOtherFakeField}"
    .HomeKey Unit:=wdStory, Extend:=wdMove
    'and so forth
    End With</pre>

    You also could use AutoText entries corresponding to your text codes. This procedure simulates pressing F3 for each word, definitely not a highly efficient approach, and there is the risk of unintended expansions, but I offer it as a possibility.

    <pre>Sub ExpandAllAT()
    Dim rngWord As Range
    On Error Resume Next
    For Each rngWord In ActiveDocument.Words
    rngWord.InsertAutoText
    Next
    End Sub</pre>

    A hybrid Find/InsertAutoText approach would allow you to keep your code as "general" as possible, which some people favor, by putting all the details of the field codes and their formatting in AutoText.

  8. #8
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Replacing text with field codes (Word 2000/VBA)

    > I'm not sure how to get a field onto the clipboard without first cutting it from the document.

    I don't either <img src=/S/grin.gif border=0 alt=grin width=15 height=15>:

    <pre>Dim myRange As Range
    Set myRange = Selection.Range
    Selection.Collapse Direction:=wdCollapseEnd
    Selection.Fields.Add _
    Range:=Selection.Range, _
    text:="((your field code)) ", _
    PreserveFormatting:=False
    Selection.MoveStart Unit:=wdCharacter, count:=-1
    Selection.Cut
    myRange.Select</pre>


    The Macro CodeToField() looks much like I had in mind, but I'd use a wildcard search:
    <pre>With .Find
    .Text = "{*}"
    .MatchWildcards = True
    ...</pre>

    and parse the field text (.Fields.Add Text:= myText) from Selection.Text:
    <pre>myText=mid$(Selection.Text,2,Len(Selection.Te xt)-2)</pre>


    To speed the macro up, you would search for any pseudo-field "{*}", and do a "ReplaceAll" for any one found.

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Regards, Klaus

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •