K
Kurtis
I have a Table of Contents in a document that I am trying to add tab spaces
after the words Article and any number or roman numeral that may appear.
Because I don't want any other occurrence of Article being altered, I am
cutting and pasting the TOC into a new blank document, running a Find, adding
tab spaces, and then cutting and pasting back into the old document.
Everything works fine, except that I am still being prompted to Save the
document, even though I am using the wdDoNotSaveChanges command. Below is
the entire code. Any help would be greatly appreciated. Thanks.
Selection.HomeKey Unit:=wdStory
Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="TOC"
Selection.Find.ClearFormatting
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Cut
Documents.Add DocumentType:=wdNewBlankDocument
Selection.PasteAndFormat (wdPasteDefault)
With Selection.Find
.Text = "article ^#^# "
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindStop
End With
Selection.Find.Execute
While Selection.Find.Found
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeBackspace
Selection.TypeText Text:=vbTab
Selection.Find.Execute
Wend
Selection.HomeKey Unit:=wdStory
Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="TOC"
Selection.Find.ClearFormatting
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1
With Selection.Find
.Text = "article ^# "
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindStop
End With
Selection.Find.Execute
While Selection.Find.Found
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeBackspace
Selection.TypeText Text:=vbTab
Selection.Find.Execute
Wend
Selection.HomeKey Unit:=wdStory
Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="TOC"
Selection.Find.ClearFormatting
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1
With Selection.Find
.Text = "article ^$^$^$^$^$^$ "
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindStop
End With
Selection.Find.Execute
While Selection.Find.Found
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeBackspace
Selection.TypeText Text:=vbTab
Selection.Find.Execute
Wend
Selection.HomeKey Unit:=wdStory
Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="TOC"
Selection.Find.ClearFormatting
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1
With Selection.Find
.Text = "article ^$^$^$^$^$ "
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindStop
End With
Selection.Find.Execute
While Selection.Find.Found
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeBackspace
Selection.TypeText Text:=vbTab
Selection.Find.Execute
Wend
Selection.HomeKey Unit:=wdStory
Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="TOC"
Selection.Find.ClearFormatting
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1
With Selection.Find
.Text = "article ^$^$^$^$ "
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindStop
End With
Selection.Find.Execute
While Selection.Find.Found
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeBackspace
Selection.TypeText Text:=vbTab
Selection.Find.Execute
Wend
Selection.HomeKey Unit:=wdStory
Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="TOC"
Selection.Find.ClearFormatting
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1
With Selection.Find
.Text = "article ^$^$^$ "
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindStop
End With
Selection.Find.Execute
While Selection.Find.Found
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeBackspace
Selection.TypeText Text:=vbTab
Selection.Find.Execute
Wend
Selection.HomeKey Unit:=wdStory
Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="TOC"
Selection.Find.ClearFormatting
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1
With Selection.Find
.Text = "article ^$^$ "
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindStop
End With
Selection.Find.Execute
While Selection.Find.Found
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeBackspace
Selection.TypeText Text:=vbTab
Selection.Find.Execute
Wend
Selection.HomeKey Unit:=wdStory
Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="TOC"
Selection.Find.ClearFormatting
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1
With Selection.Find
.Text = "article ^$ "
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindStop
End With
Selection.Find.Execute
While Selection.Find.Found
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeBackspace
Selection.TypeText Text:=vbTab
Selection.Find.Execute
Wend
Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Cut
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
Selection.PasteAndFormat (wdPasteDefault)
End Sub
after the words Article and any number or roman numeral that may appear.
Because I don't want any other occurrence of Article being altered, I am
cutting and pasting the TOC into a new blank document, running a Find, adding
tab spaces, and then cutting and pasting back into the old document.
Everything works fine, except that I am still being prompted to Save the
document, even though I am using the wdDoNotSaveChanges command. Below is
the entire code. Any help would be greatly appreciated. Thanks.
Selection.HomeKey Unit:=wdStory
Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="TOC"
Selection.Find.ClearFormatting
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Cut
Documents.Add DocumentType:=wdNewBlankDocument
Selection.PasteAndFormat (wdPasteDefault)
With Selection.Find
.Text = "article ^#^# "
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindStop
End With
Selection.Find.Execute
While Selection.Find.Found
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeBackspace
Selection.TypeText Text:=vbTab
Selection.Find.Execute
Wend
Selection.HomeKey Unit:=wdStory
Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="TOC"
Selection.Find.ClearFormatting
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1
With Selection.Find
.Text = "article ^# "
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindStop
End With
Selection.Find.Execute
While Selection.Find.Found
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeBackspace
Selection.TypeText Text:=vbTab
Selection.Find.Execute
Wend
Selection.HomeKey Unit:=wdStory
Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="TOC"
Selection.Find.ClearFormatting
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1
With Selection.Find
.Text = "article ^$^$^$^$^$^$ "
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindStop
End With
Selection.Find.Execute
While Selection.Find.Found
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeBackspace
Selection.TypeText Text:=vbTab
Selection.Find.Execute
Wend
Selection.HomeKey Unit:=wdStory
Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="TOC"
Selection.Find.ClearFormatting
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1
With Selection.Find
.Text = "article ^$^$^$^$^$ "
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindStop
End With
Selection.Find.Execute
While Selection.Find.Found
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeBackspace
Selection.TypeText Text:=vbTab
Selection.Find.Execute
Wend
Selection.HomeKey Unit:=wdStory
Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="TOC"
Selection.Find.ClearFormatting
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1
With Selection.Find
.Text = "article ^$^$^$^$ "
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindStop
End With
Selection.Find.Execute
While Selection.Find.Found
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeBackspace
Selection.TypeText Text:=vbTab
Selection.Find.Execute
Wend
Selection.HomeKey Unit:=wdStory
Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="TOC"
Selection.Find.ClearFormatting
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1
With Selection.Find
.Text = "article ^$^$^$ "
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindStop
End With
Selection.Find.Execute
While Selection.Find.Found
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeBackspace
Selection.TypeText Text:=vbTab
Selection.Find.Execute
Wend
Selection.HomeKey Unit:=wdStory
Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="TOC"
Selection.Find.ClearFormatting
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1
With Selection.Find
.Text = "article ^$^$ "
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindStop
End With
Selection.Find.Execute
While Selection.Find.Found
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeBackspace
Selection.TypeText Text:=vbTab
Selection.Find.Execute
Wend
Selection.HomeKey Unit:=wdStory
Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="TOC"
Selection.Find.ClearFormatting
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1
With Selection.Find
.Text = "article ^$ "
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindStop
End With
Selection.Find.Execute
While Selection.Find.Found
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeBackspace
Selection.TypeText Text:=vbTab
Selection.Find.Execute
Wend
Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Cut
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
Selection.PasteAndFormat (wdPasteDefault)
End Sub