Question : Create Button Macro-- Move to End of Document

I have a macro that creates a button, but I want it to put the button at the end of the document:

Sub AddCommandButton(ButtonName As String)
'***********************************************
'*Note:  For this to work, you must have a reference to Microsoft's VB for Apps Ext.
'*      1. In the VBA IDE, click on the 'Tools' menu item and select 'References...'
'*         from the drop-down list.
'*      2. In the 'Available References:' box of the 'References' window, scroll down
'*         to 'Microsoft Visual Basic for Applications Extensibility n.n'
'*      3. Click on the check box beside the name and then click OK near the top right
'*         of the dialog box.
'************************************************

Dim vbp As VBProject
Dim vbComp As VBComponent
Dim ILS As InlineShape

********'This doesn't seem to do it:
Selection.EndKey unit:=wdStory

Set ILS = ActiveDocument.InlineShapes.AddOLEControl("Forms.CommandButton.1")

Select Case ButtonName
    Case "GenDocs"
    ILS.OLEFormat.Object.Caption = "Generate Documents"
    ILS.OLEFormat.Object.Name = "GenerateDocs"
    ILS.OLEFormat.Object.AutoSize = True

    Set vbp = ActiveDocument.VBProject
    Set vbComp = vbp.VBComponents("ThisDocument")
    vbComp.CodeModule.AddFromString "Sub GenerateDocs_Click()" & vbCrLf & _
                                "   Call GenDocs " & vbCrLf & _
                                "End Sub"
   
    Case "UpdateColor"
    ILS.OLEFormat.Object.Caption = "Update Cell Colors"
    ILS.OLEFormat.Object.Name = "UpdateColor"
    ILS.OLEFormat.Object.AutoSize = True


    Set vbp = ActiveDocument.VBProject
    Set vbComp = vbp.VBComponents("ThisDocument")
    vbComp.CodeModule.AddFromString "Sub UpdateColor_Click()" & vbCrLf & _
                                "   UpdateCellColors" & vbCrLf & _
                                "End Sub"
   
    Case "NoColor"
    ILS.OLEFormat.Object.Caption = "Remove Cell Colors"
    ILS.OLEFormat.Object.Name = "NoColor"
    ILS.OLEFormat.Object.AutoSize = True

    Set vbp = ActiveDocument.VBProject
    Set vbComp = vbp.VBComponents("ThisDocument")
    vbComp.CodeModule.AddFromString "Sub NoColor_Click()" & vbCrLf & _
                                "   RemoveColor" & vbCrLf & _
                                "End Sub"
   
    End Select
   
End Sub

Answer : Create Button Macro-- Move to End of Document

Thanks Rajesh - this is the line that works for me if I'm at the top of the document and run this - it puts the command button at the end of the document:

    Selection.EndKey Unit:=wdStory
    Selection.InlineShapes.AddOLEControl ClassType:="Forms.CommandButton.1"
Random Solutions  
 
programming4us programming4us