Solved

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

Posted on 2006-11-29
17
389 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)
ID: 18042996
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
ID: 18043790
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
ID: 18044785
Hello,
using the previous "handler"
in each command button click event put this code:

CommandButton_Click Me.ActiveControl
0
 

Author Comment

by:mattrich1
ID: 18049033
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
ID: 18049724
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
ID: 18049786
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
ID: 18049954
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)
ID: 18049979
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:mattrich1
ID: 18050108
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
ID: 18050598
Hello,

In a VB Form.

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

Author Comment

by:mattrich1
ID: 18050669
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
ID: 18053132
Hello,

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

Author Comment

by:mattrich1
ID: 18065717
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)
ID: 18065775
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
ID: 18065864
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
ID: 18066754
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
ID: 18066910
Thanks to webtubbs & Obadah_HighTech...
Great stuff guys...enjoy the points!

Regards,
Matthew
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Window placement 17 74
Put text in a picture ASP.NET C# 2 50
Run code from text file in vb 1 56
Can we place a tooltip on the actual vb6 form 5 35
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

929 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

11 Experts available now in Live!

Get 1:1 Help Now