Solved

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

Posted on 2006-11-29
17
385 Views
Last Modified: 2012-06-21
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
Comment
Question by:mattrich1
  • 7
  • 6
  • 4
17 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
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
 

Author Comment

by:mattrich1
Comment Utility
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
 
LVL 3

Expert Comment

by:Obadah_HighTech
Comment Utility
Hello,
using the previous "handler"
in each command button click event put this code:

CommandButton_Click Me.ActiveControl
0
 

Author Comment

by:mattrich1
Comment Utility
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
 
LVL 3

Expert Comment

by:Obadah_HighTech
Comment Utility
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
 

Author Comment

by:mattrich1
Comment Utility
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
 
LVL 3

Expert Comment

by:Obadah_HighTech
Comment Utility
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
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:mattrich1
Comment Utility
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
 
LVL 3

Expert Comment

by:Obadah_HighTech
Comment Utility
Hello,

In a VB Form.

very sorry if you meant directly on the document (ActiveX Document Control)
0
 

Author Comment

by:mattrich1
Comment Utility
Not to worry...yes its an ActiveX Document Control inside a Word document.
So how would you do it?
Thanks
Matthew
0
 
LVL 3

Expert Comment

by:Obadah_HighTech
Comment Utility
Hello,

it is harder than it may seem, but I am working on it (in my free time)
0
 

Author Comment

by:mattrich1
Comment Utility
Really need a solution to this one - I am upping the point value for the question!
Matthew
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
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
 
LVL 47

Assisted Solution

by:Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs) earned 250 total points
Comment Utility
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
 
LVL 3

Accepted Solution

by:
Obadah_HighTech earned 250 total points
Comment Utility
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
 

Author Comment

by:mattrich1
Comment Utility
Thanks to webtubbs & Obadah_HighTech...
Great stuff guys...enjoy the points!

Regards,
Matthew
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now