Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 471
  • Last Modified:

Selecting active object (command button) in VBA/Word 2000

I have a Word document with a simple command button which has the following onclick event:
(basically when I click it, I want it to change color from RED-ORANGE and vice versa
============================================================

Private Sub CommandButton1_Click()
Select Case Project.ThisDocument.CommandButton1.BackColor

Case &HFF&
Project.ThisDocument.CommandButton1.BackColor = &H80FF&

Case &H80FF&
Project.ThisDocument.CommandButton1.BackColor = &HFF&

Case Else
Project.ThisDocument.CommandButton1.BackColor = &H80FF&

End Select
End Sub

============================================================

This code works fine, but instead of using direct referencing to the object CommandButton1, I want to be able to create reusable code I can simply copy and paste in for each new command button I create.

I want something like this:

============================================================

Private Sub CommandButton1_Click()
Select Case Document.SelectedObject.BackColor

Case &HFF&
Document.SelectedObject.BackColor = &H80FF&

Case &H80FF&
Document.SelectedObject.BackColor = &HFF&

Case Else
Document.SelectedObject.BackColor = &H80FF&

End Select
End Sub

============================================================

Document.SelectedObject.BackColor is not a real property. But I want to know how to access the selected object, object that has just been clicked? Any ideas would be much appreciated.

Cheers
Matthew
0
mattrich1
Asked:
mattrich1
  • 7
  • 6
  • 4
2 Solutions
 
Wayne Taylor (webtubbs)Commented:
Hi Matthew,

How about using a special "handler" macro that can be called from each (in a standard module)....

Public Sub CommandButton_Click(Btn As CommandButton)
    Select Case Btn.BackColor
        Case &HFF&: Btn.BackColor = &H80FF&
        Case &H80FF&: Btn.BackColor = &HFF&
        Case Else: Btn.BackColor = &H80FF&
    End Select
End Sub

....the from each of your commandbuttons, use something like this....

Private Sub CommandButton1_Click()
    CommandButton_Click CommandButton1
End Sub

It's not exactly what you require, but should make it much easier. All you'd need to do for each CommandButton is change the Btn parameter to match the name of the button.

Regards,

Wayne
0
 
mattrich1Author Commented:
Thanks Wayne

Thats half my problem solved.

Next, instead of passing the fixed value "CommandButton1" to the handler sub, can I instead pass something like "Active.Button" or "ThisDocument.ActiveSelection" - these obviously aren't the correct code samples, but I want the solution to use no fixed referencing at all....as I want to be able to copy and paste these routines for each command button I have without editing the code.

Basically, I want something like:

==================================================

Private Sub CommandButton1_Click()
------> THIS LINE -------> CommandButton_Click Thisdocument.ActiveSelection
End Sub


Public Sub CommandButton_Click(Btn As CommandButton)
    Select Case Btn.BackColor
        Case &HFF&: Btn.BackColor = &H80FF&
        Case &H80FF&: Btn.BackColor = &HFF&
        Case Else: Btn.BackColor = &H80FF&
    End Select
End Sub

==================================================

Any ideas, anyone?

Cheers
Matthew
0
 
Obadah_HighTechCommented:
Hello,
using the previous "handler"
in each command button click event put this code:

CommandButton_Click Me.ActiveControl
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
mattrich1Author Commented:
Thanks for that Obadah_HighTech,

However I get the following error when that line is executued...
-----------------------------------------------------------
Compile Error: Method or Data member not found
-----------------------------------------------------------

Perhaps the ActiveControl property is only valid for VB and not VBA? Could there be another property of Me which could be used?

Thanks
Matthew
0
 
Obadah_HighTechCommented:
Hello,

I used Word 2003 VBA, and it works 100%, but you could also try:

CommandButton_Click ActiveControl

if you still have a problem PLEASE upload your Doc file that gives this error to this question

0
 
mattrich1Author Commented:
Thanks Obadah
I'm running Word 2000, perhaps this is the problem?

When I use this (CommandButton_Click ActiveControl), I get:
Compile Error: Byref argument type mismatch

Matthew

0
 
Obadah_HighTechCommented:
Hello,

Try this then (byval in the "handler"):

Public Sub CommandButton_Click(ByVal Btn As CommandButton)
    Select Case Btn.BackColor
        Case &HFF&: Btn.BackColor = &H80FF&
        Case &H80FF&: Btn.BackColor = &HFF&
        Case Else: Btn.BackColor = &H80FF&
    End Select
End Sub
0
 
Wayne Taylor (webtubbs)Commented:
Obadah,

ActiveControl only applies to Frames, Pages in a MultiPage and Userforms. I'm pretty sure Matthew has his buttons on the Document, which is why your method isn't working for him.

Wayne
0
 
mattrich1Author Commented:
I think you might be right Wayne. The command button is in the document itself...even when I include byval in the handler, I still get an error:

Object Required (Run-time error 424)

===============================
Obadah

When you say it works 100% in Word 2003, do you have the command button in the document itself, not in a VB form?

Matthew
0
 
Obadah_HighTechCommented:
Hello,

In a VB Form.

very sorry if you meant directly on the document (ActiveX Document Control)
0
 
mattrich1Author Commented:
Not to worry...yes its an ActiveX Document Control inside a Word document.
So how would you do it?
Thanks
Matthew
0
 
Obadah_HighTechCommented:
Hello,

it is harder than it may seem, but I am working on it (in my free time)
0
 
mattrich1Author Commented:
Really need a solution to this one - I am upping the point value for the question!
Matthew
0
 
Wayne Taylor (webtubbs)Commented:
Matthew,

I'm pretty sure it can't be done. In Excel you can, but not Word. Would you be interested in the Excel solution?

Wayne
0
 
Wayne Taylor (webtubbs)Commented:
Actually, there is a way to add the handler for each commandbutton via VBA.....

Paste this into a regular module....

Sub AddHandlers()
Dim shp As InlineShape
Dim MacroString As String
For Each shp In ThisDocument.InlineShapes
    If shp.Type = wdInlineShapeOLEControlObject Then
        If shp.OLEFormat.ClassType = "Forms.CommandButton.1" Then
            MacroString = "Private Sub " & shp.OLEFormat.Object.Name & "_Click()" & vbCrLf & _
                                 "    CommandButton_Click " & shp.OLEFormat.Object.Name & vbCrLf & _
                                 "End Sub"
            With ThisDocument.VBProject.VBComponents("ThisDocument").CodeModule
                .AddFromString MacroString
            End With
        End If
    End If
Next
End Sub

Before you can run this macro, you'll need to Trust Access to the Visual Basic Project. Go to Tools > Macros > Security > Trusted Publishers and check the box "Trust Access to the Visual Basic Project".

Also, make sure you don't have any Click events for any of the buttons in the ThisDocument module already.

When you run the above macro, it'll loop through each of the buttons and add the handler programatically.

Regards,

Wayne
0
 
Obadah_HighTechCommented:
Hello,

At last I HAVE DONE IT !!!, try this code :

Private Sub CommandButton1_Click()
CommandButton_Click Selection.InlineShapes(1).OLEFormat.Object
End Sub
Private Sub CommandButton2_Click()
CommandButton_Click Selection.InlineShapes(1).OLEFormat.Object
End Sub
Private Sub CommandButton3_Click()
CommandButton_Click Selection.InlineShapes(1).OLEFormat.Object
End Sub
Public Sub CommandButton_Click(ByVal Btn As CommandButton)
    Select Case Btn.BackColor
        Case &HFF&: Btn.BackColor = &H80FF&
        Case &H80FF&: Btn.BackColor = &HFF&
        Case Else: Btn.BackColor = &H80FF&
    End Select
End Sub
0
 
mattrich1Author Commented:
Thanks to webtubbs & Obadah_HighTech...
Great stuff guys...enjoy the points!

Regards,
Matthew
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now