0

I am trying to save some values from all command buttons in Word, then delete them, and then add them back again.

This is for printing purposes.

My question is how can I loop through all the command buttons to save their top, left, name and width values, how can I then delete them and then how can I create new buttons with their previous name, width and position.

I know how to create arrays and whatnot, all I need to figure out is the syntax for looping through the command buttons and for adding new command buttons with the values from the old ones.

R3uK
  • 14,417
  • 7
  • 43
  • 77
Uriel Katz
  • 319
  • 1
  • 8
  • 21

2 Answers2

0

Not tested the code but should work with little modification where ever required

Delete Command Button

For Each o In ActiveDocument.InlineShapes      
   If o.OLEFormat.Object.Name = "CommandButtonName" Then
        'Save all properties of the command button using .Top, .Left, .Width etc
        o.Delete
   End If
Next

For Adding Command Button (Check http://support.microsoft.com/kb/246299):

   'Add a command button to a new document
    Dim doc As Word.Document
    Dim shp As Word.InlineShape
    Set doc = Documents.Add

    Set shp = doc.Content.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1")
    shp.OLEFormat.Object.Caption = "Click Here"
    shp.OLEFormat.Object.Left = 100
    shp.OLEFormat.Object.Top = 100
    shp.OLEFormat.Object.Width = 255
    shp.OLEFormat.Object.Visible = True
rags
  • 2,580
  • 19
  • 37
  • you shouldn't use that kind of loop for deleting process. Always use `For i... Step -1` if you want to delete all elements of certain type for sure! – Kazimierz Jawor Jul 12 '13 at 06:10
0

I've figured resizing the command buttons to 0x0 provided a more elegant option that didn't require arrays and was much quicker.

Uriel Katz
  • 319
  • 1
  • 8
  • 21