Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word Tables

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 10-10-2013, 11:08 AM
BrainSlugs83's Avatar
BrainSlugs83 BrainSlugs83 is offline Windows 7 64bit Office 2010 32bit
Novice
 
Join Date: Oct 2013
Location: Kirkland, WA
Posts: 5
BrainSlugs83 is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 10-11-2013, 12:36 AM
macropod's Avatar
macropod macropod is offline Windows 7 32bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,331
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
  #3  
Old 10-11-2013, 11:17 AM
BrainSlugs83's Avatar
BrainSlugs83 BrainSlugs83 is offline Windows 7 64bit Office 2010 32bit
Novice
 
Join Date: Oct 2013
Location: Kirkland, WA
Posts: 5
BrainSlugs83 is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 10-11-2013, 07:16 PM
macropod's Avatar
macropod macropod is offline Windows 7 32bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,331
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Try the attached. The content control has a table in it - nothing else. There is also a 'Test' macro to delete the table.
Attached Files
File Type: zip Content Controls - Table Deletion.zip (28.9 KB, 13 views)
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #5  
Old 10-11-2013, 09:41 PM
BrainSlugs83's Avatar
BrainSlugs83 BrainSlugs83 is offline Windows 7 64bit Office 2010 32bit
Novice
 
Join Date: Oct 2013
Location: Kirkland, WA
Posts: 5
BrainSlugs83 is on a distinguished road
Default

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.
Reply With Quote
  #6  
Old 10-11-2013, 09:48 PM
BrainSlugs83's Avatar
BrainSlugs83 BrainSlugs83 is offline Windows 7 64bit Office 2010 32bit
Novice
 
Join Date: Oct 2013
Location: Kirkland, WA
Posts: 5
BrainSlugs83 is on a distinguished road
Default

Here, I went ahead and removed the paragraph, and my issue is now reproducible with your document.

Attached Files
File Type: zip Content Controls - Table Deletion - 2.zip (22.9 KB, 6 views)
Reply With Quote
  #7  
Old 10-11-2013, 11:38 PM
macropod's Avatar
macropod macropod is offline Windows 7 32bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,331
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]

Last edited by macropod; 10-11-2013 at 11:46 PM. Reason: Updated code
Reply With Quote
  #8  
Old 11-14-2013, 01:12 AM
BrainSlugs83's Avatar
BrainSlugs83 BrainSlugs83 is offline Windows 7 64bit Office 2010 32bit
Novice
 
Join Date: Oct 2013
Location: Kirkland, WA
Posts: 5
BrainSlugs83 is on a distinguished road
Default

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. :-/
Reply With Quote
  #9  
Old 11-14-2013, 03:06 AM
macropod's Avatar
macropod macropod is offline Windows 7 32bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,331
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Quote:
Originally Posted by BrainSlugs83 View Post
I was unaware of this requirement about content controls requiring empty paragraphs in them.
It's not that content controls require empty paragraphs, but that inserting a table into one (through the UI) inserts a paragraph before and after the table.
Quote:
Again, you can delete the trailing paragraph from within Word's UI (I posted the steps above).
I can't see those steps anywhere. I'm curious as to how you're deleting the paragraph break after the table in the content control, especially if the content control is protected against deletion. Indeed, if the content control is protected against deletion, I can't even delete the paragraph break that gets inserted before the table.

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:
At any rate, allowed or not -- Word doesn't prevent users from doing it, so I have to account for it.
Until I know how it's done without doing the above, I can't really say how one might protect against it, though a ConentControlOnExit macro could probably be used to Undo that action in most cases.
Quote:
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. :-/
Perhaps the safest way is to delete the content control and its table, then recreate the content control:
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
[MS MVP - Word]
Reply With Quote
Reply

Tags
content controls, deleting, tables
Please reply to this thread with any new information or opinions.

Thread Tools
Display Modes


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


All times are GMT -7. The time now is 04:37 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft