|
Prev: New Page Event
Next: Block a document from email
From: supersub15 on 25 Jun 2008 10:41 Hi there, I'm trying to create a macro that opens Word documents in a folder and searches for the words "Changes due to". However, I want the search to take place in a range that starts at the "Overview" word (in a Heading 2 style) and ends when the next Heading 2 is met. I've done some searches on this forum, but I can't find this specific definition of my range. Thanks for any help. Carlos
From: Helmut Weber on 25 Jun 2008 11:32 Hi Carlos, like that: Sub Test7a() Dim rTmp1 As Range Dim rTmp2 As Range Dim x1 As Long Dim x2 As Long Set rTmp1 = ActiveDocument.Range Set rTmp2 = Selection.Range With rTmp1.Find .Text = "overview" .Style = "Heading 2" If .Execute Then x1 = rTmp1.start rTmp2.start = rTmp1.Paragraphs(1).Next.Range.start rTmp2.End = ActiveDocument.Range.End End If End With With rTmp2.Find .Style = "Heading 2" If .Execute Then x2 = rTmp2.Paragraphs(1).Previous.Range.End End If End With rTmp2.start = x1 rTmp2.End = x2 rTmp2.Select ' for testing With rTmp2.Find .Text = "Changes due to" While .Execute ' your code Wend End With End Sub Though the part of "' your code" might be quite difficult. -- Greetings from Bavaria, Germany Helmut Weber, MVP WordVBA Vista Small Business, Office XP
From: Helmut Weber on 26 Jun 2008 11:25 To all the co-readers, there was a mail contact in between, specifying that there may be more strings like "Changes due to FS[0-9]{5}" in the range, and "FS[0-9]{5}" should be stored in a new doc. Hi Carlos, the question is, what pattern follows the expression you are looking for. I'm assuming, it is "Changes due to " followed by "FS" followed by 5 digits. If so: Sub Test7a() Dim rTmp1 As Range Dim rTmp2 As Range Dim nDoc As Document Dim x1 As Long Dim x2 As Long Set rTmp1 = ActiveDocument.Range Set rTmp2 = Selection.Range With rTmp1.Find .Text = "overview" .Style = "Heading 2" If .Execute Then x1 = rTmp1.start rTmp2.start = rTmp1.Paragraphs(1).Next.Range.start rTmp2.End = ActiveDocument.Range.End End If End With With rTmp2.Find .Style = "Heading 2" If .Execute Then x2 = rTmp2.Paragraphs(1).Previous.Range.End End If End With rTmp2.start = x1 rTmp2.End = x2 rTmp2.Select ' for testing Set nDoc = Documents.Add With rTmp2.Find .ClearFormatting .Text = "Changes due to FS[0-9]{5}" .MatchWildcards = True While .Execute MsgBox Right(rTmp2, 7) ' for testing nDoc.Range.InsertAfter vbCrLf & Right(rTmp2, 7) ' your code Wend End With nDoc.Paragraphs(1).Range.Delete End Sub -- Greetings from Bavaria, Germany Helmut Weber, MVP WordVBA Vista Small Business, Office XP
From: supersub15 on 27 Jun 2008 11:41 On Jun 26, 11:25 am, Helmut Weber <red....(a)t-online.de> wrote: > To all the co-readers, > there was a mail contact in between, > specifying that there may be more strings like > "Changes due to FS[0-9]{5}" in the range, > and "FS[0-9]{5}" should be stored in a new doc. > > Hi Carlos, > > the question is, what pattern follows the expression > you are looking for. I'm assuming, it is > "Changes due to " followed by "FS" followed by 5 digits. > > If so: > > Sub Test7a() > Dim rTmp1 As Range > Dim rTmp2 As Range > Dim nDoc As Document > Dim x1 As Long > Dim x2 As Long > Set rTmp1 = ActiveDocument.Range > Set rTmp2 = Selection.Range > With rTmp1.Find > .Text = "overview" > .Style = "Heading 2" > If .Execute Then > x1 = rTmp1.start > rTmp2.start = rTmp1.Paragraphs(1).Next.Range.start > rTmp2.End = ActiveDocument.Range.End > End If > End With > With rTmp2.Find > .Style = "Heading 2" > If .Execute Then > x2 = rTmp2.Paragraphs(1).Previous.Range.End > End If > End With > rTmp2.start = x1 > rTmp2.End = x2 > rTmp2.Select ' for testing > Set nDoc = Documents.Add > With rTmp2.Find > .ClearFormatting > .Text = "Changes due to FS[0-9]{5}" > .MatchWildcards = True > While .Execute > MsgBox Right(rTmp2, 7) ' for testing > nDoc.Range.InsertAfter vbCrLf & Right(rTmp2, 7) > ' your code > Wend > End With > nDoc.Paragraphs(1).Range.Delete > End Sub > > -- > > Greetings from Bavaria, Germany > > Helmut Weber, MVP WordVBA > > Vista Small Business, Office XP Thanks Helmut. I will give it a try and report back.
|
Pages: 1 Prev: New Page Event Next: Block a document from email |