Need help with a macro (not sure if correct site... thx)

Hi! I'm using MS Word 2003 and am trying to create a macro to edit a very long file I have. The macro would make it so easy to fix, but I can't figure out how to create it correctly. I've been using the macro recorder so it records my moves. I had copied a word using ctrl+shift+left arrow and then tried to find the word again using the "find" function and then pasting what I had copied into the find function.

When I open the macro, it is looking at the find function with the "text" of my previously copied information, i.e., "ARNP" rather than the new copied/pasted info from my next selection. (Does this make sense?) I'll paste the macro below for those who may be able to help. (All I want is to be able to find the copied information rather than always looking for "ARNP".)

Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Copy
Selection.Find.ClearFormatting
With Selection.Find
.Text = "ARNP"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
End Sub

Thanks!

[1375 byte] By [tjohn] at [2007-12-22]
# 1

I use the following macro in much the same way. I select what I want to excise from the document and click on the associated button. If the selection is zero characters wide, you get funny results, so the macro bails if the selection is less than 2 characters long. The code can only use 255 characters at a time in the Find/Replace function, so the macro handles this as well

Sub remove_this_text()
'
' delete selected text through entire document
'
Dim text2go As String
Dim myRange As Range
text2go = Selection
If Len(text2go) < 2 Then Exit Sub
If Len(text2go) > 255 Then text2go = Left(text2go, 255)
Set myRange = ActiveDocument.Range(Start:=0, End:=0)
With myRange.Find
.ClearFormatting
.Text = text2go
With .Replacement
.ClearFormatting
.Text = ""
End With
.Execute Replace:=wdReplaceAll
End With

End Sub

- Jon
-
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_

JonPeltier at 2007-8-30 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...