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 / August 2007

Tip: Looking for answers? Try searching our database.

Macro Find and Replace with styles

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chris - 22 Aug 2007 09:32 GMT
Hi,

i have a macro which formats the text between Html-Tags like <U1>Text</U1>
with standard header styles. The problem is that my whole document will be
replaced with this format style. But i only want to format the text between
the HTML-Tags.
Her is my snippet:

With ActiveDocument.Range.Find
     .ClearFormatting
     .Replacement.ClearFormatting
     .Format = True
     .MatchWildcards = True
     .Text = "(\<U1\>)(*)(\</U1\>)"
     .Replacement.Text = "\2"
     .Replacement.Style =
ActiveDocument.Styles(WdBuiltinStyle.wdStyleHeading1)
     .Execute Replace:=wdReplaceAll
End With

Many thanks for any advice...

Chris
Edward Thrashcort - 22 Aug 2007 09:53 GMT
I remember having a similar problem which I had to kludge my way around.
The trouble is that the (*) part of the search text can be ANYTHING
including the whole document!

Something like this

- find first tag
Do while found
 - delete selection
 - get the range end position of the cursor "a"
 - find closing tag
 - get the range start "b"
 - delete the selection
 - select the range between (a+1) abd (b-1)
 - reformat
 - find first tag
loop

Eddie

> *From:* Chris<Chris@discussions.microsoft.com>
> *Date:* Wed, 22 Aug 2007 01:32:02 -0700
[quoted text clipped - 22 lines]
>
> Chris
Chris - 22 Aug 2007 10:14 GMT
Hi Edward,

thanks for your fast reply.

Can u give me a code example for this workaround. Im very new in vba macros,
so it would be very helpful....

Thanks in advance.

Chris

> I remember having a similar problem which I had to kludge my way around.
> The trouble is that the (*) part of the search text can be ANYTHING
[quoted text clipped - 42 lines]
> >
> > Chris
Helmut Weber - 22 Aug 2007 16:27 GMT
Hi Chris,

to me, the basic mistake is to apply a paragraph
style instead of a character style.
Or use direct formatting.

By the way, sure, there are more people here
(that is why I'm here as well)
with excellent knowledge than in the German groups,
according to the number of speakers of english,
some equal to but none better than Christian Freßdorf MVP.

Schönen Tag noch.
(Have a nice day)

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Edward Thrashcort - 22 Aug 2007 16:59 GMT
> Hi Edward,

"EDDIE" WILL BE FINE THANKS ;-)

Sub ReformatBetweenTags()
Dim a, b As Long
Dim oRange As Range

Selection.HomeKey Unit:=wdStory

With Selection.Find
   .ClearFormatting
   .Text = "<U1>"
   .Replacement.Text = ""
   .Forward = True
   .Wrap = wdFindStop
   .Format = False
   .MatchCase = False
   .MatchWholeWord = False
   .MatchWildcards = False
   .MatchSoundsLike = False
   .MatchAllWordForms = False

   While .Execute
       Selection.Delete
       a = Selection.Range.End
       .Text = "</U1>"
       If .Execute Then
           Selection.Delete
           b = Selection.Range.Start
           Set oRange = ActiveDocument.Range(a, b)
           oRange.Style = ActiveDocument.Styles("Heading 1")
       End If
       .Text = "<U1>"
   Wend
End With

Set oRange = Nothing
End Sub

Eddie

> *From:* Chris<Chris@discussions.microsoft.com>
> *Date:* Wed, 22 Aug 2007 02:14:01 -0700
[quoted text clipped - 57 lines]
> > >
> > > Chris
Klaus Linke - 27 Aug 2007 12:45 GMT
Hi Chris,

A safer wildcard replacement would be  .Text = "(\<U1\>)([!^13]@)(\</U1\>)".
Though if your replacement formats the whole document, either there's an end
tag missing, or you started the search in the middle of the document, or you
don't really have paragraphs (... think you replaced them with <br/> tags in
the German groups?).

Just BTW, for the other direction I replace ([!^13]@)(^13) with
<U1>\1</U1>\2
I often use that kind of tagging to clean up docs:
-- Mark up things I want to keep (styles, italic, bold...),
-- then turn the rest into pure text (Ctrl+A, Ctrl+N, Ctrl+Q,
Ctrl+Spacebar),
-- then reapply the styles and formatting.

Klaus

> Hi,
>
[quoted text clipped - 20 lines]
>
> Chris
 
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.