PDA

View Full Version : How to run a macro in an particular area



Rakesh
08-14-2010, 01:25 PM
:help
Hi Rocks,

I had created a macro for replacement using wildcards. It works fine. But what I need is, it should be run in certain condition only.

The condition is, If the line starts with the word “Series” after the Heading, the macro should run upto the next Heading (Like that throughout the Document)

Can it possible to do it?
Below is the Coding, Sample Data and Output Data for your reference
Sub series()
ActiveDocument.Content.Find.Execute _
FindText:="(Series[!\n]@)\n([!\n0-9]@)\n([!\n0-9]@)\n([0-9][!\n]@[0-9]\n)", _
MatchWildcards:=True, _
ReplaceWith:="\1^t\2^t\3^t\4", _
replace:=wdReplaceAll
ActiveDocument.Content.Find.Execute _
FindText:="(Series[!\n]@)\n([!\n0-9]@)\n([0-9][!\n]@[0-9]\n)", _
MatchWildcards:=True, _
ReplaceWith:="\1^t\2^t\3", _
replace:=wdReplaceAll
ActiveDocument.Content.Find.Execute _
FindText:="(Series[!\n]@)\n([0-9][!\n]@[0-9]\n)", _
MatchWildcards:=True, _
ReplaceWith:="\1^t\2", _
replace:=wdReplaceAll
End Sub


SAMPLE DATA

FA Educational Facilities Authority (Heading)
College of Arts,
Series 2005:
5.000% 06/01/26 1,000,000 892,930
5.000% 06/01/35 1,500,000 1,252,395
California Lutheran University,
Series 2008,
5.750% 10/01/38 3,000,000 3,029,730
FA University (Heading)
Series 2007,
Insured: AGMC
4.500% 05/15/35 4,000,000 3,917,480
Series 2009 A,
6.000% 11/01/40 2,000,000 2,193,680
Series 2009 O,
5.250% 05/15/39 4,000,000 4,282,120
Series 2010 A,
5.000% 11/01/30 2,995,000 3,073,259
FA Statewide Communities Development Authority (Heading)
College for Certain LLC,
Series 2010,
GTY AGMT: PCSD Guaranty Pool LLC
6.000% 07/01/30 2,000,000 2,007,840
Crossroads School for Arts<\!s>& Sciences,
Series 1998,
6.000% 08/01/28 (a) 1,645,000 1,588,264
FA Health Facilities Financing Authority (Heading)

OUTPUT DATA

FA Educational Facilities Authority (Heading)
College of Arts,
Series 2005:
5.000% 06/01/26 1,000,000 892,930
5.000% 06/01/35 1,500,000 1,252,395
California Lutheran University,
Series 2008,
5.750% 10/01/38 3,000,000 3,029,730
FA University (Heading)
Series 2007, Insured: AGMC 4.500% 05/15/35 4,000,000 3,917,480
Series 2009 A, 6.000% 11/01/40 2,000,000 2,193,680
Series 2009 O, 5.250% 05/15/39 4,000,000 4,282,120
Series 2010 A, 5.000% 11/01/30 2,995,000 3,073,259
FA Statewide Communities Development Authority (Heading)
College for Certain LLC,
Series 2010,
GTY AGMT: PCSD Guaranty Pool LLC
6.000% 07/01/30 2,000,000 2,007,840
Crossroads School for Arts<\!s>& Sciences,
Series 1998,
6.000% 08/01/28 (a) 1,645,000 1,588,264
FA Health Facilities Financing Authority (Heading)

Thanks in Advance
Rakesh

Tinbendr
08-15-2010, 11:47 AM
You basically define a range based on your parameters, then pass the range to your sub.

I hope this does it.

Sub DefineRange()

Dim aDoc As Document
Dim Rng1 As Range
Dim Rng2 As Range

Set aDoc = ActiveDocument
Set Rng1 = aDoc.Range
Set Rng2 = Rng1.Duplicate

Do
With Rng1.Find
.ClearFormatting
.Text = "heading"
'.Style = "Heading1" 'The style Heading 1
.Replacement.Text = ""
.Forward = True
.MatchCase = False
.Execute
End With

If Rng1.Find.Found Then
Rng2.Start = Rng1.End + 1
Rng1.End = aDoc.Range.End

With Rng2.Find
.Text = "Heading" 'Literal
'.Style = "Heading1" 'The style Heading 1
.Replacement.Text = ""
.Forward = True
.MatchCase = False
.Execute
End With

If Rng2.Find.Found Then
Rng2.Collapse wdCollapseStart
Series aDoc.Range(Rng1.Start, Rng2.End)
Else
Rng2.Collapse wdCollapseStart
Series aDoc.Range(Rng1.Start, aDoc.Range.End)
End If
End If
Rng1.Start = Rng2.End + 1

Loop While Rng1.Find.Found

End Sub

Sub Series(Rng As Range)
Rng.Find.Execute _
FindText:="(Series[!\n]@)\n([!\n0-9]@)\n([!\n0-9]@)\n([0-9][!\n]@[0-9]\n)", _
MatchWildcards:=True, _
ReplaceWith:="\1^t\2^t\3^t\4", _
Replace:=wdReplaceAll

Rng.Find.Execute _
FindText:="(Series[!\n]@)\n([!\n0-9]@)\n([0-9][!\n]@[0-9]\n)", _
MatchWildcards:=True, _
ReplaceWith:="\1^t\2^t\3", _
Replace:=wdReplaceAll

Rng.Find.Execute _
FindText:="(Series[!\n]@)\n([0-9][!\n]@[0-9]\n)", _
MatchWildcards:=True, _
ReplaceWith:="\1^t\2", _
Replace:=wdReplaceAll
End Sub

fumei
08-15-2010, 05:54 PM
And let's hope real heading styles are being used.

Rakesh
08-17-2010, 03:51 PM
Hi,

I have tried it. But it replaced all occurrence.
It not works as I mention in the first post.

If the line starts with the word “Series” after the Heading, the macro should run upto the next Heading (Like that throughout the Document)


Thanks,
Rakesh

fumei
08-18-2010, 12:48 AM
And are the paragraphs - example :

FA Health Facilities Financing Authority (Heading)


real headings? Or are they what you think of as headings?

Rakesh
08-18-2010, 11:43 AM
Yes all are Heading having style as "SO_LEV1".

The replacement should be done from "SO_LEV1" Heading to "SO_LEV1" Heading