views:

111

answers:

1

Hi, this is so weird. 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  A: 

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
ForEachLoop
Yes, the styles do exist. And everything works fine when I'm recording the macro. I can actually click Record Macro and then type some text in, then select the text and then apply the style. And when checking the macro I can see the code. But when I go back and run the recorded macro, I can see that the text is written, selection is made, but the style isn't applied. The code that applied the style is run, but the style isn't really applied. Strangely, doing this in a new document seems to work fine. Even with non-standard fonts. Perhaps I need to remake the template then?
Kenny Bones