I need to import from Excel some tabs in a Word document, to do this I used VBA in Excel.
something as this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| With SelDOCFile.Find
.Forward = True
.Wrap = wdFindContinue
.Execute FindText:="\<FLAG\>"
If .Found Then
For Bank = 0 To Banks - 1
SelDOCFile.InsertCaption Label:="Table", Title:=": Bank " & Bank _
& " [Special" & Bank & "##A]", Position:=wdCaptionPositionAbove, ExcludeLabel:=0
SelDOCFile.HomeKey Unit:=wdLine
SelDOCFile.EndKey Unit:=wdLine, Extend:=wdExtend
SelDOCFile.Font.Size = 10
SelDOCFile.Font.Bold = True
SelDOCFile.Font.Italic = False
SelDOCFile.Font.Name = "Arial"
SelDOCFile.HomeKey Unit:=wdLine
SelDOCFile.MoveRight Unit:=wdWord, Count:=6
SelDOCFile.EndKey Unit:=wdLine, Extend:=wdExtend
SelDOCFile.Style = ActiveDocument.Styles("SpecialFunc")
....
Next Bank
End If
End With |
What I want is to write Title:
="Table 1: Bank 0 [Special0##A]" with a different style for text between [], but here code doesn't work as expected. I get a difference, but the style is not fully applied, only the color, the size ... changes but when in Word I place cursor on the text, it appears in the same style as the rest of the title
Reponse:
- Captions always use the Caption style, which applies to the entire paragraph. If you want captions to be 10pt Arial, edit the Caption style. – Timothy Rylatt yesterday
- As you are only applying the style SpecialFunc to selected characters it cannot apply paragraph formatting, only Font formatting such as color and size. – Timothy Rylatt yesterday
- So I can't do it when I create caption but after I won't have problem? – MarcDS yesterday
- I've just checked this in O365 and there appears to be a bug when applying a linked style to selected characters via VBA. If you apply a character style it fully applies (i.e. the underlying xml shows the style has been applied), but a linked style only applies its formatting and acts like direct formatting. This applies to any selected text not just a caption. – Timothy Rylatt yesterday
- By adding Char it works!
SelDOCFile.Style = ActiveDocument.Styles("SpecialFunc Char") – MarcDS 2 hours ago
- There is, however, a bug as the built-in linked styles should apply correctly when using their style enum, e.g.
Selection.style = ActiveDocument.Styles(wdStyleHeading1). Custom linked styles should also apply correctly without using the name of the hidden character style. Glad you found a work around though. – Timothy Rylatt 2 hours ago
Partager