Help me with a simple MS macro

I

ivanov.ivaylo

Hi,

I want amend my macro that finds all instances of bold text in a
document and puts the text between <b> and </b>. This macro, however,
formats the tags themselves in bold. I want them in a non-bold (normal)
and blue typeface.

Thanks!

Here is the macro:

Sub BoldToTagged()
'
' BoldToTagged Macro
' Macro created 16.9.2005 by Ivaylo T Ivanov
' Finds all instances of bold text and puts the text between <b> and
</b> tags
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "<b>^&</b>"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.Font.Bold = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
H

Helmut Weber

Hi, like this:

Sub TagBold()
Dim rngDcm As Range
Set rngDcm = ActiveDocument.Range
' make sure search options are at default values
' e.g. search for "resetsearch" I've posted several times here
With rngDcm.Find
.Text = ""
.Format = True
.Font.Bold = True
While .Execute
rngDcm.Font.Bold = False
rngDcm.InsertBefore "<b>"
rngDcm.InsertAfter "</b>"
rngDcm.Collapse Direction:=wdCollapseEnd
Wend
End With
End Sub
 
I

ivanov.ivaylo

Hi Helmut,

Thanks for the help! Your macro works fine but I want the the bold text
to remain bold after the execution of the macro while the <b>/</b> tags
not being bold and be colored in blue.

Example:

Before search: "blablabla jjkk opop" (the whole text is bold)
After the search: <b>blablabla jjkk opop</b> (here only "blablabla jjkk
opop" is bold and the tags themselves <b>/</b> are blue and not bold)
 
H

Helmut Weber

Hi,
Thanks for the help! Your macro works fine but I want the the bold text
to remain bold after the execution of the macro while the <b>/</b> tags
not being bold and be colored in blue.

why?

You'll run into taggings like <b><b><b>boldword</b></b></b>
if executing the macro two or more times.

Anyway, can all be done.
BTW, I thought, I could use "setrange",
instead of defining rngtmp.start and rngtmp.end,
but couldn' get it to work.

Sub TagBold2()
Dim rngDcm As Range
Dim rngTmp As Range
Set rngDcm = ActiveDocument.Range
Set rngTmp = Selection.Range
ResetSearch
With rngDcm.Find
.Text = ""
.Format = True
.Font.Bold = True
While .Execute
rngDcm.InsertBefore "<b>"
rngDcm.InsertAfter "</b>"
rngDcm.Select
rngTmp.Start = rngDcm.Start
rngTmp.End = rngDcm.Start + 3
rngTmp.Font.Color = wdColorBlue
rngTmp.Start = rngDcm.End - 4
rngTmp.End = rngDcm.End
rngTmp.Font.Color = wdColorBlue
rngTmp.Font.Bold = False
rngDcm.Collapse Direction:=wdCollapseEnd
Wend
End With
ResetSearch
End Sub
' ---
Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Helmut Weber

"rngDcm.Select" is for testing only.
You may delete it.
It costs a lot of performace.


--

Helmut Weber, MVP WordVBA

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

ivanov.ivaylo

Hi Helmut,

Thank you again. I'll decide which macro will use.
I'm developing a set of macros in MS Word to facilitate my work to
reformat a text for a specific purpose.
Since I'm a newbie in VBA, I'm developing them one by one. I'll
customize the above code to operate with the following tags too:
<i>/</i>; <u>/</u>.

I'll be inserting the following tags too: <font face="Lucida Sans
Unicode">some text</font>
But in this case the "" seem to be too much in the expression. For the
time being, I want to assign this macro to a button. Maybe at a later
stage, I'll make the macro to search for a certain formatting. At this
stage, I'm trying to place the selected text between the tag <font
face="Lucida Sans Unicode"> and the tag "</font>". See my macro:

Sub myLucida()
With Selection
..InsertBefore ("<font face="Lucida Sans Unicode">")
..InsertAfter ("</font>")
End With
End Sub
===============

Helmut, if you are willing to send you a sample from the text I want to
reformat and the result I want to achieve, I'll be very thankful for
your help. There are a few rules to be followed and a set of macros
will do the work for some hundred pages. An expert will do it with much
more ease.

Please, contact me at (e-mail address removed).

If not interested, thanks anyway.

Regards,

Ivo
 
G

Greg

I am not Helmut. Would you be willing to attach a few dollars along
with that text to compensate others for doing your work?

By all means, send both.
 
H

Helmut Weber

Hi Ivo,
I'll be inserting the following tags too: <font face="Lucida Sans
Unicode">some text</font>
But in this case the "" seem to be too much in the expression. For the
time being, I want to assign this macro to a button. Maybe at a later
stage, I'll make the macro to search for a certain formatting. At this
stage, I'm trying to place the selected text between the tag <font
face="Lucida Sans Unicode"> and the tag "</font>". See my macro:
Sub myLucida()
With Selection
.InsertBefore ("<font face="Lucida Sans Unicode">")
.InsertAfter ("</font>")
End With
End Sub

I'd prefer further discussion in the groups.
And with that, you can access the knowledge of all (!)
of the community.

As to "insertbefore":

That would be:
rngDcm.InsertBefore "<font face=" & Chr(34) & "Lucida Sans Unicode" &
Chr(34) & ">"

Insertafter: "</font>"

Beware of linebreaks by the newsreader.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Helmut Weber

Yes, Greg,

I have all that tagging ready at hand.
Tested with more than have a million docs,
over more than ten years.

Unfortunately, without coloring tags...

One cold think of a surcharge depending on the color.

Blue velvet would be extremely expensive.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

ivanov.ivaylo

Hello Helmut,

I need further help with a macro. I reached the place where I have to
do this search and replace action:

Find: (selected phrase, e.g. "green piano")
Replace: <a href="bword://green piano">green piano</a>

That is to say, I'll select sertain phrases from the text and when I
run the macro the selected phrase should be tagged like this: <a
href="bword://XYZ">XYZ</a>

XYZ is any phrase I choose to select. I'll assign the macro to a button
for quick work.

Ivaylo
 

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