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 / March 2006

Tip: Looking for answers? Try searching our database.

Deleting between tags

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
julian_m - 18 Mar 2006 01:38 GMT
I want to delete whatever text between certain "tags" (note that I need
to remove html css comments as well as PHP comments)

so I have the following code

Sub EliminarCSSComments()

Dim oRng As Range
Set oRng = ActiveDocument.Content
With oRng.Find
  .ClearFormatting
  .Text = "/#*#/"
 .Forward = True
 .Wrap = wdFindStop 'para cuando se llega al final del documento
 .MatchWildcards = True
 Do While .Execute
   oRng.Select
   MsgBox "encontro"
   oRng.Delete
 Loop
End With

this works quite well if I wanted to delete text between /# and #/

see for instance this example

if I have
/#  this should be removed   #/
/#  this should /be re#moved   #/

both lines will be deleted.

but in the other hand, when I want to delete the text between /* and */
(wich is actually what I need), the code won't work as expected

for instance, if I have

/* this should be removed    */
/* this sho/uld be r*emoved   */

the result is

uld be r*emoved   */

note that I tryied with the following text to search

   .Text = "/" + Chr(42) + "*" + Chr(42) + "/"
   '.Text = "/* */"
   '.Text = "/*/"
   '.Text = "/\*?{1,}\*/"
   '.Text = "/**/"
   '.Text = "</*>*<*/>"
   '.Text = "/\*?{1,}\*/"

the problem seems to be that i need to search for asterisks(*), which
are seen by Word as wildcards...

any hint?

sdos - jm
Tony Jollans - 18 Mar 2006 02:08 GMT
Hi Julian,

You are correct in your assumption about the asterisks. To find an actual
asterisk you must 'escape' it by preceding it with a backslash - so looking
for "/\*" (with wildcards) finds "/*" rather than "/anything"

--
Enjoy,
Tony

> I want to delete whatever text between certain "tags" (note that I need
> to remove html css comments as well as PHP comments)
[quoted text clipped - 56 lines]
>
> sdos - jm
Jay Freedman - 18 Mar 2006 02:32 GMT
Because an asterisk has a special meaning in search expressions, each
'real' asterisk must be preceded by a backslash. The search expression
should be

  .Text = "/\*(*)\*/"

The first and third asterisks will match actual asterisk characters,
and the second asterisk will match everything between them. It doesn't
seem to me that the parentheses should be necessary if you aren't
using the \1 group in the replacement, but the search doesn't find
anything if they're omitted.

By the way, if you can do without the message box, you can speed up
you macro by rewriting it this way:

Sub EliminarCSSComments()
Dim oRng As Range
Set oRng = ActiveDocument.Content
With oRng.Find
  .ClearFormatting
  .Text = "/#*#/"
 .Forward = True
 .Wrap = wdFindContinue
 .MatchWildcards = True
 .Execute Replace:=wdReplaceAll
End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

>I want to delete whatever text between certain "tags" (note that I need
>to remove html css comments as well as PHP comments)
[quoted text clipped - 56 lines]
>
>sdos - jm
julian_m - 18 Mar 2006 02:42 GMT
> Because an asterisk has a special meaning in search expressions, each
> 'real' asterisk must be preceded by a backslash. The search expression
> should be
>
>    .Text = "/\*(*)\*/"

It works like a charm, even though I ask myself what means the
parentheses there...
I now understand how to search for special characters.

> By the way, if you can do without the message box, you can speed up
> you macro by rewriting it this way:

Right. I was using the  message box just to know what was happening
with the code ; )

Thank you very much.

saludos - jm
Graham Mayor - 18 Mar 2006 07:52 GMT
http://www.gmayor.com/replace_using_wildcards.htm  will help with wildcard
syntax.

Signature

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

>> Because an asterisk has a special meaning in search expressions, each
>> 'real' asterisk must be preceded by a backslash. The search
[quoted text clipped - 15 lines]
>
> saludos - jm
 
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.