|
|
Thread Tools | Display Modes |
#1
|
||||
|
||||
Deleting a table from a content control -- preserving the content control
Hey guys, this is more of a content controls question than a tables question -- but it has to do with tables.
I have a content control that has a table in it -- and ONLY a table in it (like if you look at the open XML there are no paragraphs above or below it -- when I delete the table via calling cc.Range.Tables(1).Delete [either in VBA or VSTO] -- the table gets deleted AND the content control. I need to just empty the content control -- calling cc.Range.Delete leaves the table intact -- inserting XML over the top of the cc.Range just puts the XML into the first cell of the table... I'm at my wit's end here. How do you delete the table (or content from a content control in general) and preserve the container? Thanks, --Mikey |
#2
|
||||
|
||||
In my testing, only the table within the content control gets deleted - the content control display reverts to the std placeholder text.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
||||
|
||||
I get the same behavior IF there's a paragraph in the content control in addition to the table. Can you show me your document?
If you insert the table by hand in the UI there is definitely a paragraph in the content control. (Turn on show markup or examine the document.xml file in the package, you'll see it.) You can either delete it manually with the XML editor in Visual Studio or select it and then select beyond the bottom of the control to delete it to mimic my scenario. Once the paragraph is gone, I can not find a way (via automation) to re-add it either. If you're having trouble creating such a content control, I can attach a sample document. |
#4
|
||||
|
||||
Try the attached. The content control has a table in it - nothing else. There is also a 'Test' macro to delete the table.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
||||
|
||||
Hey, thanks for the response.
I reviewed the attached file, but I think we're not on the same page, because in the document that you attached the content control contains a trailing paragraph. If you examine it in the Visual Studio XML editor, or if, from within Word, you turn on show paragraph marks, you can see that it's there and that it's inside the content control. I've highlighted what I mean in the following image (sorry if I wasn't clear enough before): I'll post an edited version of your the document below. |
#6
|
||||
|
||||
Here, I went ahead and removed the paragraph, and my issue is now reproducible with your document.
|
#7
|
||||
|
||||
In the document I posted, there is a paragraph mark after the table because a document must always have at least one ordinary paragraph following the last table in the document. The same applies to tables within a content control; so how did you get your table into a content control without the terminating paragraph mark?
In any event, there are ways around this. For example: Code:
Sub DelCCTbl() Dim Rng As Range With ActiveDocument.ContentControls(1).Range Set Rng = .Tables(1).Range .Tables(1).ConvertToText With Rng .End = .End - 1 .Start = .Start + 1 .Delete End With Set Rng = Nothing End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] Last edited by macropod; 10-11-2013 at 11:46 PM. Reason: Updated code |
#8
|
||||
|
||||
Hey, thanks for the response! Sorry on getting back so late. I've just been super busy and have put this issue on the back burner until now.
I was unaware of this requirement about content controls requiring empty paragraphs in them. Again, you can delete the trailing paragraph from within Word's UI (I posted the steps above). At any rate, allowed or not -- Word doesn't prevent users from doing it, so I have to account for it. As for the approach, it gets me a bit further, but there's still some circumstances where it doesn't work quite right -- for example, if the content control is a child content control of a parent, and it happens to be the very last piece of content in the parent (i.e. the parent can contain tables, other content controls, paragraphs, etc., etc. but there is zero content after the final child control)... In this situation, calling nothing but ".ConvertToText" does almost exactly what it should -- it converts the Table to text, but it moves the child control outside of the parent. I suppose I can detect this watching for a change in "cc.ParentContentControl" -- but then I'd have to undo it (what if I'm in the middle of an UndoRecord, I don't want to undo the whole thing...) -- and also, I have to figure out a way to prevent the removal of the content control... put a paragraph after it, somehow, seems to do the trick... ugh, it just gets super hairy from there. :-/ |
#9
|
||||
|
||||
Quote:
Quote:
The only way I can get a table into a content control without the paragraph before and after the table is to first create the table, then select it and insert the content control. That is the reverse of how one would ordinarily insert these into a document. AFAIK, if the document has the content controls in place before the table is added and the applicable document protection is used, it is not possible for a user to do this - they would have to remove the protection and the content control, insert the table then reinsert the content control. Quote:
Quote:
Code:
Sub DelCCTbl() Dim Rng As Range, i As Long, CCtl As ContentControl, CCType As Long, CCTtl As String, CCTag As String, bProt As Boolean With ActiveDocument With .ContentControls(1) CCType = .Type CCTag = .Tag CCTtl = .Title bProt = .LockContentControl .LockContentControl = False .Range.Characters.Last.Next.InsertBefore vbCr Set Rng = .Range .Delete End With Rng.Tables(1).Delete Rng.InsertAfter vbCr Set CCtl = .ContentControls.Add(CCType, Rng) With CCtl .Type = CCType .Tag = CCTag .Title = CCTtl .LockContentControl = bProt End With End With Set Rng = Nothing: Set CCtl = Nothing End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
content controls, deleting, tables |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Hierarchical content control | ntjson | Word VBA | 1 | 04-04-2013 12:07 AM |
Clearing content control tick box within table | jillapass | Word VBA | 1 | 08-08-2012 04:58 PM |
Word2010 check boxes and plain text content control boxes in same table | fcsungard | Word | 5 | 06-01-2012 01:16 AM |
Content control titles | jillapass | Word VBA | 3 | 05-29-2012 06:11 AM |
Retrieving content control value | jillapass | Word VBA | 4 | 05-24-2012 05:07 AM |