Make the Userform in Outlook stay on the screen - VBA - vba

So I have created a VBA Macro embedded withing Outlook. The code runs a userform.
When I run my macro:
Currently:
When I minimize Outlook, my userform also minimizes.
What I Want:
When I minimize outlook I want my userform to stay on the screen.
Any ideas on how to achieve this? I am using vbModeless to display my userform, as i want the user to access the outlook content while the userform is running.

Try adding this to your code.
Private Sub UserForm_Initialize()
Dim olapp As Object
Set olapp = GetObject(, "Outlook.Application")
olapp.ActiveWindow.WindowState = 1
End Sub
This will minimize Outlook the moment userform is shown.
Not exactly what you want (what you describe is a bit complicated), but nearly the same effect.

Related

VBA : Detect a change of Activeworkbook

I have a XLA AddIn macro with a main menu Userform. I need to enable/disable boutons on this menu userform according to the Activeworkbook.
My problem is that I don't know how to update the Userform when the user change or close the Activeworkbook.
I do have a UserForm_Activate, which update the Userform. But that event is not triggered when the user close the Activeworkbook then click on the Userform.
I would need to update the Userform either :
as soon as the Activeworkbook is changed or closed
or as soon as the user reach the Userform (ie. before he can click on any control).
How would you proceed ?
In the ThisWorkbook module of your xla:
Private WithEvents xlApp as Excel.Application
You will then see that xlApp has events such as WorkbookActivate and WorkbookDeactivate etc.

Excel Opened Documents History Log - AddIn

Apologies in advance if this ends up being generic. I have done some research on this and drawn a complete blank.
Excel is great, I love Excel. So much so that the "Recent Documents" section is of almost no use to me as I use that many spreadsheets in an insane amount of locations.
I have been researching a way to log (using VBA as an AddIn) documents when they are opened. Even if it is into something as simple as a text file with the date, however I cannot figure out how to have the VBA code "know" when a file is opened (which, along with outputting to a Text file, is all I want it to do at this stage).
Is there a way to have the VBA look for this action from within an excel instance?
The following steps (adapted from the excellent post at http://www.cpearson.com/excel/AppEvent.aspx ) is the "minimally viable" way to do what you need.
open a new workbook
open the VB editor
Insert a class module; in the properties window, set class name to CExcelEvents
Add the following code in the class module:
Private WithEvents App As Application
Private Sub Class_Initialize()
Set App = Application
MsgBox "initialized the class!"
End Sub
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
MsgBox "New Workbook was opened: " & Wb.Name
End Sub
5. Right-click on the "ThisWorkbook" element in the project explorer, and select "View Code"
6. Add the following code:
Private XLApp As CExcelEvents
Private Sub Workbook_Open()
Set XLApp = New CExcelEvents
End Sub
This creates an instance of the CExcelEvents class, and "turns on event handling" when the addIn is loaded.
Finally, save the file as myEvents.xlam in the location where addIns are stored - this varies depending on your machine...
If you now close the file, and add the addIn (again, depends on your environment whether that is from the Developer ribbon or the Tools menu), you should see a dialog box that says "initialized the class!". This shows the addIn is properly installed and working.
Now, when you open a workbook, another message box will appear: "New Workbook was opened: " with the name.
Obviously you will want to get rid of the message boxes, and put in some "useful" code that does whatever you want to do (for example, log the name of the workbook to a file). It sounds to me like you don't need help with the latter - if I am wrong then please let me know.

Control already created Outlook message from excel VBA

longtime reader, first time messenger (not my first time making a bad joke though),
I would like to know if it is possible to gain control of an Outlook email message that has already been created. At work we have to download new work orders from a secure website, thanks mostly to this site, I have been able to set up a macro that logs in, finds the new work orders, and clicks the button to open the work order. Once this button is clicked, a new IE window is opened with a pdf file, and the "Send page by email" command is used to create a new outlook message. I have the outlook 12 reference (using Office 2007), and am able to take control of an existing outlook session to create a new email using:
Dim SendOrder As Outlook.Application
Set SendOrder = GetObject(, "Outlook.Application")
But I cannot figure out how to make it control the email message that was opened by IE. I tried using GetObject(, "Outlook.Application.MailItem), and a few other failed ideas, but 3 ideas were all I had, so I'm hoping someone on here can help me out with this, otherwise I'll probably have to save the file in IE and create a new email message, which seems like adding an extra step.
You're on the right path, I think. Something like this works with Outlook mailItems opened from Outlook. I have not tested it on mailItems opened from IE, though.
Sub GetAMailItem()
'## Requires reference to MS Outlook object library ##
Dim oApp As Outlook.Application
Dim mItem As MailItem
Set oApp = GetObject(, "Outlook.Application")
If TypeName(oApp.ActiveWindow) = "Inspector" Then
Set mItem = oApp.ActiveWindow.CurrentItem
End If
Set oApp = Nothing
End Sub
Found the guts of that code here, just made a modification or two to give you a structured example that might suit your needs.

How to add MS outlook reminders event handlers with VBA

I want to extend MS Outlook so that when a calendar reminder pops up, I can run a VBA hook that can run an external program (like a batch script). In my case, I want to "forward" the reminder to a Linux desktop since I work in both environments and I don't always have the Windows desktop visible.
I see an example at http://office.microsoft.com/en-us/outlook-help/HV080803406.aspx and have opened VBA Developer view in MS outlook 2010 and inserted a class module and added that VBA code, but I do not see how to activate this code - when a reminder pops up, this code is not activated.
Update
Here is what I ended up adding to Outlook's ThisOutlookSession to run an external batch script when a reminder pops up.
Public WithEvents objReminders As Outlook.Reminders
Private Sub Application_Startup()
Set objReminders = Application.Reminders
End Sub
Private Sub objReminders_ReminderFire(ByVal ReminderObject As Reminder)
Cmd = "C:\path\to\my\reminder-hook.cmd" & " " & ReminderObject.Caption
Call Shell(Cmd, vbHide)
End Sub
Put it in the "ThisOutlookSession" module and restart Outlook.
Also, ensure that macros are enabled in Outlook settings.

Is it possible to run a VBA function whenever Outlook starts up?

I would like to have a VBA function run as soon as Outlook starts up. Is this possible and if so, what do I need to do? My searches on Google have failed me.
I don't mind that the security alert will pop up.
Use the Application_Startup event in ThisOutlookSession:
Private Sub Application_Startup()
MsgBox "Foo"
End Sub
Have you looked at the Application_Startup() event?