Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2005

Tip: Looking for answers? Try searching our database.

Loop Help

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Debra Farnham - 23 Jan 2005 18:04 GMT
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
Jay Freedman - 23 Jan 2005 20:06 GMT
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.

--
Regards,
Jay Freedman
Microsoft Word MVP         FAQ: http://word.mvps.org

>Hi again everyone
>
[quoted text clipped - 60 lines]
>
>Debra
Greg Maxey - 23 Jan 2005 20:33 GMT
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"

Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support

> Hi Debra,
>
[quoted text clipped - 115 lines]
>>
>> Debra
Jay Freedman - 24 Jan 2005 14:50 GMT
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. :-)

Signature

Regards,
Jay Freedman
Microsoft Word MVP          FAQ: http://word.mvps.org

> Jay,
>
[quoted text clipped - 125 lines]
>>>
>>> Debra
Greg Maxey - 23 Jan 2005 20:26 GMT
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

Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support

> Hi again everyone
>
[quoted text clipped - 60 lines]
>
> Debra
Debra Farnham - 23 Jan 2005 20:47 GMT
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

> Debra,
>
[quoted text clipped - 96 lines]
> >
> > Debra
Debra Farnham - 23 Jan 2005 20:58 GMT
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

> Debra,
>
[quoted text clipped - 96 lines]
> >
> > Debra
Greg Maxey - 23 Jan 2005 21:35 GMT
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

Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support

> Hi again Greg
>
[quoted text clipped - 114 lines]
>>>
>>> Debra
Debra Farnham - 23 Jan 2005 22:21 GMT
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

> Debra,
>
[quoted text clipped - 169 lines]
> >>>
> >>> Debra
Debra Farnham - 23 Jan 2005 22:25 GMT
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

> Debra,
>
[quoted text clipped - 169 lines]
> >>>
> >>> Debra
Greg Maxey - 23 Jan 2005 23:17 GMT
Glad I could help.

Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support

> Just re-read my last post (it appears I can't spell)
>
[quoted text clipped - 184 lines]
>>>>>
>>>>> Debra
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.