Results 1 to 9 of 9
  1. #1
    Bronze Lounger IanWilson's Avatar
    Join Date
    Dec 2000
    Location
    Bristol, United Kingdom
    Posts
    1,523
    Thanks
    0
    Thanked 1 Time in 1 Post

    Recording a macro doesn't produce expected result (Word 97)

    I have a largish number of documents where I need to replace 'machinery and plant' with 'plant and machinery', 'machinery or plant' with 'plant or machinery', 'Machinery and Plant' with 'Plant and Machinery' and so on. So I started a macro recording, and used the replace facility to go through all the possible combinations. When I was recording the macro, Word replaced everything as expected, including text in the document headers. But when I run the macro, it replaces text in the body of the document OK, but not in the headers. This is a pity, since every single document contains the text 'Machinery and Plant' in the headers, usually in different odd and even headers in more than one section. If I open the header and run the macro it only works for that section's odd or even header as the case may be, not all of them.

    Why doesn't it do the same running the macro as it did recording it?

    Ian <img src=/S/scratch.gif border=0 alt=scratch width=25 height=29>

  2. #2
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Recording a macro doesn't produce expected result (Word 97)

    Hi Ian:
    I'm not a VBA expert, but I have a suggestion. You could simply use Find/Replace without a macro:

    Using Wildcards:
    Find: ([Mm]achinery)(*)([Pp]lants)
    Replace: 321
    Then do Replace All.

    The parentheses break up the pattern into 3 parts: machinery, everything that comes after machinery & before plants, then plants. They are represented by 123.

    The brackets [Mm] say to look for either upper or lowercase. Then you simply replace with the 3rd expression, the 2nd, & then the first. Of course, you'll have to open & close each document manually, so I guess it all depends upon what is meant by a "largish number of documents", but this may be faster than figuring out a macro.

    Cheers,

  3. #3
    Bronze Lounger IanWilson's Avatar
    Join Date
    Dec 2000
    Location
    Bristol, United Kingdom
    Posts
    1,523
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Recording a macro doesn't produce expected result (Word 97)

    Thanks Phil.

    Not sure I wholly follow. Will this replace the case pattern as required i.e. Machinery and plant becomes Plant and machinery, but Machinery and Plant becomes Plant and Machinery, and so on?

    And why does something that happens while the macro is being recorded - the text in the header being replaced - not happen when the same macro is run in the same document? - Ian

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

    Re: Recording a macro doesn't produce expected result (Word 97)

    It will be safer to use
    Find: ([Mm]achinery)(?{1,10})([Pp]lants)

    (this would limit the text between "machinery" and "plants" to a max. of ten characters)

    You could use some other restriction; but (*) is pretty risky: it might match hundreds of paragraphs, so "machinery" in one chapter might get exchanged with "plants" in some other chapter.

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

  5. #5
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Recording a macro doesn't produce expected result (Word 97)

    Because recording a macro does not include all operations performed whilst recording.

    See my rants about recored macros at my web site.

  6. #6
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Recording a macro doesn't produce expected result (Word 97)

    Hi Ian:
    I like the string that Klaus uses better than mine, for the reasons that he mentioned. However, it is not acting correctly on my machine right now, & I can't figure out why. Nevertheless, there is one problem that you raise. I didn't realize that you also had expressions like "Machinery and plant" which were to become "Plant and machinery". i.e. I was thinking that the capitalization of the words would always remain the same.

    I'm now thinking that you might be able to use two operations, something like:
    Find: (Machinery)(?{1,10})(plants)
    Replace All: Plants2machinery

    However, when I try this, it replaces
    Machinery and Plants with
    Plants and Plants

    so I'm missing something. You can do this (using wildcards):
    Find: (Machinery)( )([a-z]{1,6})( )(plants)
    Replace All: Plants234machinery

    for your first operation.

    This says find an expression Machinery uppercase, followed by a space, 1-6 letters (covers in, and, etc.), followed by a space & then lowercase plants. It then replaces it with Plants, followed by space word space, followed by machinery. Harder to explain that to do. Run this Find/Replace All first & then run:
    Find[Mm]achinery)( )([a-z])( )([Pp]lants)
    Replace:54321

    These work, but I'm puzzled why I can't get the string that Klaus uses to work.

  7. #7
    Bronze Lounger IanWilson's Avatar
    Join Date
    Dec 2000
    Location
    Bristol, United Kingdom
    Posts
    1,523
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Recording a macro doesn't produce expected result (Word 97)

    Thanks for all the suggestions. What I resorted to in the end was to assign a button to the macro I had recorded, open each document in turn, click my button once to do the changes in the body of the document, then open the header and click the button again, clicking along to each sections header in turn. Not as elegant as I had hoped, but at about 1 minute per document I got them all done in about half an hour.

    Ian

  8. #8
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Recording a macro doesn't produce expected result (Word 97)

    Ian,

    I missed this thread of a week ago; just want to note:

    It is a known limitation that Find/Replace when done in the user interface will go to all the document's story ranges, while when done in code, it goes only to the main text story.

    But you should be able to get the find/replace code to visit each of the document's story ranges by doing something like this:

    Dim aStory As Range
    For Each aStory In ActiveDocument.StoryRanges
    'put your Find/Replace code here
    Next 'aStory

    - I haven't tested it but it should work.

    Gary

  9. #9
    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: Recording a macro doesn't produce expected result (Word 97)

    If the sections have different headers, you probably have to drill down even deeper. Posts containing both Section and StoryRange probably have the details and sample code. (But I didn't search; too many disturbing memories.)

Posting Permissions

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