Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-27-2016, 12:56 PM
JuliaZ JuliaZ is offline Copy Text only of a Content Control Windows 7 32bit Copy Text only of a Content Control Office 2013
Novice
Copy Text only of a Content Control
 
Join Date: Jul 2016
Posts: 3
JuliaZ is on a distinguished road
Default Copy Text only of a Content Control


Is there a way to select a content control in Word 2013 and copy the text only? When the content control is selected and copied and then pasted into another document, Word copies the content control into the document along with the text. We only want the text to be copied, not the content control itself.
Reply With Quote
  #2  
Old 07-27-2016, 11:22 PM
gmayor's Avatar
gmayor gmayor is offline Copy Text only of a Content Control Windows 10 Copy Text only of a Content Control Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

If you are copying and pasting manually then, it is not the copying that is the problem but the pasting. Select Paste > Keep Text Only.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 07-28-2016, 07:03 AM
JuliaZ JuliaZ is offline Copy Text only of a Content Control Windows 7 32bit Copy Text only of a Content Control Office 2013
Novice
Copy Text only of a Content Control
 
Join Date: Jul 2016
Posts: 3
JuliaZ is on a distinguished road
Default

I tried doing Keep Text only and then I lose underscores, strikethroughs, etc. I want to keep all formatting and text, just not the Content Control itself.
Reply With Quote
  #4  
Old 07-28-2016, 08:59 PM
gmayor's Avatar
gmayor gmayor is offline Copy Text only of a Content Control Windows 10 Copy Text only of a Content Control Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

In that case you will need a macro. The following should do the job, copying the formatted text of the content control containing the cursor to the clipboard.

Code:
Sub CopyCC_Content()
Dim oCC As ContentControl
Dim oRng As Range
    On Error GoTo lbl_Exit
    For Each oCC In ActiveDocument.ContentControls
        If Selection.InRange(oCC.Range) Then
            Set oRng = oCC.Range
            oCC.Delete
            oRng.FormattedText.Copy
            ActiveDocument.Undo 1
            Exit For
        End If
    Next oCC
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 07-29-2016, 01:15 PM
JuliaZ JuliaZ is offline Copy Text only of a Content Control Windows 7 32bit Copy Text only of a Content Control Office 2013
Novice
Copy Text only of a Content Control
 
Join Date: Jul 2016
Posts: 3
JuliaZ is on a distinguished road
Default

Is there any way to get the contents without deleting the original content control? If something happens (the user clicks undo, or word ends abruptly), we don't want the original document to lose its content control.
Reply With Quote
  #6  
Old 03-26-2022, 05:08 PM
skarden skarden is offline Copy Text only of a Content Control Windows Vista Copy Text only of a Content Control Office 2010 64bit
Novice
 
Join Date: Dec 2011
Location: Orlando, FL
Posts: 27
skarden is on a distinguished road
Default

I would change the code in one main way, and that is to test if the ContentControl ("CC") is locked. If it is, and you don't unlock it, then you won't be able to delete it.

It is also important to note that this will not work for a nested CC unless you unlock them too. You would need to add a test if there is one or more parents. The best way to do that is to call a Function that tests the a parent's ID. If there is no parent then it will error out. If there is, then unlock the Parent and pass the Parent CC (via the ID you just got) back to the function and keep going until you get to the top Parent Level (i.e. Parent_ID will be "" when the test for it fails).

Sub Copy_CC_Text

On Error Goto lblError

Dim bLocked As Boolean

Dim oCC As ContentControl

Dim oRng As Range

For Each oCC In ActiveDocument.ContentControls
If Selection.InRange(oCC.Range) Then
Set oRng = oCC.Range
If oCC.LockContentControl = True Then
bLocked = True
oCC.LockContentControl = False
End If
oCC.Delete
oRng.FormattedText.Copy
ActiveDocument.Undo 1
If bLocked Then
oCC.LockContentControl = True
End If
Exit For
End If
Next oCC

lblExit:
Exit Sub
lblError:
Debug.Print "In Copy_CC_Text got error: " & Err.Number & ", " & Err.Description
Goto lblExit
Exit Sub

Last edited by skarden; 03-27-2022 at 01:01 PM.
Reply With Quote
  #7  
Old 03-28-2022, 07:37 AM
skarden skarden is offline Copy Text only of a Content Control Windows Vista Copy Text only of a Content Control Office 2010 64bit
Novice
 
Join Date: Dec 2011
Location: Orlando, FL
Posts: 27
skarden is on a distinguished road
Default

BTW, I found that when you undelete a Date (calendar) control it doesn't come back as a date CC. It also doesn't remember the date format or storage formate. So I had to create variables that save my CC's type, date format (e.g. MM/dd/YYYY) and storage formate (for this later one, I often set it to just Date vs Date and Time if it is just a date)

So at the top in my Dim section I added:

Dim sDate_Format As String 'In case we have a Date control we will have to reset its format after it is undeleted
Dim sDate_Storage As String
Dim sType As String 'Need this because we have to reset the type to Date after it is undeleted if it was a date CC

And then when I found it I added:
sType = CC.Type
If sType = wdContentControlDate Then
sDate_Format = CC.DateDisplayFormat
sDate_Storage = CC.DateStorageFormat
End If

Finally after I did the undelete I added:
If sType = wdContentControlDate Then 'If you check sType it will be 6 which is the Date type.
ActiveDocument.ContentControls(sCC_ID).Type = wdContentControlDate
ActiveDocument.ContentControls(sCC_ID).DateDisplay Format = sDate_Format 'We also have to rest date display, e.g. MM/dd/YYY
ActiveDocument.ContentControls(sCC_ID).DateStorage Format = sDate_Storage 'We also have to rest date storage format, e.g. date only
End If

I also wanted to note that I needed to put a DoEvents after the Delete as I found that date controls were not always being undeleted (and as I mentioned, it wasn't being undeleted as a Date control). It may be that was because it wasn't being undeleted as a Date control it had to take extra time to decide what to restore it as, and because on top of that my CC was nested 3 deep, or just because. In any event. Adding the DoEvents there seems to have solved that.

I also found that it was having problems with CC.Delete so when I found my CC I saved its ID to sCC_ID and then replaced CC.Delete with:
ActiveDocument.ContentControls(sCC_ID).Delete
Reply With Quote
  #8  
Old 06-16-2024, 10:03 AM
gmaxey gmaxey is offline Copy Text only of a Content Control Windows 10 Copy Text only of a Content Control Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Stuart,


I just came across this post and was reading your notes on performance. After running your code, I was not seeing the issue where date type CCs was losing the selected date or date format.


Anyway, I thought that to avoid any such similar problems, why don't we never delete the original CC in the first place?


Here are two methods that never alter the original CC:

Code:
Sub Copy_CC_ContentOnly()
Dim oCC As ContentControl, oCCNested As ContentControl
Dim oRng As Range
  'This code copies the "content only" of the content control the cursor is located in.
  'It uses a temporary range variable set to end of the active document. After the code runs,
  'the content is copied to the clipboard.
  If Selection.Information(wdInContentControl) Then
    Set oCC = Selection.Range.ParentContentControl
    If Not oCC.ShowingPlaceholderText Then
      oCC.Copy
      Set oRng = ActiveDocument.Range
      oRng.Collapse wdCollapseEnd
      oRng.Paste
      Set oCC = oRng.ContentControls(1)
      oCC.LockContentControl = False
      Set oRng = oCC.Range
      oCC.Delete
      For Each oCCNested In oRng.ContentControls
        oCCNested.LockContentControl = False
        oCCNested.Delete
      Next oCCNested
      On Error Resume Next
      oRng.FormattedText.Copy
      On Error GoTo 0
      oRng.Delete
    Else
      MsgBox "The selected content control does not contain any content to copy.", _
              vbInformation + vbOKOnly, "NOTHING COPIED"
    End If
  End If
lblExit:
  Exit Sub
End Sub
Sub Copy_CC_ContentOnly_TempDoc()
Dim oCC As ContentControl, oCCNested As ContentControl
Dim oRng As Range
Dim oDoc As Document, oDocTmp As Document
  'This code copies the "content only" of the content control the cursor is located in.
  'It uses a temporary document as a range variable. After the code runs,
  'the content is copied to the clipboard.
  If Selection.Information(wdInContentControl) Then
    Set oDoc = ActiveDocument
    Set oCC = Selection.Range.ParentContentControl
    If Not oCC.ShowingPlaceholderText Then
      Set oDocTmp = Documents.Add(, , , False)
      DoEvents
      Set oRng = oDocTmp.Range
      oCC.Copy
      oRng.Collapse wdCollapseEnd
      oRng.Paste
      Set oCC = oRng.ContentControls(1)
      oCC.LockContentControl = False
      Set oRng = oCC.Range
      oCC.Delete
      For Each oCCNested In oRng.ContentControls
        oCCNested.LockContentControl = False
        oCCNested.Delete
      Next oCCNested
      oRng.FormattedText.Copy
      oDocTmp.Close wdDoNotSaveChanges
    Else
      MsgBox "The selected content control does not contain any content to copy.", _
              vbInformation + vbOKOnly, "NOTHING COPIED"
    End If
  End If
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #9  
Old 06-19-2024, 05:49 AM
skarden skarden is offline Copy Text only of a Content Control Windows 10 Copy Text only of a Content Control Office 2010 64bit
Novice
 
Join Date: Dec 2011
Location: Orlando, FL
Posts: 27
skarden is on a distinguished road
Default

Greg:
Great hearing from you again!

Am I correct that you are still using delete, but that the 1st method pastes the CC in the original doc and the 2nd one pastes it to a 2nd doc, thereby leaving the CC alone?

I'm not sure about why I was having the issue with date cc's, but since my current code works I haven't tested it further to see if it is no longer needed.

Your code is certainly more compact than what I am using and I'll have to take some time to analyze it to see if I should replace mine with yours.

Thanks for taking the time to look at it and letting me know about this post.

Stuart
Reply With Quote
  #10  
Old 06-19-2024, 08:50 AM
gmaxey gmaxey is offline Copy Text only of a Content Control Windows 10 Copy Text only of a Content Control Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Thanks Stuart. Yes, in both versions a temporary range and content control is created and used to get the content control text. The actual content control is never altered. Only copied.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy Text only of a Content Control Content Control Copy - Copies Data and CC itself shammi_raj Word 3 03-30-2016 07:01 PM
Is there VBA to paste text into content control? kintap Word VBA 2 07-02-2014 07:42 AM
Copy content control entries to next table next page Mel_Herndon Word VBA 2 05-22-2014 05:07 PM
Copy Text only of a Content Control Deleting a table from a content control -- preserving the content control BrainSlugs83 Word Tables 8 11-14-2013 03:06 AM
Templates: automatic text generation from Rich Text content control Chickenmunga Word 0 10-01-2008 11:16 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:30 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft