Results 1 to 7 of 7
  1. #1
    Lounger
    Join Date
    Jan 2004
    Location
    Prague, Czech Republic
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    VBA - replacing (Word2k)

    Hello,

    after converting code from VBEditor into HTML, I get this:

    <font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> FormatNumber()
    Regards,
    Martin

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: VBA - replacing (Word2k)

    To work out the code you need, try recording a macro after you have pasted in the text.
    1. Double click the REC on the status bar at the bottom of the screen - you will be prompted for a macro name
    2. Open the replace dialog and search for <font face=Courier New> and replace with <pre>
    3. (The replace for your quotes - I don't understand what you want here)
    4. Do another replace of /font with /pre
    5. Double click the REC to stop recording.

    Then go and have a look at the recorded code you just created. This should give you a head start on coding it.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    Lounger
    Join Date
    Jan 2004
    Location
    Prague, Czech Republic
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA - replacing (Word2k)

    Thanks Andrew,

    the " " should have read &NBSP; replaced with " " (space). The string was lost probably due to HTML here at Woody's. I have written it in capitals now.

    The recorder is definitely a good tip and in this trivial example it helps. What I wanted to know, how do I code something like this:

    Search for last occurence of /font and replace it with /pre or,
    search for first occurence of "<" and ">" and replace the text in between with "pre".
    I am familiar with Excel Object model but regarding Word, I am a complete beginner. I can only insert paragraphs, pictures and do some basic formatting.

    Thanks again
    Regards,
    Martin

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: VBA - replacing (Word2k)

    Using the selection object the code can work along these lines
    <pre> Selection.HomeKey Unit:=wdStory
    With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .text = "<font face=Courier New>"
    .Replacement.text = "<pre>"
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWildcards = False
    .Execute Replace:=wdReplaceOne

    .text = "&NBSP"
    .Replacement.text = " "
    .Wrap = wdFindContinue
    .Execute Replace:=wdReplaceAll

    Selection.EndKey Unit:=wdStory
    .text = "</font>"
    .Replacement.text = "</pre>"
    .Forward = False
    .Wrap = wdFindStop
    .Execute Replace:=wdReplaceOne
    End With</pre>

    You can speed it up by using ranges instead but for this simple example I don't see any real value in going down the range path.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

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

    Re: VBA - replacing (Word2k)

    Steve Roman's book Writing Word Macros has a pretty good introduction to using the Find object.
    See the list of Word VBA books at my URL below.

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

    Re: VBA - replacing (Word2k)

    > You can speed it up by using ranges instead [...]

    Range.find is faster in cases where the corresponding Selection.find often updates the screen.
    Since that doesn't happen in the macro, it wouldn't profit from changing it to ranges.
    So no need to feel guilty ;-)

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

  7. #7
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: VBA - replacing (Word2k)

    But Klaus, if I don't feel guilty for that then I have to find something else to feel guilty about. It's the Catholic schoolboy thing coming out. <img src=/S/brainwash.gif border=0 alt=brainwash width=15 height=15>
    Andrew Lockton, Chrysalis Design, Melbourne Australia

Posting Permissions

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