View Full Version : Selecting multiple style in single paragraph
vikas
05-15-2015, 06:30 AM
Hi All,
Is there a way in Word to find multiple style in a paragraph (styles is marked with "linked paragraph and character style", so now we can set multiple styles in single paragraph).
code is below..
My Code:
Public Sub product()
Resetsearch //this is function see below
Selection.HomeKey wdStory, wdMove
With Selection.Find
.Text = ""
.Replacement.Text = "<person-group person-group-type=""author""><name><surname></surname><given-names></given-names>^&</name></person-group>"
.Style = ActiveDocument.Styles("Ref_AuthorName")
'.Font.Italic = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
'author_2
Resetsearch
Selection.HomeKey wdStory, wdMove
With Selection.Find
.Text = ""
.Replacement.Text = "<name><surname></surname><given-names></given-names>^&</name>"
.Style = ActiveDocument.Styles("Ref_AuthorName_2")
'.Font.Italic = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
End sub
Public Sub Resetsearch()
Selection.HomeKey Unit:=wdStory
With Selection.Find
.Parent.Collapse
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End Sub
Thanks in advanced.
gmayor
05-15-2015, 09:51 PM
The short answer is 'no', but if you were to provide a sample document and explain EXACTLY what you are trying to do there may be an alternative approach that could be used.
vikas
05-18-2015, 08:21 AM
Hi,
I have one paragraph see below
"Martin is good boy"
In above line i have applied some styles to text "Martin" i.e "Prod_Name" style and for another "is good boy" applied "Prod_Comment" style.
Now i want output like this see below:
<product><String-name>Martin</String-name><comment>is good boy</Comment></product>
My code:
Public Sub Resetsearch()
Selection.HomeKey Unit:=wdStory
With Selection.Find
.Parent.Collapse
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End Sub
Sub Product()
xmlStructure = xmlStructure & "<Product>" & Chr(13)
Resetsearch
Selection.HomeKey wdStory, wdMove
With Selection.Find
.Text = ""
.Replacement.Text = "<String-Name>^&</String-Name>"
.Style = ActiveDocument.Styles("Prod_Name")
'.Font.Italic = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
Resetsearch
Selection.HomeKey wdStory, wdMove
With Selection.Find
.Text = ""
.Replacement.Text = "<Comment>^&</Comment>"
.Style = ActiveDocument.Styles("Prod_Comment")
'.Font.Italic = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
xmlStructure = xmlStructure & "</Product>" & Chr(13)
End Sub
gmayor
05-18-2015, 09:38 PM
Assuming the styles are character styles, then
Sub TestMacro()
FormatProd "Martin"
lbl_Exit:
Exit Sub
End Sub
Sub FormatProd(strProd As String)
Dim oRng As Range
Dim oFound As Range
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(FindText:=strProd)
oRng.Text = "<product><String-name>" & oRng.Text & "</String-name>"
oRng.Style = "Prod_Name"
oRng.Collapse 0
Set oFound = oRng
oFound.End = oFound.Paragraphs(1).Range.End - 1
oFound.Text = "<comment>" & oFound.Text & "</Comment></product>"
oFound.Style = "Prod_Comment"
oRng.Collapse 0
Loop
End With
lbl_Exit:
Exit Sub
End Sub
gmaxey
05-19-2015, 05:22 AM
Here is another approach which may be more universally adaptable to your process. As with Graham's code the key the character style applied.
Sub ScratchMacro()
Dim oRng As Word.Range
'A basic Word macro coded by Greg Maxey
TagParts "Prod_Name", "Prod_Comment"
Set oRng = Selection.Paragraphs(1).Range
oRng.Text = "<product>" & oRng.Text & "/product>"
lbl_Exit:
Exit Sub
End Sub
Sub TagParts(ParamArray arrStyles())
Dim oRng As Range
Dim strTag As String
Dim lngIndex As Long
For lngIndex = 0 To UBound(arrStyles)
Set oRng = Selection.Paragraphs(1).Range
strTag = Split(arrStyles(lngIndex), "_")(1)
With oRng.Find
.Style = arrStyles(lngIndex)
While .Execute
oRng.Text = "<" & strTag & ">" & oRng.Text & "</" & strTag & ">"
oRng.Collapse wdCollapseEnd
Wend
End With
Next lngIndex
lbl_Exit:
Exit Sub
End Sub
vikas
05-19-2015, 05:59 AM
Its not working. i think u didn't get it what i want to say.
i want a text on style basis.
e.g:
Fransois Dosse Paul Ricoeur. Les sens some ,Paris 2008
i apply style to text:
Fransois Dosse =string_name style
Paul Ricoeur = source style
Les sens = comment style
Paris = location style
2008= year style
Output:
<string-name>Fransois Dosse</string-name><source>Paul Ricoeur</source>.<comment>Les sens</comment>some ,<location>Paris</location><year>2008</year>.
macro picked a text on the style bases and put it in the respective tags. Above styles are user defined styles.
Thanks in Advance
gmaxey
05-19-2015, 05:24 PM
I'm sorry but it is working. Here is a version tailored to your last example. It the character styles "string_name, source, comment, location and year" are applied to the text (not the leading/trailing spaces, punctuation or paragraph mark the code produces the output you defined.
Sub ScratchMacro()
Dim oRng As Word.Range
'A basic Word macro coded by Greg Maxey
TagParts "string_name", "source", "comment", "location", "year"
Set oRng = Selection.Paragraphs(1).Range
lbl_Exit:
Exit Sub
End Sub
Sub TagParts(ParamArray arrStyles())
Dim oRng As Range
Dim strTag As String
Dim lngIndex As Long
For lngIndex = 0 To UBound(arrStyles)
Set oRng = Selection.Paragraphs(1).Range
If InStr(arrStyles(lngIndex), "_") > 0 Then
strTag = Split(arrStyles(lngIndex), "_")(1)
Else
strTag = arrStyles(lngIndex)
End If
With oRng.Find
.Style = arrStyles(lngIndex)
While .Execute
oRng.Text = "<" & strTag & ">" & oRng.Text & "</" & strTag & ">"
oRng.Collapse wdCollapseEnd
Wend
End With
Next lngIndex
lbl_Exit:
Exit Sub
End Sub
vikas
07-29-2015, 12:28 AM
Hi Gmaxey,
Thanks for reply.
But your code is not working. is there any another way to replace words with tags on style basis?
Thanks in advance!
Vikas
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.