views:

159

answers:

1

I'm stumped about this thing - when recording a macro where I basically add some fields and go into Design Mode to be able to replace the dummy text of the placeholder. Now, I go out of Design Mode when recording the macro and everything seems to be working ok. But when playing the macro, it just stops after ActiveDocument.ToggleFormsDesign.

What could be causing this? Has anyone else experienced this?

Here's a snippet of a macro:

Selection.Range.ContentControls.Add (wdContentControlText)
ActiveDocument.ToggleFormsDesign
Selection.TypeText Text:="Date"
Selection.MoveLeft Unit:=wdCharacter, Count:=4, Extend:=wdExtend
Selection.Style = ActiveDocument.Styles("TextRed")
ActiveDocument.ToggleFormsDesign
+2  A: 

The reason is because the Selection object becomes lost after ToggleDesignMode - meaning there is no longer a Selection object. In your recorded example, you reselected the place in which to type "Date" but Word doesn't know where to select.

The way to get around this is to use recorded macros as a starting point, but then further clean them up. Like this:

Sub InsertContentControl()
    Dim myDoc As Document
    Set myDoc = ActiveDocument
    Dim tr As Style
    Set tr = myDoc.Styles("TextRed"):
    Dim cc As ContentControl
    Dim sel As Range
    Set sel = Selection.Range
    Set cc = sel.ContentControls.Add(wdContentControlText)
    cc.SetPlaceholderText Text:="Date"
    cc.DefaultTextStyle = tr
End Sub

To do this with a new style, use the following:

Sub InsertContentControlwithNewStyle()
    Dim myDoc As Document
    Set myDoc = ActiveDocument
    Dim tr As Style
    Set tr = myDoc.Styles.Add("New TextRed")
    tr.BaseStyle = wdStyleNormal
    tr.Font.ColorIndex = wdRed
    Dim cc As ContentControl
    Dim sel As Range
    Set sel = Selection.Range
    Set cc = sel.ContentControls.Add(wdContentControlText)
    cc.SetPlaceholderText Text:="Date"
    cc.DefaultTextStyle = tr
End Sub
Otaku
Well, anything after ActiveDocument.ToggleDesignMode seems to be not be running, no matter what code it is. Like it just breaks.BUT, seeing how you provided a way for me to apply apply the placeholder text without going into DesignMode then that's even better! :)Think I'll just mark this as the answer and up it up a bit. And never fiddle with design mode using macros again huh :)
Kenny Bones
@Kenny Bones: Yeah, certain actions can cause the `selection` to be lost. Macro recorder is a good start, but it's just that - a start.
Otaku
Another thing, when applying the style to cc.DefaultTextStyle, I get this error message "Could not apply the style" with an error code of 5849. I know the style exists though.
Kenny Bones
Error 5849 means you're trying to apply a Table Style to something that isn't a Table. So you'll need to edit the style and change it's Style Type to "Paragraph" instead of "Table".
Otaku
Really? Well, the style isn't a table style, it is set to Paragraph actually. And when I go and edit the style, all I can choose between is Paragraph and Linked (paragraph and character)
Kenny Bones
Then the likely cause is that you have more than one document open at the time and Word is not finding "TextRed" as a style in another document, therefore it doesn't exist. You can do one of two things - close down all other documents or set a variable to your specific document. I've updated a bit above.
Otaku
Just checked, no additional open documents. And no additional winword.exe either. Just tried the new code and the same error appear at the line of cc.DefaultTextStyle = tr. Also, I checked the style and made sure it's set to Paragraph. This doesn't happen with other built in styles is seems, such as Heading 1. This is pretty weird. I've also tried to create all new styles, but noen are working. Isn't that strange?
Kenny Bones
Yep, that is strange. I can't replicate your issue. I've done the same code above with a new style and it works for me as well.
Otaku
I don't understand anything. This happens in all new documents as well as if I run the macro in a table or outside of it. What kind of style are you applying when you're trying it out? I'm running Word 2007 btw on Windows 7. Don't know if that makes any difference though.
Kenny Bones
I know I may get in trouble for saying this, but is your "TextRed" style actually spelled correctly, i.e. it's not "Text Red" with a space? Also, give the second code set above a try that I've put above. This adds a new style to your document (you can delete it later if not needed) and uses that for the content control. Does this work?
Otaku
Ok, I think I see what's happening now. I compared own style with the one created by your macro. And the Style type of your style was "Linked (paragraph and character) and applying that style works. However, the style doesn't seem to be applied before you actually type something into the content control. The placeholder text just picks up the style which is set to the place where the cursor is at the moment the macro is run. That's why I wanted to go into Design Mode because that changes the style of the placeholder text as well and new text typed in.
Kenny Bones