简体   繁体   中英

Word VBA: how to split paragraph into two styles?

I made a program that goes through the document and if there is a paragraph with tab in it, it splits it in two:

sSPlit = Split(aPara.Range.Text, vbTab)
aPara.Range.Text = sSPlit(0) & vbCrLf & sSPlit(1)

That works great. The problem is, I'd like the first splitted paragraph to have "Style1" and the second "Style2".

aPara.Style = "Style1"

adds this style to the next, yet unsplitted paragraph. Please help.

One possible solution is to calculate the ranges to apply the style to and then simply retrieve the range of calculated characters, eg:

Dim par1Start As Integer
Dim par2Start As Integer

par1Start = aPara.Range.Start
par2Start = par1Start + Len(sSplit(0)) + 1

aPara.Range.Text = sSplit(0) & vbCrLf & sSplit(1)

ActiveDocument.Range(par1Start, par2Start).Style = "Style1"
ActiveDocument.Range(par2Start, par2Start + Len(sSplit(1))).Style = "Style2"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM