Results 1 to 7 of 7
  1. #1
    New Lounger
    Join Date
    Jun 2015
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Microsoft Word Find/Replace automation help

    I need to copy a document to notepad to remove formatting and paste it into a new document.

    However, during that process, things like italicized text are lost.

    Currently, I have to manually replace those italics by hand. I have a find and replace process I use to make finding the formally italicized text easy but what I would love to do is run a find/replace selection or script that does it for me. I messed around with using the ^& in the replace box to do this but the results are wildly unreliable.

    Do you know of a way to make this happen?
    Last edited by StudentoftheWrittenWord; 2015-06-23 at 10:28. Reason: Update

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Try the following macro - it should do pretty much all you've specified.
    Code:
    Sub Demo()
    Dim wdDocSrc As Document, wdDocTgt As Document, StrTxt As String
    Dim bOpt01 As Boolean, bOpt02 As Boolean, bOpt03 As Boolean, bOpt04 As Boolean
    Dim bOpt05 As Boolean, bOpt06 As Boolean, bOpt07 As Boolean, bOpt08 As Boolean
    Dim bOpt09 As Boolean, bOpt10 As Boolean, bOpt11 As Boolean, bOpt12 As Boolean
    Dim bOpt13 As Boolean, bOpt14 As Boolean, bOpt15 As Boolean, bOpt16 As Boolean
    Set wdDocSrc = ActiveDocument
    Set wdDocTgt = Documents.Add
    With Application.Options
      bOpt01 = .AutoFormatAsYouTypeApplyHeadings
      bOpt02 = .AutoFormatAsYouTypeApplyBorders
      bOpt03 = .AutoFormatAsYouTypeApplyBulletedLists
      bOpt04 = .AutoFormatAsYouTypeApplyNumberedLists
      bOpt05 = .AutoFormatAsYouTypeApplyTables
      bOpt06 = .AutoFormatAsYouTypeReplaceQuotes
      bOpt07 = .AutoFormatAsYouTypeReplaceSymbols
      bOpt08 = .AutoFormatAsYouTypeReplaceOrdinals
      bOpt09 = .AutoFormatAsYouTypeReplaceFractions
      bOpt10 = .AutoFormatAsYouTypeReplacePlainTextEmphasis
      bOpt11 = .AutoFormatAsYouTypeReplaceHyperlinks
      bOpt12 = .AutoFormatAsYouTypeFormatListItemBeginning
      bOpt13 = .AutoFormatAsYouTypeDefineStyles
      bOpt14 = .TabIndentKey
      .AutoFormatAsYouTypeApplyHeadings = False
      .AutoFormatAsYouTypeApplyBorders = False
      .AutoFormatAsYouTypeApplyBulletedLists = False
      .AutoFormatAsYouTypeApplyNumberedLists = False
      .AutoFormatAsYouTypeApplyTables = False
      .AutoFormatAsYouTypeReplaceQuotes = False
      .AutoFormatAsYouTypeReplaceSymbols = False
      .AutoFormatAsYouTypeReplaceOrdinals = False
      .AutoFormatAsYouTypeReplaceFractions = False
      .AutoFormatAsYouTypeReplacePlainTextEmphasis = False
      .AutoFormatAsYouTypeReplaceHyperlinks = False
      .AutoFormatAsYouTypeFormatListItemBeginning = False
      .AutoFormatAsYouTypeDefineStyles = False
      .TabIndentKey = False
    End With
    With wdDocTgt.Range
      .FormattedText = wdDocSrc.Range.FormattedText
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = ""
        .Forward = True
        .Format = True
        .Font.Italic = True
        .MatchWildcards = False
        .MatchCase = True
        .Replacement.Text = "<i>^&<\i>"
        .Execute Replace:=wdReplaceAll
      End With
      StrTxt = .Text
      .Text = vbNullString
      .InsertAfter StrTxt
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Forward = True
        .Format = True
        .Replacement.Style = "Emphasis"
        .MatchWildcards = True
        .Text = "\<i\>(*)\<\\i\>"
        .Replacement.Text = "\1"
        .Execute Replace:=wdReplaceAll
      End With
    End With
    With Application.Options
      .AutoFormatAsYouTypeApplyHeadings = bOpt01
      .AutoFormatAsYouTypeApplyBorders = bOpt02
      .AutoFormatAsYouTypeApplyBulletedLists = bOpt03
      .AutoFormatAsYouTypeApplyNumberedLists = bOpt04
      .AutoFormatAsYouTypeApplyTables = bOpt05
      .AutoFormatAsYouTypeReplaceQuotes = bOpt06
      .AutoFormatAsYouTypeReplaceSymbols = bOpt07
      .AutoFormatAsYouTypeReplaceOrdinals = bOpt08
      .AutoFormatAsYouTypeReplaceFractions = bOpt09
      .AutoFormatAsYouTypeReplacePlainTextEmphasis = bOpt10
      .AutoFormatAsYouTypeReplaceHyperlinks = bOpt11
      .AutoFormatAsYouTypeFormatListItemBeginning = bOpt12
      .AutoFormatAsYouTypeDefineStyles = bOpt13
      .TabIndentKey = bOpt14
    End With
    End Sub
    Last edited by macropod; 2015-06-23 at 19:44. Reason: code revision
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    New Lounger
    Join Date
    Jun 2015
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question No errors but some glitches.

    The new document that is created appears to have two complete copies of the manuscript and some of the sections that were in italics are still surrounded by <I> and </I>. Oddly enough, those sections appear to be areas where the text is all caps (example: "YES").

    In case I haven't been clear enough, I am deeply appreciative for this help. I sensed it should be possible to do this but all my efforts ended badly.

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    No, you should have two documents - your original and the output version with the same content, except that the new one will have the italics formatting (and no tags), where applicable. I have tested the macro and it works fine, producing the output described.

    Note: You refer to <I></I> tags, but the code uses <i></i> tags. Try inserting:
    .MatchCase = True
    before:
    .Replacement.Text = "<i>^&<\i>"
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    New Lounger
    Join Date
    Jun 2015
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thank you Paul.

    I'm not sure why I am getting the weird double content. It could be the version of Word or user error on my part.

    Than you so much Paul for your time and assistance.

  6. #6
    New Lounger
    Join Date
    Jun 2015
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Book Suggestions

    Paul,

    There are some additional steps I would love to try and automate. What is a good book you would recommend on this subject? I am exclusively working with Word documents.

    I was a VB programmer once upon a time, so I think I could pick this up if I had something to learn from.

    Thank you!

  7. #7
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    I don't know why you'd be getting doubled content. There is nothing in the code to do that. The code explicitly takes whatever is in your source document, replicates it once in the target document, then does all the processing there, the penultimate step of which is to replace all the formatted content with the tagged unformatted equivalent, before replacing the tags with italics.

    All PC versions of Word should perform the same. What version of Word are you using? If it's a Mac version, you may have to replace:
    .Text = vbNullString
    with
    .Text = ""
    as I've seen suggestions Macs don't support vbNullString properly.

    As for books you might read, I'm not across the current titles and most of my Word knowledge & programming skills are self taught, developed whilst helping other users solve their Word productivity issues. A book you might find useful is The Secret Life of Word: A Professional Writer's Guide to Microsoft Word Automation, by R Delwood, published by XML Press in 2011(http://xmlpress.net/publications/word-secrets/). I contributed content for and did much of the technical review of this book. This isn't a programming book as such (though it does have some programming in it) and doesn't profess to teach you how to program. Rather, it shows how to combine Word's various tools to achieve whatever the desired result might be. Another that I contributed to (and has much more programming in it) is Word Hacks, by A Savikas, published by O'Reilly Media in 2005 (http://shop.oreilly.com/product/9780596004934.do). I contributed content for this book also. Although it pre-dates Office 2007, much of the content is still relevant.
    Last edited by macropod; 2015-06-23 at 19:55.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Tags for this Thread

Posting Permissions

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