Loop Help

D

Debra Farnham

Hi again everyone

Win 2K
Word 2K

I have the following not so elegant code to italicize text, between hyphens,
in a custom document property named Offence.

Dim SearchString, SearchChar, MyFirstHyphen, MySecondHyphen, Phrase

SearchChar = "-" ' Search for "-".

SearchString = ActiveDocument.CustomDocumentProperties("Offence") ' String
to search in.

'find first hyphen
MyFirstHyphen = InStr(4, SearchString, SearchChar, 1) + 1

'find second hyphen
MySecondHyphen = InStr(MyFirstHyphen, SearchString, SearchChar, 1) -
MyFirstHyphen - 1

Phrase = Mid(SearchString, MyFirstHyphen, MySecondHyphen)

'Italicize Text


Selection.Find.Replacement.Font.Italic = True
With Selection.Find
.Text = Phrase
.Replacement.Text = Phrase
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute
With Selection
If .Find.Forward = True Then
.Collapse direction:=wdCollapseStart
Else
.Collapse direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward = True Then
.Collapse direction:=wdCollapseEnd
Else
.Collapse direction:=wdCollapseStart
End If
.Find.Execute
End With

Selection.GoTo what:=wdGoToBookmark, name:="Start"

The problem is, I need to loop it through the custom property so if there
are further pairs of hyphens in the custom property, that it italicizes them
as well.

Any suggestions on cleaning up the italicization would be greatly
appreciated as well.

I don't know why, but I cannot wrap my head around looping.

TIA

Debra
 
J

Jay Freedman

Hi Debra,

Where to begin?? ... Let's just throw out the existing code and start
over from a statement of the problem.

You have a series of phrases, separated by hyphens, stored in the
custom property. You want to search the document for each occurrence
of each phrase, and change them to italic.

There's a function in VBA, called Split(), that's specifically made
for breaking down a string into separate substrings, given the
separator character. The results go into a Variant variable as a sort
of array. You can look up this function in the Help.

Then you can use the For Each ... Next loop construction to step
through all the substrings in the Variant array. Each substring gets
assigned to the .Text parameter of the .Find, and then you do a
ReplaceAll to change that substring to italic wherever it occurs. The
..Replacement.Text parameter is set to the code "^&" which means "the
same text as in .Text".

So the complete macro is this:

Public Sub ItalicOffences()
Dim myArray As Variant
Dim substr As Variant
Dim myRg As Range

myArray = Split(ActiveDocument. _
CustomDocumentProperties("Offence"), "-")

For Each substr In myArray
If Len(substr) > 0 Then
Set myRg = ActiveDocument.Range
With myRg.Find
.ClearFormatting
.Text = substr
.Replacement.Text = "^&"
.Replacement.Font.Italic = True
.Format = True
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With
End If
Next substr
End Sub

Note: It appears from your code that there's something in the first
three or four characters of the custom property that you aren't using
as a search phrase. The Split function can't deal with that, so you
should remove those characters and deal with them some other way.
 
G

Greg Maxey

Debra,

It appears that all you are doing with your introductory string construction
is to try to find anything beteen the first pair of hypens. No matter what
you do with the find and replace part the search string is still the text
between the first pair of hyphens.

Try this:

Sub ScratchMacro()

Dim oFld As Field
Dim myRange As Range

For Each oFld In ActiveDocument.Fields

If oFld.Type = wdFieldDocProperty Then
oFld.Select
Set myRange = Selection.Range

myRange.Find.Replacement.Font.Italic = True

With myRange.Find
.Text = "-*-"
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
.Format = True
.Execute Replace:=wdReplaceAll
End With
End If
Next
Selection.GoTo what:=wdGoToBookmark, Name:="Start"
End Sub
 
G

Greg Maxey

Jay,

Maybe I misunderstand Debra. I thought she was trying to italicize text
bounded by two hyphens:

Now -is the time- for all good men -to come- to the aid of their country.

Italicize: "is the time" and "to come"
 
D

Debra Farnham

Hi Greg

It seems you did not misunderstand me at all.

Your solution works like a charm.

Thank you so much for your help. It is HUGELY appreciated!

Debra
 
D

Debra Farnham

Hi again Greg

Your solution works fine, but it seems to be taking quite a long time to
loop.

I have several custom properties in this document.

Is there any way to have it JUST loop through the property named Offence?

Thanks again

Debra
 
G

Greg Maxey

Debra,

Is your string -sometext- unique to the Offence docproperty? If so you
could speed things up by simply search the whole document for the string:

Sub ScratchMacro2()

Dim myRange As Range
Set myRange = ActiveDocument.Content

myRange.Find.Replacement.Font.Italic = True
With myRange.Find
.Text = "-*-"
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
.Format = True
.Execute Replace:=wdReplaceAll
End With
Selection.GoTo what:=wdGoToBookmark, Name:="Start"
End Sub


If you have scads of DocProperty fields you could isolate the Offence fields
with the following. Still I think your speed issue is related to having to
evaluate every field.


Sub ScratchMacro()

Dim oFld As Field
Dim myRange As Range

For Each oFld In ActiveDocument.Fields

If oFld.Type = wdFieldDocProperty And InStr(oFld.Code.Text, "Offence") <> 0
Then
oFld.Select
Set myRange = Selection.Range

myRange.Find.Replacement.Font.Italic = True

With myRange.Find
.Text = "-*-"
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
.Format = True
.Execute Replace:=wdReplaceAll
End With
End If
Next
Selection.GoTo what:=wdGoToBookmark, Name:="Start"
End Sub
 
D

Debra Farnham

Hi Greg

Yes the string is unique to the Offence docproperty. Hyphens could be found
elsewhere in the document but I only need the next that appears between the
hyphens in the Offence field italicized.

Looks to me like your addition to the code witll work. Just about to test
it.

Thanks tons for all your help!

Debra
 
D

Debra Farnham

Just re-read my last post (it appears I can't spell)

In any event, your resolution has now been tested and works beautifully.

Thanks again

Debra
 
J

Jay Freedman

Hi Greg,

I got the completely opposite impression, mainly from the code that set
SearchString to the value of the document property and then used InStr to
find the position of the hyphen in that string.

I guess Debra was even more confused than I thought. Guess I'd better take
my mind-reading hat into the shop for an adjustment. :)
 

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