PDA

View Full Version : [SOLVED:] Add tags to Bold text only at start of selected paragraphs



JPG
03-16-2020, 05:46 AM
I want to make a macro that will be selective in adding some <link> tags fore and aft of text only at the start of paragraphs and limited to a certain font size and color. A copy of the text is placed at the head.... easier to show as below. Word has a problem with spaces it seems.


Some sort of heading in bold with spaces followed by some more text in a plain font, maybe a different size but not necessarily.


I want to be able to select a range of paragraphs and tag just the bold text and place a copy before the found text.

<link>Some sort of heading in bold with spaces</link>Some sort of heading in bold with spacesfollowed by some more text in a plain font, maybe a different size but not necessarily

gmayor
03-16-2020, 07:22 AM
Simple enough

Sub Macro1()
Dim orng As Range
Set orng = ActiveDocument.Range
With orng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Font.Bold = True
Do While .Execute()
If orng.Start = orng.Paragraphs(1).Range.Start Then
orng.InsertBefore "<link>"
orng.InsertAfter "</link>"
End If
orng.Collapse 0
Loop
End With
lbl_Exit:
Set orng = Nothing
Exit Sub
End Sub

JPG
03-16-2020, 07:54 AM
Thanks but not what I stated, but I really appreciate the help.
1. Selected paragraphs only
2. Copy of bold text with tags is to be placed in front, so
Some sort of heading in bold with spaces ​followed by some more text in a plain font, maybe a different size but not necessarily.
to become

<link>Some sort of heading in bold with spaces</link>Some sort of heading in bold with spaces ​followed by some more text in a plain font, maybe a different size but not necessarily.

JPG
03-16-2020, 08:44 AM
This appears to do what I need.
I wanted also to limit by font size and color.
Thanks for pointing me in the right direction.




InSelection = False
If selection.Type = wdSelectionIP Then InSelection = True
If InSelection = True Then
MsgBox ("select some text")
Exit Sub
End If
Application.ScreenUpdating = False
Dim RngFnd As Range, StrTxt As String
With selection
Set RngFnd = .Range
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.text = ""
.Replacement.text = ""
.Forward = True
.Format = False
.Wrap = wdFindStop
.MatchWildcards = True
.Font.Bold = True
.Font.ColorIndex = wdAuto
.Font.Size = 10
.Execute
End With
Do While .Find.Found
If .InRange(RngFnd) Then
If .Paragraphs.Count > 1 Then .Start = .Paragraphs(1).Range.End
If .Start = .Paragraphs(1).Range.Start Then
StrTxt = .text
.InsertBefore "<link>" & StrTxt & "</link>"
.Start = .End - Len(StrTxt)
End If
End If
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
End With
RngFnd.Select



Finally I think I worked it out. Will add some comboboxes for setting font and color to parse and should be good to go.

Thanks for your examples, and time.