I have this code in ThisDocument in a word .docm:
Private Sub selConcept_Click()
'enable the content field, delete the other two fields,
'and delete the buttons including itself
ActiveDocument.ContentControls(3).LockContents = False
ActiveDocument.ContentControls(4).Delete
ActiveDocument.ContentControls(4).Delete
ActiveDocument.InlineShapes(3).Delete
ActiveDocument.InlineShapes(3).Delete
ActiveDocument.InlineShapes(3).Delete
End Sub
Private Sub selTask_Click()
ActiveDocument.ContentControls(4).LockContents = False
ActiveDocument.ContentControls(3).Delete
ActiveDocument.ContentControls(4).Delete
ActiveDocument.InlineShapes(3).Delete
ActiveDocument.InlineShapes(3).Delete
ActiveDocument.InlineShapes(3).Delete
End Sub
Private Sub selRef_Click()
ActiveDocument.ContentControls(5).LockContents = False
ActiveDocument.ContentControls(3).Delete
ActiveDocument.ContentControls(3).Delete
ActiveDocument.InlineShapes(3).Delete
ActiveDocument.InlineShapes(3).Delete
ActiveDocument.InlineShapes(3).Delete
End Sub
Private Sub formatSaveB_Click()
With Dialogs(wdDialogFileSaveAs)
.Format = wdFormatFilteredHTML
.Show
End With
End Sub
selConcept, selTask
, and selRef
are command buttons, and there are three rich text content controls on the page also. Clicking one of those three command buttons deletes itself and the two other buttons, and deletes two of the rich text controls.
Put another way: clicking any of the sel
buttons leaves you with one rich text control, and zero sel
buttons.
formatSaveB
is another command button at the end, and the code associated with it opens a dialog to save as an .htm document.
Everything works if a user goes through the process and uses the formatSaveB
button to save; it gets saved as an HTM file.
I want users to be able to save Word drafts however, if possible, but if I've clicked one of the three sel
buttons, then I save the doc as a renamed .docm file, like draft.docm
, then when I open it again, the formatSaveB
button does nothing. It seems to be completely disabled after the save as .docm. Even if I take everything else out of the draft.docm ThisDocument page, it still doesn't work.
I notice that if I just save as draft.docm
and don't close it, the formatSaveB
button still works.
When I try debugging I see "Runtime error 430: Word vba class does not support automation"
UPDATE:
Thanks Peter, actually I think the right ones are being targeted, it's tricky because when it deletes (3), then the next one becomes (3), as you say. I think I've got that worked out though, in any case the correct fields get deleted when I click. The Format button that stops working doesn't get deleted, it simply stops working, and it's being selected by name. It's not selecting or deleting any other controls, it's just supposed to save as .htm (which it does, until I save the .docm as a draft.)
On the other other hand I do find selecting by index a pain, but I haven't found a way to select ContentControls by name, can you advise about that? I can select the buttons by name, but the same method doesn't seem to work on the ContentControls.
For example I've got a Rich Text content control, created using Design Mode in Word, and if I right click on it, I can give it a name ( Concept
) and tag (ConceptTag
). Named and tagged that way, neither of these work:
ActiveDocument.ConceptControls("Concept").Delete
ActiveDocument.ConceptControls("Concept").Select
Selection.Delete
Any advice on how to select and delete those by name? I tried a suggestion to select by tag, and it didn't work either.
ContentControls
orInlineShapes
is that you'll always get the 3rd and 4th control/shape -- but there's no guarantee which control/shape that is! You need to find another distinguishing aspect of each of the controls and shapes you want to delete and/or give them a specific name when they're created. I strongly suspect that your buttons areContentControls(3)
andContentControls(4)
. - PeterT