Passing variables into object

L

Laurent

Or such thing...


I have an Userform called "CollButtons" that contains lots of pictures that
are used as buttons. When I refer to the a picture called, for example,
"PictureABC" I type

With CommandBars("Menu Bar").Controls(MyMenu)
.Controls(MyMenuItems(i, 0)).Picture = CollBoutons.PictureABC.Picture
(more stuff here)
End With

(these lines works fine in my code)


Now, I want to to have that ".PictureABC." to be any string I could pass,
that is any of the picture name in my userform CollButtons

How I do that ?

This is stupid question but I really try to found out myself.

I'm vaguely believe it has to deal with object declaration and passing, but
I'm not advanced enough in VBA to totally grasp intuitively how I should
do that... :)

Thanks
 
L

Laurent

in
microsoft.public.word.vba.beginners
Or such thing...


I have an Userform called "CollButtons" that contains lots of pictures
that are used as buttons. When I refer to the a picture called, for
example, "PictureABC" I type

With CommandBars("Menu Bar").Controls(MyMenu)
.Controls(MyMenuItems(i, 0)).Picture =
CollBoutons.PictureABC.Picture (more stuff here)
End With

(these lines works fine in my code)


Now, I want to to have that ".PictureABC." to be any string I could
pass, that is any of the picture name in my userform CollButtons

How I do that ?

This is stupid question but I really try to found out myself.

I'm vaguely believe it has to deal with object declaration and
passing, but I'm not advanced enough in VBA to totally grasp
intuitively how I should do that... :)

Thanks

Answering my own question after a week and posting to the group for
reference, here what I was looking for.


For Each MyControl In frmButtons.Controls
If TypeOf MyControl Is MSForms.Image Then
If MyControl.Name = MyMenuItems(4, i) Then
Set MyImage = MyControl
myButton.Picture = MyImage.Picture
End If

If MyControl.Name = MyMenuItems(5, i) Then
Set MyImage = MyControl
myButton.Mask = MyImage.Picture
End If

End If
Next MyControl


The snippet is part of a larger code I wrote to create runtime customized
menu, with *easy editable* data within code and internal customized icons
carried in the template itself. Here the code below for the benefit of
others. The code is given as it is. It may not be the best technique around
as I learned VBA two weeks ago only, but it works !




Option Explicit

Dim i As Integer
Dim MenuEntryCount As Integer
Dim MyMenuItems() As Variant


Sub PopulateMenu()
'This routine creates a custom menu in the main menu bar. It is almost
generic.
'Adapted in part from a routine and a button trick initially proposed by
Romke Soldaat
'found at http://msdn.microsoft.com/library/default.asp?url=/library/ _
' en-us/dnoffsol02/html/odc_ButtonBonanzaPartI.asp
'and in part from another routine proposed by Bill Coan
'found at http://www.mvps.org/word/FAQs/MacrosVBA/AddMenu.htm
'FaceID can be identified with small utility FaceID Browser
'found at http://www.mvps.org/skp/faceid.htm

ReDim MyMenuItems(5, 0)

Dim myButton As CommandBarButton
Dim MyMenuName As String
Dim MyMenuPosition As Integer
Dim MyControl As Control
Dim MyImage As MSForms.Image

MenuEntryCount = 0

'The name of your menu
MyMenuName = "Any String"
'Position in title bar
MyMenuPosition = 7

CustomizationContext = ActiveDocument.AttachedTemplate

'Delete the menu if existing
On Error Resume Next
CommandBars("Menu Bar").Controls(MyMenuName).Delete
On Error GoTo 0


'Syntax: FillMenu Array(Caption, Action, NewGroup, FaceID,Imagename,
imagenamemask
'Add any number of line required, one for each menu item.
'The sub will autoajust accordingly
'The last two lines are optional. If added, they refers to name of image
components
'on form "FrmButtons". These images are simply modifierd and imported *.ico
file.
'Nothing happen if the named images are not on the form. The menu will just
have no icon.

'This presentation is useful because it can be edited like a table
FillMenu Array("Item 1 Caption", "Macro", False, 0, "Item1", "Item1Mask")
FillMenu Array("Item 2", "Macro ItemTwo", True, 18, "", "")
FillMenu Array("Open", "MyOpen", False, 23, "", "")
'...
'...
'add as many as you wish. No change of line code logic required.

'The last redim is not necessary, but it doesn't hurt to remove an used
dimension.
MenuEntryCount = MenuEntryCount - 1
ReDim Preserve MyMenuItems(5, MenuEntryCount)

'Add the menu itself
With CommandBars("Menu Bar").Controls
.Add(Type:=msoControlPopup, Before:=MyMenuPosition).Caption =
MyMenuName
End With

'Add each button
For i = 0 To MenuEntryCount

With CommandBars("Menu Bar").Controls(MyMenuName).Controls
Set myButton = .Add(Type:=msoControlButton)
myButton.Caption = MyMenuItems(0, i)
myButton.OnAction = MyMenuItems(1, i)
myButton.BeginGroup = MyMenuItems(2, i)
If MyMenuItems(3, i) > 0 Then myButton.FaceId = MyMenuItems(3, i)

End With

'Try to add a custom-made image if available and no FaceID present
If MyMenuItems(3, i) = 0 Then
For Each MyControl In frmButtons.Controls
If TypeOf MyControl Is MSForms.Image Then
If MyControl.Name = MyMenuItems(4, i) Then
Set MyImage = MyControl
myButton.Picture = MyImage.Picture
End If

If MyControl.Name = MyMenuItems(5, i) Then
Set MyImage = MyControl
myButton.Mask = MyImage.Picture
End If

End If
Next MyControl
End If

Next i
End Sub

Function FillMenu(ByVal ArrayGiven As Variant)
'Fill the dynamic array with table-like presented data

For i = 0 To 5
MyMenuItems(i, MenuEntryCount) = ArrayGiven(i)
Next i
MenuEntryCount = MenuEntryCount + 1

'Add another dimension to the array, preserving existing data
ReDim Preserve MyMenuItems(5, MenuEntryCount)
End Function

Sub MyOpen()
'This is just an example how to launch an internal Word commands.
Application.Dialogs(wdDialogFileOpen).Show
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top