[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
Last edited by Chrissy; Jan 8th, 2010 at 02:28 PM.
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
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
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
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
Last edited by Siddharth Rout; Jan 8th, 2010 at 02:46 PM.
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
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>
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
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
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.
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
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero