Solved

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

Posted on 2006-11-29
17
393 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 

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
 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

772 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