How to find and disable a content control by tag to delete it and its contents?

自古美人都是妖i 提交于 2019-12-11 01:56:02

问题


I have the unfortunate task of being forced to design a Word-based electronic production card for the unit at my company, even though I've never worked with VBA. I would much rather have done this in Excel since I wouldn't have to wrestle with content control and hard-to-find locations in various tables over the pages, but the company's documentation-system forces this particular one to be in Word.

My issue is that for proper form of the production card I need to use tables, and I need the production card to be dynamic to limit its size to what operations that are relevant for a specific order. My chosen solution is to create a full form, and to use a user form/prompt where they can choose which parts to use and which parts to ommit, and the ommitted ones will then be deleted. Part of reason for the solution is because that is how their previous (and Excel-based) production card works, so it would make it more familiar for the end user.

Because MS Word is finicky I need to use content control within these tables to not have the end user accidentally destroy half of it, but after a full workday I still cannot figure out how find and shut off the content control of the cells in tables that I want to delete. I do have the content controls tagged since that seems like the only reasonable way to find them.

This is my current code for the subprocedure, but for some reason I cannot get the ID through the ccID line, even though I have verified that the string supplied as argument is correct.

Private Sub DeleteCCByTag(ccTag As String)
    Dim cc As ContentControl
    Dim ccID As String
    ccID = ThisDocument.SelectContentControlsByTag(ccTag).Item(1).ID
    'MsgBox ccID        'Debug prompt
    Set cc = ThisDocument.ContentControls(ccID)
    cc.LockContentControl = False
    cc.LockContents = False
    cc.Delete (False)
End Sub

回答1:


First of all- your code is working find for me but...

  1. ContentControls tag is case-sensitive which could be a problem in your situation

  2. You could solve your problem without searching for ID value in this way:

    Private Sub DeleteCCByTag_Alternative(ccTag As String)
    
        Dim cc As ContentControl
        Set cc = ThisDocument.SelectContentControlsByTag(ccTag).Item(1)
    
        With cc
    
            .LockContentControl = False
            .LockContents = False
    
            .Range.Delete               'to delete CC content
    
            .Delete (False)
        End With
    End Sub
    
  3. CC.Delete in your code deletes only ContentControl objects itself but not its content. To delete content you need to add additional line which I did in my code above.




回答2:


I would add this as a comment to KazJaw's answer but I don't have the rep.

According to Microsoft's documentation, if you pass True to the Delete method it removes both the content control and its contents.

So: just get the Item as KazJaw showed, without jumping through the hoop of getting its ID:

Set cc = ThisDocument.SelectContentControlsByTag(ccTag).Item(1)

then call .Delete(True) on it.



来源:https://stackoverflow.com/questions/25199398/how-to-find-and-disable-a-content-control-by-tag-to-delete-it-and-its-contents

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!