I have a friend with a VBA project in Excel. This project has a lot of Forms that pop up and perform various functionality while the spreadsheet is being used. Some of them have complex Form_Initialize methods that rely on other things already existing (this is not a problem when the project is used as expected).
We are trying to print out the names of every control on every form within the application. Our problem is that the VBA.UserForms collection only contains forms that have already been instantiated, and we can't instantiate all the forms without their Form_Initialize methods executing.
For example:
For Each f In VBA.UserForms
Debug.Print f.Name
Debug.Print "----------------------"
For Each c In f.Controls
Debug.Print c.Name
Next c
Next f
does nothing if no forms have been used/loaded. This code:
For Each c in frmConfig.Controls
Debug.Print c.Name
Next c
First executes frmConfig.Form_Initialize(), then loops through the controls on the form printing their names. This crashes, as things that need to happen before this form is available have not happened.
Is it possible to get the names of the controls on a form WITHOUT instantiating the form (avoiding execution of frmConfig.Form_Initialize())?
Any help much appreciated!
Is this what you are trying?
Option Explicit
Sub FindObjects()
Dim vbc As VBIDE.VBComponent
Dim frm As Object
Dim Ctrl As MSForms.Control
For Each vbc In ThisWorkbook.VBProject.VBComponents
If vbc.Type = vbext_ct_MSForm Then
With VBA.UserForms
On Error Resume Next
Set frm = .Add(vbc.Name)
Debug.Print "Found userform :" & vbc.Name
If Err.Number = 0 Then
For Each Ctrl In frm.Controls
Debug.Print "Controls in Userform " & vbc.Name & _
" - " & Ctrl.Name
Next Ctrl
End If
On Error Go To 0
End With
End If
Next vbc
End Sub
IMP:
Set reference to Microsoft Visual Basic For Applications Extensibility
In Excel options, set "Trust Access To the VBA project Object Model"
Screen Shot
FOLLOWUP
Since this is a one time thing, do this
Open VBA Project
Press CTRL + F
Do as shown in the screenshot below and then run the code.
Close the file without saving once you have got what you need
Related
I'm working on a little Excel-VBA GUI/Form for the user to read and write data from/to an .ini file. One of the UserForms has a MultiPage item for which the user creates pages at runtime and after help from stackoverflow, they can also move the pages around. Because the control properties are used to write data to the .ini file, the controls must be named correctly, that is for my purpose, among other things, in line with the relevant MultiPage.Value. I added the following to the MoveLeft/Right methods and I get an error saying I cannot change the Control.Name property at runtime (382).
For i = 1 To UFmodproject.MultiPage1.Pages.Count - 1
For Each Ctrl In UFmodproject.MultiPage1.Pages(i).Controls
If TypeOf Ctrl Is MSForms.TextBox Then
Ctrl.Name = Left(Ctrl.Name, Len(Ctrl.Name) - 1) & i
End If
Next
Next
The strange thing is, I'm doing pretty much the same thing when copying a page, and that doesn't produce any errors:
For Each newCtrl In UFmodproject.MultiPage1.Pages(pCount).Controls
For Each Ctrl In UFmodproject.MultiPage1.Pages(UFmodproject.MultiPage1.Value).Controls
If (Ctrl.Left = newCtrl.Left And Ctrl.Top = newCtrl.Top) Then
newCtrl.Name = Left(Ctrl.Name, Len(Ctrl.Name) - 1) & pCount
Exit For
End If
Next
'[bunch of other code...]
Next
What could be the error here? For what it's worth, I tried Dim Ctrl as Object and Dim Ctrl as Control, both work when copying but none of them when I only try to set the Control.Name property. Furthermore, the code to copy a page is in a standard module and called from a class module. The new code to rename the controls is in the class module. I tried moving it to a standard module like the other, but to no avail.
EDIT:
I thought I had figured it out and that the error wasn't that it couldn't change the property at runtime, but it seemed that an ambiguous name is detected when the Control.Name property is set to its own value. But I was still getting errors when I tried to move a page to Value 1. Since I still seemed to have ambiguous names I ended up with the following to test:
Sub SetNames()
Dim Ctrl As Object
Dim Ctrl2 As Object
Dim i As Integer
Dim Name As String
For i = 1 To UFmodproject.MultiPage1.Pages.Count - 1
For Each Ctrl In UFmodproject.MultiPage1.Pages(i).Controls
If Int(Right(Ctrl.Name, 1)) <> i Then
For Each Ctrl2 In UFmodproject.Controls
If Ctrl2.Name = Left(Ctrl.Name, Len(Ctrl.Name) - 1) & i Then
Ctrl2.Name = Left(Ctrl.Name, Len(Ctrl.Name) - 1) & i + 4
Exit For
End If
Next
Ctrl.Name = Left(Ctrl.Name, Len(Ctrl.Name) - 1) & i
End If
Next Ctrl
Next i
End Sub
but I still kept getting the same error, but when I checked for the object with the supposedly used name in the immediate window it confirms the object doesn't exist. What am I doing wrong?
Here is a bit of background on what I'm trying to do: I'm creating a userform to track Inventory items and prices, using checkboxes in a multipage object. The clerk checks off everything put into an order and uses a submit button, which will take some actions.
In order for the project not to require a coding person every time Inventory items change, the checkboxes are being dynamically generated when the userform is activated, from cell values on an Inventory sheet. The clerks just adjust the Inventory sheet and the form automatically adjusts for them.
This is my code to dynamically create all the checkboxes (currently this form can accommodate up to 160 possible checkboxes), in case this is effecting my issue (side note, each tab on the multipage has a frame on it, and all checkboxes are within the frame, so I could change background colors, the frame in this example being titled "frmreg"):
Sub StoreFrmRegCheckboxGenerator()
'Works with the store userform
Dim curColumn As Long
Dim LastRow As Long
Dim i As Long
Dim chkBox As msforms.CheckBox
'This sub dynamically creates checkboxes on the Regular Items tab based
'on values in Column A of the Inventory sheet
curColumn = 1 'Set your column index here
LastRow = Worksheets("Inventory").Cells(Rows.Count, curColumn).End(xlUp).Row
For i = 2 To 9
If Worksheets("Inventory").Cells(i, curColumn).Value <> "" Then
Set chkBox = store.frmreg.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
chkBox.Caption = Worksheets("Inventory").Cells(i, curColumn).Value & " - $" & Worksheets("Inventory").Cells(i, curColumn).Offset(0, 1).Value
chkBox.AutoSize = True
chkBox.WordWrap = True
chkBox.Left = 5
chkBox.Top = 1 + ((i - 1) * 25)
End If
Next i
'Cut some code out here identical to this previous section, but for the rest of the cells in column A up to Row 33, in blocks of 8
End Sub
The above code is in the Userform_Initialize sub, and it works perfectly.
However, since the number of checkboxes is not static, and can be as many as 160, I'm trying to write one sub to take the same set of actions any time any checkbox is clicked.
The closest solution I've found is from this question: Excel Macro Userform - single code handling multiple checkboxes, from sous2817.
Here is his code that I'm trying to use:
In a new class module:
Option Explicit
Public WithEvents aCheckBox As msforms.CheckBox
Private Sub aCheckBox_Click()
MsgBox aCheckBox.Name & " was clicked" & vbCrLf & vbCrLf & _
"Its Checked State is currently " & aCheckBox.Value, vbInformation + vbOKOnly, _
"Check Box # & State"
End Sub
The "store" userform, at the top, right under Option Explicit:
Dim myCheckBoxes() As clsUFCheckBox
At the bottom of the Userform_Initialize sub, AFTER I call the all the subs that dynamically create all the checkboxes:
Dim ctl As Object, pointer As Long
ReDim myCheckBoxes(1 To Me.Controls.Count)
For Each ctl In Me.Controls
If TypeName(ctl) = "CheckBox" Then
pointer = pointer + 1
Set myCheckBoxes(pointer) = New clsUFCheckBox
Set myCheckBoxes(pointer).aCheckBox = ctl
End If
Next ctl
ReDim Preserve myCheckBoxes(1 To pointer)
When I try to open the userform I get this error:
"Compile Error: User-defined type not defined"
Pointing to this line:
Dim myCheckBoxes() As clsUFCheckBox
Am I missing a library reference? I haven't been able to figure this out.
I have a macro in Powerpoint that gives me Information of a Shape. To bypass the error if no shape is selected I insert an errormask. However, this is very annoying.
Is it therefore possible to grey out the button if e.g. no Shape is selected. That way the user would npot even have a chance to click it.
Custom UI XML:
http://pastebin.com/T6NQ8WF8
Assuming you are using a 2007+ version of PowerPoint, the only way to manipulate the ribbon controls, buttons, etc., is through ribbon extensibility. It is possible to do this at run-time, with a vba hook, but it is much more difficult than in previous versions of PowerPoint where you could just use VBA to manipulate the controls' .Enabled or .Visible properties.
Here is an example of using ribbon extensibility to customize the ribbon at run-time. As you can see, it is not easy. I will show this in Option 2, below.
In this case, you have an error condition that you can easily identify using the .Type property of the Selection.ShapeRange. I think that attempting to conditionally disable this button at run-time (Option 2, below) is probably more trouble than it is worth.
Update
Is there a setting that greys your all buttons that don't have an effect.
No. The macros are the "effect", even if the result of the macro is that no action is performed. What you are asking is whether there is a setting which can compile and interpret your macros, determine whether that macro performs "an action" (e.g., manipulates a shape, changes a property assignment, etc.) and then disable buttons based on this determination. There is no such setting.
OPTION 1 -- Simply Do Not Display the MsgBox; Perform No Action if Invalid Selection
I will make some edits to clean up your code and use a better method of avoiding that error:
Sub Infos()
Dim n as String
Dim w as String
Dim h as String
Dim l as String
Dim T as String
With ActiveWindow.Selection.ShapeRange
Select Case .Type
Case 0
'MsgBox ("No shape selected.")
Exit Sub
Case Else
n = .Name
w = .Width
h = .Height
l = .Left
T = .Top
MsgBox "Name: " & n & Chr$(CharCode:=13) & "Länge: " & w & _
Chr$(CharCode:=13) & "Höhe: " & h & Chr$(CharCode:=13) & _
"Linkeposition: " & l & Chr$(CharCode:=13) & "Höhenposition: " & T
End Select
End Sub
OPTION 2 -- Use an Application Event Handler and Manipulate Ribbon at Run-Time
I mentioned that this is not easy. I uploaded an example file to Google Docs Presentation1.pptm. This should get you started. You can see now how much difficult this method is. If you are creating a PPAM/Add-In file, there are further considerations and complexities you may encounter. Good luck!
There are several errors in your code.
1. Your XML is not valid when I check in Custom UI Editor. I edited it here:
http://pastebin.com/SpG0Rtqq
2. Your Infos macro contains errors. You omit the End With statement, also, your n assignment will fail (and the rest of them will produce strange result) if the selection is multiple shapes. You can fix that by:
n = IIf(.ShapeRange.Count > 1, "Multiple shapes", .ShapeRange(1).Name)
w = IIf(.ShapeRange.Count > 1, "Multiple shapes", .ShapeRange(1).Width)
h = IIf(.ShapeRange.Count > 1, "Multiple shapes", .ShapeRange(1).Height)
l = IIf(.ShapeRange.Count > 1, "Multiple shapes", .ShapeRange(1).Left)
T = IIf(.ShapeRange.Count > 1, "Multiple shapes", .ShapeRange(1).Top)
Once you have fixed those components...
Add a module called mod_EventHandler, which includes this code. This will create an application event-handler class object, cEventClass:
Option Explicit
Public cPPTObject As New cEventClass
Public TrapFlag As Boolean
Sub TrapEvents()
'Creates an instance of the application event handler
If TrapFlag = True Then
MsgBox "Relax, my friend, the EventHandler is already active.", vbInformation + vbOKOnly, "PowerPoint Event Handler Example"
Exit Sub
End If
Set cPPTObject.PPTEvent = Application
TrapFlag = True
End Sub
Sub ReleaseTrap()
If TrapFlag = True Then
Set cPPTObject.PPTEvent = Nothing
Set cPPTObject = Nothing
TrapFlag = False
End If
End Sub
Since we need this class object, add a class module to your PowerPoint file, named cEventClass. In this module, put this code below. This code forces a refresh of the ribbon. This procedure implicitly calls the EnabledBtInfo subroutine, which then tests if the current selection is Shape(s).
Option Explicit
Public WithEvents PPTEvent As Application
Private Sub PPTEvent_WindowSelectionChange(ByVal Sel As Selection)
'Force refresh of the "btInfo" button:
RefreshRibbon "btInfo"
End Sub
And finally, another standard code module with this code to control the Button's visibility/enabled. Note that EnabledBtInfo is the VBA Hook for this button, and it tests whether Selection is shapes, before refreshing the ribbon:
Option Explicit
Public Rib As IRibbonUI
Public xmlID As String
'Callback for customUI.onLoad
Sub RibbonOnLoad(ribbon As IRibbonUI)
TrapEvents 'instantiate the event handler
Set Rib = ribbon
End Sub
Sub EnabledBtInfo(control As IRibbonControl, ByRef returnedVal)
'Check the ActiveWindow.Selection.ShapeRange
returnedVal = (ActiveWindow.Selection.Type = ppSelectionShapes)
Call RefreshRibbon(control.Id)
End Sub
Sub RefreshRibbon(Id As String)
xmlID = Id
If Rib Is Nothing Then
MsgBox "Error, Save/Restart your Presentation"
Else
Rib.Invalidate
End If
End Sub
When a shape(s) is selected, the magnifying glass icon is enabled:
When shape(s) is not selected, button is disabled:
And finally, when multiple shapes are selected:
I want to allow users to be able to paste values into TextBoxes in a userForm in VBA. You can use Ctrl-v just fine, but not everyone knows how to do that.
How do I enable copy and pasting using a right-click menu?
I realize this is an old post but I believe there is a more efficient method.
Userform Contextual Menu class code
http://www.andypope.info/vba/uf_contextualmenu.htm
There are even sample excel spreadsheets for the code examples.
The class module handles the construction of the contextual menu, the capture of right clicking in textboxes and the actual Cut. Copy and Paste actions. The class makes use of the userform's ActiveControl object. The code even handles controls within container controls such as Frames and Multipage.
The follow Initialization code, from the userform, shows how simple it is to define and use the class object. You only need declare a variable to the object and then set a reference for each textbox you want to have contextual menu capabilities. You can loop through all controls and automatically reference each textbox.
Private m_colContextMenus As Collection
Private Sub UserForm_Initialize()
Dim clsContextMenu As CTextBox_ContextMenu
Dim cTRL as Control
Set m_colContextMenus = New Collection
For Each cTRL In Me.Controls
Select Case TypeName(cTRL)
Case "TextBox"
'MsgBox cTRL.Name & ": " & Me.Controls(cTRL.Name).Value
Set clsContextMenu = New CTextBox_ContextMenu
With clsContextMenu
Set .TBox = Me.Controls(cTRL.Name)
Set .Parent = Me
End With
m_colContextMenus.Add clsContextMenu, CStr(m_colContextMenus.Count + 1)
Case Else
'MsgBox TypeName(cTRL) & ": " & cTRL.Name
End Select
Next
End Sub
Download example workbook which contains both .xls and .xlsm files
This may be of interest: http://word.mvps.org/faqs/userforms/AddRightClickMenu.htm
I would like to be able to access the document properties of a PowerPoint add-in file (a presentation saved as "PowerPoint Add-in (*.ppa)", from some VBA code in the add-in itself.
If it helps to understand the problem, what I'm actually trying to do is read a custom document property that stores the version number of the add-in, so that I can display that in a dialog box.
With Word & Excel I can do this using ThisDocument & ThisWorkbook, both of which return a reference to the document containing the running code. However, there is no ThisPresentation equivalent in PowerPoint.
For a standard PowerPoint presentation or template, I could use ActivePresentation. However, this method won't work for an add-in.
Any ideas? Please, no suggestions about where else I should stick the version number :-)
Like everyone else I expected a ThisPresentation object in PowerPoint. I thought of another way to accomplish it, without a hardcoded filename. Obviously any piece of code would need to know how to distinguish between the projects. I chose to use the projectname for this (default name "VBAProject" in the Project Explorer): it is not used for anything else, no user will change it and if it is protected they can't.
Here is my code (change MyProject into your own projectname):
Function ThisPresentation() As Presentation
Dim p As Presentation
For Each p In Presentations
If p.VBProject.Name = "MyProject" Then
Set ThisPresentation = p
Exit Function
End If
Next
End Function
REVISED FEB 2, 2010: Cleaned up answer to only show the final solution
Here's the way to do what was asked, no DLLs. Really simple:
Sub ReturnPPAasPresentation()
Dim p As Presentation
Set p = Presentations("presentation1.ppa")
Dim title As String, version As String
version = p.CustomDocumentProperties("Version").Value
title = p.BuiltInDocumentProperties("Title").Value
MsgBox "Version: " & version & " of " & title, vbOKOnly, title
End Sub
Credit goes to macnerd nerd for the general idea, but added the AddIn functionality that was requested by the OP. Unfortunately, AddIns don't have VBProject names, so not quite as robust:
Function ThisPresentation(project_name As String) As Object
Dim p As Object
all_presentations = Array(Application.AddIns, Application.Presentations)
For Each pArray In all_presentations
For Each p In pArray
Debug.Print p.FullName
If InStr(p.FullName, project_name) > 0 Then
Set ThisPresentation = p
Exit Function
End If
Next
Next
End Function