+ Reply to Thread
Results 1 to 3 of 3

Create Command Button and format

Hybrid View

  1. #1
    Registered User
    Join Date
    11-16-2011
    Location
    Alberta
    MS-Off Ver
    2010
    Posts
    15

    Create Command Button and format

    Hello,
    I've been having a rough time getting this code to run properly in MS Word 2012. I'm trying to create a command button upon document open and modify it's properties.

    Private Sub Document_open()
    
    'Add a command button to a new document
        Dim doc As Word.Document
        Dim shp As Word.InlineShape
        Set doc = ActiveDocument
        
        Set shp = doc.Content.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1")
        
        doc.InlineShapes(1).ConvertToShape
        
        'On Error Resume Next
        With CommandButton1
            .AutoSize = False
            .Caption = "Save & Close"
            .Enabled = True
            .Font = Calibri
            .Font.Bold = True
            .Font.Underline = False
            .Font.Size = 10
            .Height = 24
            .Left = 500
            .Locked = False
            .TakeFocusOnClick = True
            .Top = 25
            .Width = 72
        End With
        
    End Sub
    When stepping through the code I get the "can't enter break mode" when reaching the button creation line, this isn't so bad because the code still runs. I just wanted to mention in case it might help point to the problem.

    The reason I converted it to a shape is so I can access it's properties that require modification, however, upon reaching the "with" statement I get error 424 object required.

    The weird thing is that if I leave the button still in the document and run the code again it works fine (although it makes another button)....so it seems like maybe the cannot enter break mode is causing the code to not recognize the command button....or maybe it needs a delay or something.

    Any help is greatly appreciated....Thanks!
    Last edited by dmeinder; 04-17-2013 at 10:30 AM.

  2. #2
    Registered User
    Join Date
    11-16-2011
    Location
    Alberta
    MS-Off Ver
    2010
    Posts
    15

    Re: Create Command Button and format

    Bump....Thanks

  3. #3
    Registered User
    Join Date
    11-16-2011
    Location
    Alberta
    MS-Off Ver
    2010
    Posts
    15

    Re: Create Command Button and format

    so here is the solution:

    Private Sub Document_open()
    
    Dim DocTest As String
    On Error Resume Next
    DocTest = ActiveDocument.Tables(1).Cell(1, 1).Range.Text
    DocTest = Left(DocTest, Len(DocTest) - 2)
    
    If Not DocTest = "Subject" Then
    
        Dim doc As Word.Document
        
        Set doc = ActiveDocument
    
        With doc.Content.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1").ConvertToShape.OLEFormat.Object
            .AutoSize = False
            .Caption = "Save & Close"
            .Enabled = True
            .Font.Underline = False
            .Font.Size = 11
            .Height = 24
            .Left = 500
            .Locked = False
            .TakeFocusOnClick = True
            .Top = 25
            .Width = 72
        End With
        
        On Error Resume Next
        For Each o In ActiveDocument.Shapes
            If Not o.OLEFormat.Object.Name = "CommandButton1" Then
                o.Delete
            End If
        Next
    End If
    
    If DocTest = "Subject" Then
        On Error Resume Next
        For Each o In ActiveDocument.Shapes
            If o.OLEFormat.Object.Name = "CommandButton1" Then
                o.Delete
            End If
        Next
    End If
        
    End Sub
    Thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1