2
votes

I have a macro code that basically creates a few tables, then types in some text in some of then, and then places a few text placeholders. The problem appear when I try to apply styles to the text. At first I thought it was only the placeholders that aren't affected by the code. But it seems as though regular text, selected by the macro isn't applied as well.

The code basically looks like this:

Selection.TypeText Text:="Entreprisecost:"
Selection.MoveRight Unit:=wdCell
Set cc = Selection.Range.ContentControls.Add(wdContentControlText)
cc.SetPlaceholderText Text:="Description of the cost"
cc.DefaultTextStyle = "EnterpriseStyle"
Selection.Style = ActiveDocument.Styles("EnterpriseStyle")

Notice how I define the style on both the placeholder AND the selection.

Next, I tried to record a simple macro where I select the entire row, then apply the style to the selection. This works when I'm recording. But it doesn't work when I run the macro. It's strange:

Selection.MoveUp Unit:=wdLine, Count:=5, Extend:=wdExtend
Selection.Style = ActiveDocument.Styles("ExperienceStyle")

Why is this happening? My macro security settings are set to default medium, but I choose of course to enable macros once the template is opened. This happens when I both open the template itself and when i doubleclick it to create a new document based on the template. Any ideas?

Edit: Every bit of the macro works, besides applying styles. The code that applies the style is run, the the text doesn't change. And when I select the text to check which style its in, I can see that the style is applied. But it isn't at the same time. Strange, if I select the text, then manually reapply the style, meaning, clicking on the same style that's already selected, THEN I see that the style is really applied.

It's like the style is being set without it actually being applied.

2
in reading above, i'm not sure what you want to happen - regardless of any code you currently have. maybe you could edit and explain your desired state and then maybe a solution could be applied for that.Meihua
Well, I want the style to be applied to the selection. With the code above, the style isn't applied and I find that strange.Kenny Bones

2 Answers

2
votes

First, you have to help us out with the code and set up. I assume you have at least a six by two table with the selection in cell 6,1 (bottom, left cell). Second, don't make us guess what the variables are; use Dim statements. Third, we don't have your styles, so I changed them to standard Normal.dot ones.

With that said, your code works fine as below. The only wrong thing I can see is that you used ExperienceStyle in the last part and EnterpriseStyle in the first part. You would get an error if either one did not exist.

Public Sub Test()
    Selection.TypeText Text:="Entreprisecost:"
    Selection.MoveRight Unit:=wdCell

    Dim cc As ContentControl
    Set cc = Selection.Range.ContentControls.Add(wdContentControlText)
    cc.SetPlaceholderText Text:="Description of the cost"
    cc.DefaultTextStyle = "Title"
    Selection.Style = ActiveDocument.Styles("Title")
    Selection.Style = ActiveDocument.Styles("Strong") 'Proof the style is being changed.

    Selection.MoveUp Unit:=wdLine, Count:=5, Extend:=wdExtend
    Selection.Style = ActiveDocument.Styles("Strong")
End Sub
1
votes

I think your proble is that you are not clearing format before applying new Style. Try to do that and tell us if it works