R
Rodney Atkins
Hi all.
The macro below works on some machines but not others, and there seems
to be no rhyme or reason to whether it works or not. I've been able to
determine that when it does not work, it's because the Documents.Add
command fails. In other words, a new document is not created, which
then blows the rest of the macro.
It works on my work machine (Word 97 SR2 on Win Xp Pro), but not on
other machines with the same setup. It works on my home machine (Word
2000 on Win XP Home). It does not work on one machine with Word 2002
on Win 2000; it does work on another with Word 2002 and Win XP Home.
I was wondering if anyone had any ideas why this might be occurring.
Thanks.
Rodney
Sub Reforder()
Dim MySelection, DocName, NewRefs
MySelection = Selection.Text
DocName = ActiveDocument.name
Application.ScreenUpdating = False
Documents.Add
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.TypeText MySelection
DoSort
Selection.WholeStory
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
NewRefs = Selection.Text
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
Documents(DocName).Activate
Application.ScreenUpdating = True
Selection.TypeText NewRefs
End Sub
Sub DoSort()
Selection.WholeStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p^p"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "; "
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ", "
.Replacement.Text = "^t"
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " ^p"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.WholeStory
Selection.Sort ExcludeHeader:=False, FieldNumber:="Field 2",
SortFieldType _
:=wdSortFieldNumeric, SortOrder:=wdSortOrderAscending,
FieldNumber2:= _
"Field 1", SortFieldType2:=wdSortFieldAlphanumeric,
SortOrder2:= _
wdSortOrderAscending, Separator:=wdSortSeparateByTabs
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^t"
.Replacement.Text = ", "
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = "; "
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "; ^p"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " ; "
.Replacement.Text = "; "
.Forward = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
The macro below works on some machines but not others, and there seems
to be no rhyme or reason to whether it works or not. I've been able to
determine that when it does not work, it's because the Documents.Add
command fails. In other words, a new document is not created, which
then blows the rest of the macro.
It works on my work machine (Word 97 SR2 on Win Xp Pro), but not on
other machines with the same setup. It works on my home machine (Word
2000 on Win XP Home). It does not work on one machine with Word 2002
on Win 2000; it does work on another with Word 2002 and Win XP Home.
I was wondering if anyone had any ideas why this might be occurring.
Thanks.
Rodney
Sub Reforder()
Dim MySelection, DocName, NewRefs
MySelection = Selection.Text
DocName = ActiveDocument.name
Application.ScreenUpdating = False
Documents.Add
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.TypeText MySelection
DoSort
Selection.WholeStory
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
NewRefs = Selection.Text
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
Documents(DocName).Activate
Application.ScreenUpdating = True
Selection.TypeText NewRefs
End Sub
Sub DoSort()
Selection.WholeStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p^p"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "; "
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ", "
.Replacement.Text = "^t"
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " ^p"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.WholeStory
Selection.Sort ExcludeHeader:=False, FieldNumber:="Field 2",
SortFieldType _
:=wdSortFieldNumeric, SortOrder:=wdSortOrderAscending,
FieldNumber2:= _
"Field 1", SortFieldType2:=wdSortFieldAlphanumeric,
SortOrder2:= _
wdSortOrderAscending, Separator:=wdSortSeparateByTabs
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^t"
.Replacement.Text = ", "
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = "; "
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "; ^p"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " ; "
.Replacement.Text = "; "
.Forward = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub