Diane Rhoads
Diane Rhoads

Reputation: 21

Word document with command buttons -- copy entire document except for command buttons?

I have a word document that has a "Reset Fields" command button and a "Copy" button. Once the fields are filled in I need to copy the document to paste in an email and/or other program that is specific to my company.

When I click the copy command it does what I want, except it also copies the command buttons.

The code for the command button is currently:

Private Sub CommandButton2_Click()
Dim cc As ContentControl
     Dim ff As Field
     
     
     If ActiveDocument.ProtectionType <> wdNoProtection Then
         ActiveDocument.Unprotect
     End If
     Selection.WholeStory
    Selection.Copy
End Sub

What can I use instead of Selection.WholeStory to stop at the end of the document and NOT copy the command buttons?

Upvotes: 2

Views: 34

Answers (2)

Charles Kenyon
Charles Kenyon

Reputation: 1028

Get rid of the Command buttons.

Put your code in the template for the document as macros. Put buttons for the macros on the QAT in the template.

Neither the macros nor the buttons get sent.

Upvotes: 0

Michal
Michal

Reputation: 6064

How about instead of not copying the command buttons, we copy everything and then delete the unwanted items from the new document?

Private Sub CommandButton1_Click()
    Dim newDoc As Document
    Dim docRange As Range
    Dim cc As ContentControl
    Dim ff As Field
    Dim shape As shape
    Dim inlineShape As inlineShape
    
    If ActiveDocument.ProtectionType <> wdNoProtection Then
        ActiveDocument.Unprotect
    End If
    
    ' Copy the entire document content using Range
    Set docRange = ActiveDocument.Content
    docRange.Copy
    
    ' Create a new document and paste the content
    Set newDoc = Documents.Add
    newDoc.Content.Paste
    
    ' Loop through all content controls (including command buttons) and delete them - remove this section if not required
    For Each cc In newDoc.ContentControls
        cc.Delete
    Next cc
    
    ' Loop through all form fields (including command buttons) and delete them - remove this section if not required
    For Each ff In newDoc.FormFields
        ff.Delete
    Next ff
    
    ' Loop through all inline shapes (including ActiveX controls) and delete them - remove this section if not required
    For Each inlineShape In newDoc.InlineShapes
        If inlineShape.OLEFormat.ProgID = "Forms.CommandButton.1" Then
            inlineShape.Delete
        End If
    Next inlineShape
    
    ' Loop through all shapes (including ActiveX controls) and delete them - remove this section if not required
    For Each shape In newDoc.Shapes
        If shape.OLEFormat.ProgID = "Forms.CommandButton.1" Then
            shape.Delete
        End If
    Next shape
End Sub

Upvotes: 1

Related Questions