Search Macro

A

atrscomputers

Hey,


I'm trying to create a macro in word that finds the word Engineer and
when it does copy the first 7 charators of that line and paste it
either to another word document or and excel spreadsheet. I have done
VB for excel macro's but this is confusing me. Any help would be
appreciated.


Thanks
Andrew T Smith
 
H

Helmut Weber

Hi Andrew,

like this:

Sub GetFirstSevenofLine()
Dim rngDcm As Range
Dim rngTmp As Range
Set rngDcm = ActiveDocument.Range
ResetSearch
With rngDcm.Find
.Text = "engineer"
While .Execute
rngDcm.Select
Selection.Bookmarks("\line").Select
Set rngTmp = Selection.Range
rngTmp.End = rngTmp.Start + 7
rngDcm.collapse direction:=wdcollapseend
MsgBox rngTmp.Text ' gotcha. the rest is easy.
Wend
End With
End Sub
' ---
Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub

Note: This is about lines, not paragraphs.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top