Results 1 to 2 of 2

Thread: Programing a Command Button in Word

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2022
    Posts
    1

    Exclamation Programing a Command Button in Word

    I have come here in hope...........

    I used to have a document in Lotus Notes that had a table with a "Press When Complete Button" in the last cell of row in the table.
    Once clicked, the Users Name, Date and Time would replace the button in the table. (All part of a audit trail)

    Is there anyone in here that can assist me it transferring this to Word.

    I have got as far as creating the button, I now need to program it - bit with next to NO VB experience, I am stuck.

    I have added a doc with shots of said button.

    Thanks in advance.......... Jon
    Attached Files Attached Files

  2. #2
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

    Re: Programing a Command Button in Word

    How to create a Button in Word:

    https://docs.microsoft.com/en-us/pre...button-in-word

    Get User Name (assuming that this is the user name you are looking for):

    https://docs.microsoft.com/en-us/off...ation.username

    Get Date:

    https://docs.microsoft.com/en-us/off.../date-function

    https://docs.microsoft.com/en-us/off...insertdatetime

    Get Time:

    https://docs.microsoft.com/en-us/off.../time-function

    Sample Code:

    Code:
    Option Explicit
    
    Sub Test()
    
        '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 = "Press When Complete"
        
        'Add a procedure for the click event of the inlineshape
        '**Note: The click event resides in the This Document module
        Dim sCode As String
        sCode = "Private Sub " & shp.OLEFormat.Object.Name & "_Click()" & vbCrLf & _
                "   MsgBox ""You Clicked the CommandButton""" & vbCrLf & _
                "End Sub"
        doc.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString sCode
    
    End Sub
    
    
    Sub Button1_Click()
    
        '
        ' Button1_Click Macro
        '
        '
        Dim strUserName As String, strDate As String, strTime As String
        
        strUserName = Application.UserName
        strDate = Date
        strTime = Time
        MsgBox "User: " & strUserName & ", Date: " & strDate & ", Time: " & strTime, , "Button Clicked"
    
    End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width