-
[RESOLVED] Select and Delete a command button in Word
I have a form that a user fills out and clicks a button to save and email the form. I do this by copying the document into a new document and saving it. When doing this, the button gets copied as well... I don't want to copy the button.
How do I select that particular button and delete it?
Chrissy
-
Re: Select and Delete a command button in Word
Show us the code that you are using...
-
Re: Select and Delete a command button in Word
This is the code I use to select and copy the document.
Code:
ThisFileName = ThisDocument.Name
NewFile = Documents.Add.Name
Windows(ThisFileName).Activate
Selection.WholeStory
Selection.Copy
Windows(NewFile).Activate
Selection.PasteAndFormat (wdPasteDefault)
'The following code works to delete the button, but I don't like using the number to reference the button, i would rather refer to its name so there could never be any confusion in the future as to what it is deleting.
ActiveDocument.InlineShapes(2).Select
Selection.Delete
-
Re: Select and Delete a command button in Word
Try this
Code:
Sub Sample()
Dim shp As InlineShape
'ThisFileName = ThisDocument.Name
'NewFile = Documents.Add.Name
'Windows(ThisFileName).Activate
'Selection.WholeStory
'Selection.Copy
'Windows(NewFile).Activate
'Selection.PasteAndFormat (wdPasteDefault)
'~~> This will delete all shapes in Activedocument
For Each shp In ActiveDocument.InlineShapes
shp.Delete
Next
End Sub
-
Re: Select and Delete a command button in Word
i don't want to delete all of the shapes in the document, just the button.
-
Re: Select and Delete a command button in Word
How many command buttons are there in the word doc?
-
Re: Select and Delete a command button in Word
There is only one button, but there are other shapes.
-
Re: Select and Delete a command button in Word
Try this
Code:
Sub Sample()
Dim shp As InlineShape
'ThisFileName = ThisDocument.Name
'NewFile = Documents.Add.Name
'Windows(ThisFileName).Activate
'Selection.WholeStory
'Selection.Copy
'Windows(NewFile).Activate
'Selection.PasteAndFormat (wdPasteDefault)
For Each shp In ActiveDocument.InlineShapes
If shp.OLEFormat.ClassType = "Forms.CommandButton.1" Then shp.Delete
Next
End Sub
-
Re: Select and Delete a command button in Word
it stops on this line...
If shp.OLEFormat.ClassType = "Forms.CommandButton.1" Then shp.Delete
With an error of Object variable or with variable not set...
-
Re: Select and Delete a command button in Word
Have you included
Dim shp As InlineShape
as I have done?
-
Re: Select and Delete a command button in Word
-
Re: Select and Delete a command button in Word
I just tried your code without my code and I am getting that error :D
Let me check it out...
-
Re: Select and Delete a command button in Word
I am getting an error on this line
Windows(NewFile).Activate
Try this. You will not see the 6th message box...
Code:
Sub Sample()
'Dim shp As InlineShape
On Error GoTo err
ThisFileName = ThisDocument.Name
MsgBox "1"
NewFile = Documents.Add.Name
MsgBox "2"
Windows(ThisFileName).Activate
MsgBox "3"
Selection.WholeStory
MsgBox "4"
Selection.Copy
MsgBox "5"
Windows(NewFile).Activate
MsgBox "6"
Selection.PasteAndFormat (wdPasteDefault)
MsgBox "7"
err:
MsgBox err.Description
'~~> This will delete all shapes in Activedocument
'For Each shp In ActiveDocument.InlineShapes
'If shp.OLEFormat.ClassType = "Forms.CommandButton.1" Then shp.Delete
'Next
End Sub
-
Re: Select and Delete a command button in Word
It works fine for me... it doesn't make sense you would get an error on that row of code. All it is doing is activating the newly created document that was created with this line of code>
vb Code:
NewFile = Documents.Add.Name
-
Re: Select and Delete a command button in Word
I think this is what you want...
Code:
Sub Sample()
Dim oWordDoc1 As Word.Document, oWordDoc2 As Document
Dim shp As InlineShape
Set oWordDoc1 = ThisDocument
Set oWordDoc2 = Documents.Add
oWordDoc1.Activate
Selection.WholeStory
Selection.Copy
oWordDoc2.Activate
Selection.PasteAndFormat (wdPasteDefault)
For Each shp In ActiveDocument.InlineShapes
If shp.OLEFormat.ClassType = "Forms.CommandButton.1" Then shp.Delete
Next
End Sub
-
Re: Select and Delete a command button in Word
It still gives the same error on the same line of code... I'm using Office 2003, what are you using?
-
Re: Select and Delete a command button in Word
Same and it is working just fine for me...
Can I see your word doc?
Edit:
How are you adding the commandbutton to the worddoc?
-
1 Attachment(s)
Re: Select and Delete a command button in Word
-
Re: Select and Delete a command button in Word
For some strange reason, I don't get that error anymore... I didn't change anything other than closing the file and opening it again...
Very strange!! Thanks for your help!!
-
1 Attachment(s)
Re: Select and Delete a command button in Word
I tried it and It works for me... :)
It also gave me the file save dialog box. see pic below...
-
Re: Select and Delete a command button in Word
Quote:
Originally Posted by
Chrissy
For some strange reason, I don't get that error anymore... I didn't change anything other than closing the file and opening it again...
Very strange!! Thanks for your help!!
You are welcome :wave:
Do remember to mark the thread resolved ;)
-
1 Attachment(s)
Re: Select and Delete a command button in Word
I take that back... it's not working but I have no idea why. I deleted the company name picture off of the file I posted and it seams to run fine with that gone. But... with that left in the file, it errors out on that same line. I will repost so you can see.
-
1 Attachment(s)
Re: Select and Delete a command button in Word
Try this
Code:
Dim shp As Object
ThisFileName = ThisDocument.Name
NewFile = Documents.Add.Name
Windows(ThisFileName).Activate
Selection.WholeStory
Selection.Copy
Windows(NewFile).Activate
Selection.PasteAndFormat (wdPasteDefault)
For Each shp In Activedocument.Fields
If shp.OLEFormat.ClassType = "Forms.CommandButton.1" Then shp.Delete
Next
-
Re: Select and Delete a command button in Word
That seemed to do it! Thank you so much... again!