Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to Word (2003) macros: cannot run on whole document

Status
Not open for further replies.

jimbackus

Technical User
Jul 2, 2002
10
GB
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
 

hi,
Code:
    Dim MyRange As Range
    
    Set MyRange = Selection.Range
    
    MyRange.WholeStory
    
    MyRange.Find.Execute FindText:="^pID :  ", _
    ReplaceWith:="", Replace:=wdReplaceAll

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top