Run Worksheet_Open code with a userform active - vba

My program calls 7 subroutines in the workbook_open event. I want to show a progress bar (with a 'Done' button when the progress is 100%) but when I show the progress bar, it doesn't run the workbook_open event anymore until I close the userform, then it goes on running. Is there a way to show a userform with the code running in the background?
Private Sub Workbook_Open()
frmProgress.Show
'Here it stops running the code
frmProgress.lblTask = "Updating pivottables..."
Call updatePivots
ufProgress.Label1.BackColor = &H8000000D
frmProgress.lblTask = "Updating data..."
Call updateData
ufProgress.Label2.BackColor = &H8000000D
'Some More code
frmProgress.lblTask = "Done!"
End Sub
I know the changing of the colors and the multiple labels are not efficient, I'm working on that.

You should put the code in the UseForm Activate event handler which would trigger on Workbook Open event handler :)

Related

How can I cancel a button's action on an Access form if the button is double clicked?

I have a MS Access database that has a switchboard on it that opens different forms within the database. For some reason, some of the users like to double click the buttons that open the different forms. This is a problem because one of the forms that opens happens to have a checkbox right where the button that opens it is, so when they double click the button to open the form, it causes the checkbox to toggle and change it's value. It's a constant problem.
I tried adding a double click event handler that would essentially do nothing, but it's not firing and the form still opens and the checkbox keeps getting toggled.
I've tried user education, I've tried telling them that it's causing problems, but they don't seem to get it. Is it possible to only let a button work when it's clicked one time, and if it's clicked twice it just doesn't work, or it throws an error at them to shame them?
One way to solve this is:
When the second forms opens set the following properties in Form_Open:
Private Sub Form_Open(Cancel As Integer)
Me.AllowEdits = False ' Will prevent form from being edited
Me.TimerInterval = 500 ' Will trigger Form_Timer after 500 miliseconds
End Sub
and in the Form_Timer event (kicks in after 500 miliseconds)
Private Sub Form_Timer()
Me.AllowEdits = True ' Allow edit again
Me.TimerInterval = 0 ' Stop timer
End Sub
With this the second form will not be possible to edit the first half a second while the second click comes.
You can try this code under the double click event of the button
Private Sub Command1_DblClick(Cancel As Integer)
Cancel = True
End Sub
In my case the command button name is Command1

Excel Add-In Context Menu Buttons

I have an Excel Add-In that I have created ( .xlam file ) I am trying to have this add-in create buttons on the right click menu so that I can quickly run a macro based on the selected cell. I added code to ThisWorkbook, and it works in the xlsm file. (I copied the code from HERE.) I SaveAs to an xlam, load the add-in, and I get nothing on the context menu. I have a feeling these subs do not load through the add-in. However, I can get code in Workbook_Open to function. Can someone point me in the right direction?
Private Sub Workbook_Deactivate()
On Error Resume Next
With Application
.CommandBars("Cell").Controls("Open Drawing").Delete
End With
On Error GoTo 0
End Sub
Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Dim cmdBtn As CommandBarButton
On Error Resume Next
With Application
.CommandBars("Cell").Controls("Open Drawing").Delete
Set cmdBtn = .CommandBars("Cell").Controls.Add(Temporary:=True)
End With
With cmdBtn
.Caption = "Open Drawing"
.Style = msoButtonCaption
.OnAction = "Open_Drawing_Main"
End With
On Error GoTo 0
End Sub
You have to use AddinInstall event. I assume that your context menu procedure works fine. So double click on ThisWorkbook in the add-in file (before you install it and save it as xlam), select AddinInstall event from the upper write dropdown menu and place some code like this:
Private Sub Workbook_AddinInstall()
Call AddToRightClickMenuOptions_Main
End Sub
in this code AddToRightClickMenuOptions_Main is the sub that creates the context menu. Note that you may want to remove the context menu when you uninstall the add-in:
Private Sub Workbook_AddinUninstall()
Call DeleteFromRightClickMenuOptions_Main
End Sub
where DeleteFromRightClickMenuOptions_Main is the routine that removes the context menu (you can find the code easily on the internet)
An Excel Add-in (at least, the ones I have created) still have their own workbook. The Workbook_SheetBeforeRightClick and Workbook_Deactivate will run when the .xlam's workbook is either right-clicked or deactivated. If you want the code to run only for when the selected workbook is right-clicked or deactivated, you'll have to have the code in that specific workbook's code.
However
Since you are trying to get this to work with an add-in, I'm going to go through a few relevant points since once an Add-in is loaded, it (and it's features) are accessible from all other open workbooks.
If the option is added to the context menu whenever a page is right-clicked in any book (which it would (as coded, if it worked)), removing the context menu option when any workbook is deactivated won't have any visible effect, as the next time a right-click happens the option will once again be visible.
Is there a reason why the context menu can't just be added when the add-in is opened (add-in's Workbook_Open), and removed when it's closed(add-in's Workbook_BeforeClose)? (As this will visibly have the same effect as the above code.)

PowerPoint displays TWO MsgBox concurrently

Weird behavior when loading an addin from the Developer | AddIns ribbon...
Observed the add-in's Auto_Open procedure fires first, but WHILE that message box is displayed (which should under normal circumstances pause execution awaiting user input -- dismissal of the MsgBox), the ribbon's onLoad event also fires (apparently upon clicking anywhere in the first message box), displaying another message box concurrently!
Have you ever seen TWO message boxes displayed at the same time? These are not userforms displayed modelessly, they are simple MsgBox prompts.
What could explain this behavior? And what can prevent it?
Oddly enough, once the Add-in is loaded, the events fire with what I would consider "normal" sequential order: first the Auto_open, and then the onLoad, but only after you have dismissed the first msgbox.
Ribbon OnLoad procedure:
'This procedure hooks to the ChartBuilder_PPT Add-in menu
Option Private Module
Option Explicit
Private rib As IRibbonUI
''Callback for customUI.onLoad
Sub RibbonOnLoad(ribbon As IRibbonUI)
MsgBox "RibbonOnLoad"
Set rib = ribbon
'Call UPDATE(rib)
End Sub
In another module,
Option Explicit
Const AddInName As String = "ChartBuilder_PPT.ppam"
Const ShortName As String = "ChartBuilder_PPT"
Sub Auto_Open()
MsgBox "Auto_Open"
Call UnloadCB
End Sub
Sub Auto_Close()
Call UnloadCB
End Sub
Sub UnloadCB()
If AddInExists Then
Application.AddIns(ShortName).Loaded = msoFalse
Else
End If
Function AddInExists() As Boolean
Dim a As AddIn
For Each a In Application.AddIns
If a.Name = ShortName Then
AddInExists = True
a.AutoLoad = msoCTrue
Exit For
End If
Next
End Function

Calling a macro from a button in edit mode while in PowerPoint

I'm trying to write a vba macro that can be called in edit-mode in PowerPoint 2007-2010.
I can easily add a Command Button to a presentation. However, this button can only be clicked to trigger the vba macro while in slideshow mode.
However, what I would like to do is have this button trigger the associated vba macro while in edit mode. Clicking on it in edit mode allows me to change its size etc, but it doesn't call the macro.
In Excel on the other hand, I get exactly the expected behaviour when I insert a button -> clicking on it calls the vba action.
So how can I create a button (or other element that acts the same way) that calls a vba macro during edit view in PowerPoint. The only way I can think of is using a ribbon action, however this is unpractical in this case, because the macro will modify a shape that is associated with the button and there might be several of these shapes per slide that should each have their own button.
The only way I can think of is using a ribbon action, however this is unpractical in this case, because the macro will modify a shape that is associated with the button and there might be several of these shapes per slide that should each have their own button.
Depending on what you're trying to do, a ribbon button that launches a macro might be quite practical. The macro could operate on the current selection (and test the current selection to ensure that it's something appropriate).
With ActiveWindow.Selection.ShapeRange
' operate on the currently selected shapes
End with
just answer it a some where else also
it is possible to do so all you need is download this file
http://www.officeoneonline.com/eventgen/EventGen20.zip
install it
create a class module
paste this code
Option Explicit
Public WithEvents PPTEvent As Application
Private Sub Class_Initialize()
End Sub
Private Sub PPTEvent_WindowSelectionChange(ByVal Sel As Selection)
If Sel.Type = ppSelectionShapes Then
If Sel.ShapeRange.HasTextFrame Then
If Sel.ShapeRange.TextFrame.HasText Then
If Trim(Sel.ShapeRange.TextFrame.TextRange.Text) = "Text inside your shape" Then
Sel.Unselect
yoursub
End If
End If
End If
End If
End Sub
insert a new module
paste this code
Dim cPPTObject As New Class1
Dim TrapFlag As Boolean
Sub TrapEvents()
If TrapFlag = True Then
MsgBox "Already Working"
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
Sub yoursub()
MsgBox "Your Sub is working"
End Sub
Now run TrapEvents and whenver you will click shape with that text in it your sub will run
Credits to the person who wrote this http://www.officeoneonline.com/eventgen/eventgen.html

Where to cancel Application.OnTime in VBA

Using VBA in Excel 2003, I'm trying to cancel an Application.OnTime event using the following code:
Application.OnTime EarliestTime:=varNextRunTime, Procedure:="SomeMethod", Schedule:=False
where varNextRunTime is a global variable containing the next time it is due to run. This code runs in the Workbook_BeforeClose event handler so is always run when the workbook is closed, which is my intention.
However, if the user tries to close the workbook, but changes their mind and hits the cancel button when prompted to Save (Yes, No, Cancel), the Application.OnTime event is still cancelled. BeforeClose is always run before they decide to hit cancel, so has anyone got any ideas how I can only cancel the Application.OnTime event when the workbook is closed?
Here's some ideas
http://www.dailydoseofexcel.com/archives/2004/06/16/beforeclose-vs-beforereallyclose/
Check the Saved property of the Workbook in your event handler. If the workbook is unsaved then display your own dialog to find out if the users wants to save changes, not save changes or cancel.
Here's some rough code. Obviously uncomment the line which deals with the Application.OnTime part and change the MsgBox title to something suitable
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim response As Integer
If Not (Me.Saved) Then
response = MsgBox("Do you want to save changes to '" & Me.Name & "'?", vbYesNoCancel, "put title here")
If (response = vbCancel) Then
Cancel = True
ElseIf (response = vbYes) Then
Me.Save
End If
End If
If Not (Cancel) Then
' Application.OnTime EarliestTime:=varNextRunTime, Procedure:="SomeMethod", Schedule:=False
End If
End Sub
Investigate using:
Application.Quit
If you find this command results in the Excel program remaining open although the document has closed, you may want to follow with
ActiveWorkbook.Close False
I'm not in position to test this or give more insights, unfortunately.
A bit late to the show but here is a simple solution that I've come across (and tested):
If a user deactivates the workbook by closing it, the workbook will still remain the ActiveWorkbook when the Workbook_WindowDeactivate event fires. If the user deactivates the workbook by switching to another workbook, then the new workbook will become the ActiveWorkbook by the time Workbook_WindowDeactivate fires. You can use this behavior to determine the action that caused the event to fire:
Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
If Application.ActiveWorkbook.Name = Me.Name Then
'Your code here
End If
End Sub