I have recorded a macro in Normal.dot but it doesn't run as desired. What is required is to process the whole of a document that exists in a separate file. The code fragment below repeats 100 times but ideally it should work through the whole document without using a For:Next loop. Somehow I need to tell the macro to do this but don't know how. Advice please.
About the macro: it processes a Word document that has been exported from a specialised database. It searches for a string then copies some text following the found text and pastes in in before the paragraph mark on the previous line. It has to preserve the paragraph mark otherwise it destroys the style used for the previous paragraph. The it deletes the line with the found text.
Sub Export_cleanup()
'
' Export_cleanup Macro
'
Dim doc As Document, idx As Integer
Selection.Find.ClearFormatting
With Selection.Find
.Text = "^pID : "
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
For idx = 1 To 100
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, count:=1
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, count:=1, Extend:=wdExtend
Selection.Copy
Selection.MoveUp Unit:=wdLine, count:=1
Selection.EndKey Unit:=wdLine
Selection.TypeText Text:=" ["
Selection.PasteAndFormat (wdPasteDefault)
Selection.TypeText Text:="]"
Selection.MoveRight Unit:=wdCharacter, count:=1
Selection.MoveDown Unit:=wdLine, count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, count:=1
Next
End Sub
About the macro: it processes a Word document that has been exported from a specialised database. It searches for a string then copies some text following the found text and pastes in in before the paragraph mark on the previous line. It has to preserve the paragraph mark otherwise it destroys the style used for the previous paragraph. The it deletes the line with the found text.
Sub Export_cleanup()
'
' Export_cleanup Macro
'
Dim doc As Document, idx As Integer
Selection.Find.ClearFormatting
With Selection.Find
.Text = "^pID : "
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
For idx = 1 To 100
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, count:=1
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, count:=1, Extend:=wdExtend
Selection.Copy
Selection.MoveUp Unit:=wdLine, count:=1
Selection.EndKey Unit:=wdLine
Selection.TypeText Text:=" ["
Selection.PasteAndFormat (wdPasteDefault)
Selection.TypeText Text:="]"
Selection.MoveRight Unit:=wdCharacter, count:=1
Selection.MoveDown Unit:=wdLine, count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, count:=1
Next
End Sub