Results 1 to 10 of 10
  1. #1
    Star Lounger
    Join Date
    Jan 2003
    Location
    Manchester, Lancashire, England
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Word Swap Macro (Word 2003)

    Does anyone have a neat macro to swap two words or phrases in a Word document including text in headers & footers? I use one that worked well in previous versions before Word 2003 but now fails if more than one document is open. For some reason it flips to the non-current document.

  2. #2
    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: Word Swap Macro (Word 2003)

    Can you walk me through an example of this? I vaguely recall in another word processor (WordPerfect for DOS? WordStar?) that there was a built-in transpose function that switched the words on either side of the cursor/insertion point. This seems not too difficult to code up, and your old macro probably could be adapted to handle it, but the idea of swapping phrases seems more complicated.

  3. #3
    Star Lounger
    Join Date
    Jan 2003
    Location
    Manchester, Lancashire, England
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Swap Macro (Word 2003)

    <P ID="edit" class=small>(Edited by jscher2000 on 02-Dec-03 11:16. [pre] and [/pre] tags added around code to preserve indenting.)</P>Thanks for your interest. My macro is below. It just inputs 2 words or strings then swaps the first for "##" then swaps the 2nd for the first then swaps "##" for the 2nd then does the same for the footer area 'cos although I think the first run thro should catch the footer sometimes it does not. Any thoughts/inputs would be very welcome.

    <pre>Sub Swap()
    '
    ' Swap Macro
    ' Macro recorded 12/04/99 by Ian Douglas
    '
    Dim String1, String2

    String1 = InputBox("Enter First String")
    String2 = InputBox("Enter Second String")

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = String1
    .Replacement.Text = "##"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    With Selection.Find
    .Text = String2
    .Replacement.Text = String1
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    With Selection.Find
    .Text = "##"
    .Replacement.Text = String2
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

    If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
    ActiveWindow.Panes(2).Close
    End If
    If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
    ActivePane.View.Type = wdOutlineView Then
    ActiveWindow.ActivePane.View.Type = wdPrintView
    End If
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    If Selection.HeaderFooter.IsHeader = True Then
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
    Else
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    End If

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = String1
    .Replacement.Text = "##"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    With Selection.Find
    .Text = String2
    .Replacement.Text = String1
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    With Selection.Find
    .Text = "##"
    .Replacement.Text = String2
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    End Sub</pre>


  4. #4
    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: Word Swap Macro (Word 2003)

    I'm having a different problem: I can get it not to change documents, but it doesn't swap in the header, either. Try this on for size. It could be tightened where subsequent headers and footers are linked back, and in fact that code has been posted here before, but at this moment, it's the most I can do:
    <pre>Sub SwapRange()
    ' Replace across all ranges
    Dim String1 As String, String2 As String, rngTemp As Range, docTarget As Document
    Set docTarget = ActiveDocument
    String1 = InputBox("Enter First String")
    String2 = InputBox("Enter Second String")
    ' Exit if user leaves String1 blank
    If Trim(String1) = vbNullString Then Exit Sub
    ' These cover main doc, footnotes, endnotes, and headers/footers of section 1
    For Each rngTemp In docTarget.StoryRanges
    WordSwap String1, String2, rngTemp
    Next
    ' This picks up headers/footers of subsequent sections (if any)
    If docTarget.Sections.Count > 1 Then
    Dim hdrftr As HeaderFooter, intSect As Integer
    For intSect = 2 To docTarget.Sections.Count
    For Each hdrftr In docTarget.Sections(intSect).Headers
    WordSwap String1, String2, hdrftr.Range
    Next
    For Each hdrftr In docTarget.Sections(intSect).Footers
    WordSwap String1, String2, hdrftr.Range
    Next
    Next
    End If
    ' Clean up objects
    If Not hdrftr Is Nothing Then Set hdrftr = Nothing
    If Not rngTemp Is Nothing Then Set rngTemp = Nothing
    If Not docTarget Is Nothing Then Set docTarget = Nothing
    End Sub


    Sub WordSwap(str1 As String, str2 As String, ByVal rng As Range)
    With rng.Find
    ' Set defaults
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    ' First replace
    .Text = str1
    .Replacement.Text = "##"
    .Execute Replace:=wdReplaceAll
    ' Second replace
    .Text = str2
    .Replacement.Text = str1
    .Execute Replace:=wdReplaceAll
    ' Third replace
    .Text = "##"
    .Replacement.Text = str2
    .Execute Replace:=wdReplaceAll
    End With
    Set rng = Nothing
    End Sub</pre>

    Hope this helps.

  5. #5
    Star Lounger
    Join Date
    Jan 2003
    Location
    Manchester, Lancashire, England
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Swap Macro (Word 2003)

    Thanks very much. It seems to work fine for me. In my application I don't have anything in the headers anyway. Thanks once again!!

  6. #6
    Star Lounger
    Join Date
    Jan 2003
    Location
    Manchester, Lancashire, England
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Swap Macro (Word 2003)

    Despite my enthusiasm that my problem was solved it isn't quite. Your macro is better than my original but the problem of confining it to one open doc is unresolved. With one doc open its great with more it jump to the non-focus (if thats the right phrase!) doc. Ant new thoughts??

  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: Word Swap Macro (Word 2003)

    Can you figure out at what step the macro starts operating on the incorrect document? By creating an object reference, and then creating a range using that reference, avoiding all use of selection and window objects, it should in theory operate only on the one document. But then, I don't have the latest and greatest version of Word. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    (If the problem is not that changes are made to a different document but that focus shifts at some point, you could re-activate the original document before destroying the object reference.)

  8. #8
    Star Lounger
    Join Date
    Jan 2003
    Location
    Manchester, Lancashire, England
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Swap Macro (Word 2003)

    Thanks for your interest. Unhappily the problem is inconsistant. Mostly I don't see the effect & sometimes I do. I will take more note of when it happens & try to answer your questions next time then.

  9. #9
    Star Lounger
    Join Date
    Jan 2003
    Location
    Manchester, Lancashire, England
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Swap Macro (Word 2003)

    Sometimes the macro is perfectly behaved & just does its job with the screen view just about static. Othertimes the screen view leaps about the active document with text being selected as it sworks then just as it finishes the focus swaps to the non active doc but nothing more happens in the non active document. In this 'mode' the entire transpose operation takes many times longer (but still only seconds) than the 'correct' mode. I haven't been able to distinguish why the latter sequence happens but, of course, want only the former!! Any thoughts??

  10. #10
    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: Word Swap Macro (Word 2003)

    I think I'll hold off on installing Word 2003 for a little while longer. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    Unless some other code is getting called that we have not discussed in this thread, I really can't understand why a different document suddenly receives focus.

Posting Permissions

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