none
How can I refer to button which fired onclick event? RRS feed

  • Question

  • So I'm creating onclick events dynamically and they Call Copy sub

    Second sub is meant to iterate through fields and compare fields code and button's name

    How do I access button's name in Copy sub which was called from button event? It seems there is no such keyword as "this"

    And how do you compare strings?


    • Edited by Raston89 Thursday, February 23, 2012 8:49 AM
    Thursday, February 23, 2012 8:46 AM

Answers

  • I don't know much about Word but put this together with a bit of experimenting

    with 3 commandbuttons in Fields

    Private Sub CommandButton1_Click()
         Call test(CommandButton1)
    End Sub
    
    Private Sub CommandButton2_Click()
         Call test(CommandButton2)
    End Sub
    
    Private Sub CommandButton3_Click()
         Call test(CommandButton3)
    End Sub
    
    Sub test(cbt As MSForms.CommandButton)
    Dim doc As Document
    Dim rng As Range
    Dim shp As Word.InlineShape
    Dim shps As Word.InlineShapes
    Dim fld As Field
            Set doc = ThisDocument
         Set shps = doc.InlineShapes
            For Each shp In shps
                 If shp.OLEFormat.Object.Name = cbt.Name Then
                         Set fld = shp.Field
                         Set rng = fld.Result
                            MsgBox cbt.Name & vbCr & _
                                        cbt.Caption & vbCr & _
                                        "Field index: " & fld.Index & vbCr & _
                                        "Range.Start: " & rng.Start
                                        cbt.Caption = "hello " & rng.Start
                 End If
         Next
    
    End Sub

    Peter Thornton

    • Marked as answer by Raston89 Thursday, February 23, 2012 2:57 PM
    Thursday, February 23, 2012 12:32 PM
    Moderator

All replies

  • Raston89 wrote:
    > So I'm creating onclick events dynamically and they Call Copy sub
    >
    > Second sub is meant to iterate through fields and compare fields
    > code and button's name
    >
    > How do I access button's name in Copy sub which was called from
    > button event? It seems there is no such keyword as "this"
    > ...
     
    Maybe you can refer to the control with the focus:
     
    Screen.ActiveControl
     
    or for the name as string:
     
    Screen.ActiveControl.Name
     
    There are also:
     
    Screen.ActiveForm
    Application.CurrentObjectName
    Application.CurrentObjectType
    CodeContextObject and CodeContextObject.Name
     
    > And how do you compare strings?
     
    I'm not sure what you mean. Show an example.
     
    --
    cu
    Karl
    *********
    Access-FAQ (German/Italian): http://www.donkarl.com
     
    Thursday, February 23, 2012 9:06 AM
  • Hi

    When I run Sub it says "Object required" (using your method)

    Also I have no idea why Copy is called everytime I run Sub? This function should be called after onclick event. But everytime I run it, it copies hardcoded value, but then if I click button, it doesnt copy it... And this is probably reason why I get error "Object required", because it fires Sub before button is clicked, so there is no focus yet


    Public Sub Test()
    Dim doc As Word.Document
    Set doc = ActiveDocument
    
    Dim shp As InlineShape
    For Each shp In ActiveDocument.InlineShapes
    Dim sCode As String
        sCode = "Private Sub " & shp.OLEFormat.Object.Name & "_Click()" & vbCrLf & _
                "   Call Copy " & vbCrLf & _
                "End Sub"
        doc.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString sCode
    
    Next shp
    
    End Sub
    
    Public Sub Copy()
    Dim MyData As DataObject
    Set MyData = New DataObject
    Dim fld As Field
    MsgBox (Screen.ActiveControl.Name)
        For Each fld In ActiveDocument.Fields
            Dim nameShp As String
            'nameShp = shp.OLEFormat.Object.Name
            Next fld
    Dim Code As String
    Code = ActiveDocument.Fields(13).Code
    MyData.SetText (Code)
    MyData.PutInClipboard
    
    End Sub


    • Edited by Raston89 Thursday, February 23, 2012 9:24 AM
    Thursday, February 23, 2012 9:12 AM
  • Raston89 wrote:
    >
    > When I run Sub it says "Object required" (using your method)
    > ...
    > Public Sub Test()
    > Dim doc As Word.Document
    > ...
     
    Initially you didn't specify the application.
    My answer refers to Access not Word, where the
    objects are different.
     
    --
    cu
    Karl
    *********
    Access-FAQ (German/Italian): http://www.donkarl.com
     
    Thursday, February 23, 2012 9:52 AM
  • My bad, of course it's word application

    Is there similar function in Word?

    Thursday, February 23, 2012 10:31 AM
  • Which application are you working with, eg Excel, Word etc.
    What sort of a button, eg Forms or ActiveX
    What is the button on, eg a userform or on something else like an Excel worksheet

    And how do you compare strings?

    Do you mean you want to know if the strings are exactly the same (if so case sensitive or not), or do you mean you want to know if alphabetically before or after or identical.

    Peter Thornton

    Thursday, February 23, 2012 10:34 AM
    Moderator
  • 1. It's Word application

    2. ActiveX

    3. Actually I will iterate through fields and I need to check if fields contain button name

    Thursday, February 23, 2012 10:40 AM
  • I don't know much about Word but put this together with a bit of experimenting

    with 3 commandbuttons in Fields

    Private Sub CommandButton1_Click()
         Call test(CommandButton1)
    End Sub
    
    Private Sub CommandButton2_Click()
         Call test(CommandButton2)
    End Sub
    
    Private Sub CommandButton3_Click()
         Call test(CommandButton3)
    End Sub
    
    Sub test(cbt As MSForms.CommandButton)
    Dim doc As Document
    Dim rng As Range
    Dim shp As Word.InlineShape
    Dim shps As Word.InlineShapes
    Dim fld As Field
            Set doc = ThisDocument
         Set shps = doc.InlineShapes
            For Each shp In shps
                 If shp.OLEFormat.Object.Name = cbt.Name Then
                         Set fld = shp.Field
                         Set rng = fld.Result
                            MsgBox cbt.Name & vbCr & _
                                        cbt.Caption & vbCr & _
                                        "Field index: " & fld.Index & vbCr & _
                                        "Range.Start: " & rng.Start
                                        cbt.Caption = "hello " & rng.Start
                 End If
         Next
    
    End Sub

    Peter Thornton

    • Marked as answer by Raston89 Thursday, February 23, 2012 2:57 PM
    Thursday, February 23, 2012 12:32 PM
    Moderator