Applying style to docVariable
How do I apply a style to a docVariable?
The scenario is that there is an input form from which the user selects various options. Each option is represented by a docVariable in the active document. If the option is selected, the docVariable value is equal to a certain string. If it is not selected, the docVariable value is equal to " ". ("" Gives me Errors unfortunately).
In a few instances, a style needs to be applied if the option is selected. How do I get at it to apply the style?
Re: Applying style to docVariable
document.. are you working with Word? a webpage? ???
Re: Applying style to docVariable
My apologies. I am working in a Word 2003 document.
Re: Applying style to docVariable
I thought maybe if I inserted a bookmark to the docVariable in the Word doc I might be able to get at it that way to set the style. But I get the error: "object variable or with block variable not set"
Code:
Dim StyleRng As Range
StyleRng = ActiveDocument.Bookmarks("BMsSig_1Va").Range 'Error here
StyleRng.Style = "Heading 2"
StyleRng.Words(2).Select
Selection.InsertStyleSeparator
Re: Applying style to docVariable
I just tried this.. it worked fine..
Im not using a "range" but it set the Heading 2 style to the first word
ActiveDocument.Words(1).Select
Selection.Style = ActiveDocument.Styles("Heading 2")
Re: Applying style to docVariable
I ended up doing this, which worked, with issues:
Code:
ActiveDocument.Bookmarks("BMsSig_1Va").Select
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeParagraph
Selection.Style = ActiveDocument.Styles("Heading 3")
Selection.MoveRight Unit:=wdCharacter, Count:=23
Selection.InsertStyleSeparator
1. I had to insert the paragraph in the selection rather than including it as part of the string because otherwise the style was applied to the preceding paragraph where the return was placed as that is where the docVariable bringing in the text which is bookmarked is located. I will probably insert yet another docVariable that will be set to a return if that option is set because if the option is then changed to something else, the return should not be there nor the style applied.
2. The InsertStyleSeparator did not insert a style separator. I'm not sure if this doesn't work when the selection is a docVariable or what the reason would be, but I guess that's another post. For the time being I think I can work around it by splitting the docVariable into, yet again, 2 docVariables and inserting the StyleSeparator between them.
Re: Applying style to docVariable
For some reason the numbering for the style does not apply when the paragraph return is in through Chr(13) or VbCrLf or anything else in a docVariable. I only see the numbering apply when I do
Selection.TypeParagraph
Any ideas?
This then leaves me having to test for the paragraph if the control box that generates the paragraph is unchecked, which seems most inefficient.