Why did application.reminder stop working? - vba

Using Outlook 365.
I needed code to bring Outlook reminders to the front of all other windows. I found this code on the Extend Office website, and entered it into the ThisOutlookSession module:
Private Sub Application_Reminder(ByVal Item As Object)
'Updated by ExtendOffice 20180814
Dim xAppointment As AppointmentItem
If Item.Class = olAppointment Then
Set xAppointment = Item
MsgBox xAppointment.Subject, 4096 + vbInformation + vbOKOnly, "Outlook reminders"
End If
End Sub
Last week, it worked fine; whenever a reminder fired, I'd see a Msgbox reiterate its message.
This week, I realized that the Msgbox was no longer popping up. I believe my computer had restarted over the weekend, but that's the only change I can think of. I've tried trapping the code with breakpoints, and as near as I can tell, it's just not running.
I'm not familiar with "ThisOutlookSession", but just to be safe I tried restarting Outlook, and it didn't help. I use two different Desktops in Windows, and I tried running Outlook on each, but that didn't seem to matter.

First of all, you need to check the Trust Center settings in Outlook whether VBA macros are allowed to run. Then try to enable macros in Outlook by following:
"File"->"Options"->"Trust Center"->"Trust Center Settings..."->"Macro Settings"->"Enable all macros"
You may find similar issues posted in the Outlook Not Running Visual Basic After Restart and VB Macro runs successfully at first, but fails after Outlook restart posts.

Related

Outlook explorer activate handler--DoEvents causes Outlook window to maximize

I have a macro that runs when an explorer is activated. I discovered that if I put a DoEvents function/statement in the macro, then any time I use another app (say, a browser) and then click back on the main Outlook (i.e., explorer) window title bar, the Outlook window maximizes, as if I had double-clicked on it.
If I comment DoEvents out, the window behaves normally.
This behavior occurs even when DoEvents is the only statement in the Activate macro.
The macro runs as expected when the Activate event occurs, but the window state changes for no apparent reason if DoEvents is present.
Is this a known issue?
Thanks!
==== EDIT =====
If I run the following code in ThisOutlookSession, the strange window behavior occurs:
Private WithEvents my_x As Explorer
Private Sub Application_Startup()
Set my_x = Application.ActiveExplorer
End Sub
Private Sub my_x_Activate()
DoEvents
End Sub
In addition, clicking once on an item in an explorer when Outlook does not have the focus causes the item to open, as if double-clicked. Plus occasional other strange behaviors.
I am using Outlook 2013 in Win10.
There is absolutely no reason to use DoEvents. Ever. You might be stealing some of the Windows messages that Outlook itself expected to handle.
First of all, I'd suggest scanning your machine for viruses.
Then I'd recommend checking the list of running add-ins in Outlook. You may try to turn them off and see how Outlook works after.
There is no need to use the DoEvents in the Activate event handler.

Office 365 Protected View Breaking Excel Marcos

Our client has updated to Microsoft Office 365 (Version 1708) and when receiving a generated Excel file (xlt) via email which contains a vb macro, it now displays "PROTECTED VIEW Be careful - email attachments can contain viruses. Unless you need to edit, it's safer to stay in Protected View." - and doesn't run the macro (of course).
Clicking the "Enable Editing" the user gets a runtime error, but in Office 2013 it works fine.
Also in Office 365 if we untick "Enable Protected View for Outlook attachments" and resend the file, it works okay.
Any ideas?
Cheers,
Steve
After many hours of hacking and googling, we found that this is an existing bug in Office 365.
A workable workaround for us was to change:
Private Sub Workbook_Open()
Call formatData
End Sub
To:
Private Sub Workbook_Activate()
Call formatData
End Sub

macros on document open. Bug in ms office 2013?

I have a macros that starts running when a document opens. It looks like this:
Private Sub Document_Open()
....
Dim strInput As String
strInput = ActiveDocument.Content
....
In office 2010 this macros works nicely, however, in office 2013 I get this error message (translation to English):
This command is not available, because there are no documents opened
And when I hit on Debug button, I see that this line of code is highligthed:
strInput = ActiveDocument.Content
I think it's a bug of office 2013, because for some insane reason it invokes this macros before the document is opened, even though I clearly force it to do right after opening - Private Sub Document_Open(). So, what is wrong with that and how can I fix that?
If you're seeing the yellow message bar then this isn't a bug. It's because your document isn't "trusted" - it's "sandboxed". You need to check whether it's a ProtectedViewWindow - introduced in Office 2010 - and take appropriate action.
The Protected View is explained in this article: https://support.office.com/en-us/article/What-is-Protected-View-d6f09ac7-e6b9-4495-8e43-2bbcdbcb6653. Basically, it prevents code from running in any document that comes from an origin / author that isn't trusted. The user needs to make an explicit decision to trust the document in order to go into Edit mode.
Obviously, this is a major impediment to VBA code, so you have the ProtectedViewWindow object in the object model. There are application-level events that can be used to manage this state in the entire Word environment, such as ProtectedViewWindowOpen that triggers when a document is opened "sandboxed". After determining whether the document is trustworthy, the code can make the document editable using the ProtectedViewWindow.Edit method.
For example, to trust all documents that come from a specific file path:
Private Sub app_ProtectedViewWindowOpen(ByVal PvWindow _
As ProtectedViewWindow)
If PvWindow.SourcePath = "C:\Test" Then
PvWindow.Edit
End If
End Sub
The above needs to be in an application-level class module and events for this class need to be active on the application. If you're not familiar with using application-level events, see https://msdn.microsoft.com/en-us/library/office/ff821218.aspx

Automatically save draft email

I'm looking for VBA code for Outlook 2013 that will automatically save a draft either when opening the composition window, when replying or forwarding an email (it's OK if it fires on newly-composed messages also) or just before sending it when hitting the Send button.
Essentially emulating hitting the "Save" icon or Ctrl-S either before composing or before the message gets sent.
Outlook generates a "Could not complete the operation because the mail provider does not support it" error when replying to or forwarding a non-Google Apps account email when Google Apps accounts are combined with other email accounts in Outlook.
I discovered that a work-around is to manually save the email before sending it. I can do that manually but often forget. Can't do it after the error message appears. Trying to save after the "could not complete" error is thrown results in another error.
BTW, I already have a routine running off the Application_ItemSend event; will adding another conflict?
Update: Here is a version of what I tried:
Private Sub Application_ItemSend(ByVal item As Object, Cancel As Boolean)
' Runs when the item is being sent.
Call AutoSaveDraft(item)
Call CheckSubject(item)
End Sub
(CheckSubject fires)
Private Sub AutoSaveDraft(item As Object)
item.Save
SendKeys "+{F12}" ' Save the message. ##
SendKeys "^S"
Sleep 1000 ' Pause execution 1 second. The Windows sleep function is called in the beginning ##
End Sub
(Yes, I have all three potential save methods. Have also tried them individually.
You are free to call the Save method in the ItemSend event handler if it helps to avoid issues.
You may find the Getting Started with VBA in Outlook 2010 article helpful.
Since the original poster doesn't make it clear, this is what actually works to prevent the errors related to non-Google Apps accounts when simply hitting Send:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Item.Save
End Sub

Problem Getting Outlook 2007 Running VBA Script

I'm trying to get Outlook to save the attachment in a daily email to a folder where I can have a file system watcher ready to parse and analyze the attachment (it's the report of a data integrity checker). I've set up a Rule that is supposed to run a VBA script, but it just doesn't run as far as I can tell. I've verified in VB6 that the code will in fact save some text to a file, so if Outlook actually runs the VBA script it should be able to do the same. But it doesn't! Can anyone see what the heck I'm doing wrong?
Dim WithEvents objInbox As Outlook.Items
Private Sub Application_Startup()
Set objInbox = Session.GetDefaultFolder(olFolderInbox).Items
End Sub
Sub SnagAttachment(theItem As MailItem)
On Error Resume Next
Dim fnum As Integer
fnum = FreeFile()
Open "c:\temp\success.txt" For Output As #fnum
Print #fnum, "Ran SnagAttachment Successfully"
Close #fnum
End Sub
Note that when I use the Rules wizard, and choose "run a script" the Sub SnagAttachment is listed as a script that can be selected.
How would you know if it's working or not when you put On Error Resume Next at the top of the procedure? You would never find out.
Here are the rules for creating a script that should be run as part of a Rule:
How to create a script for the Rules Wizard in Outlook
Also note the caveat found at How to process incoming messages in Microsoft Outlook:
A "run a script" rule is not a good choice for heavy traffic
applications, as Outlook is likely to skip applying the rule if too
many items arrive that meet the rule's conditions.
In order to get the script to work you need to change the security settings in Outlook. Go to Tools > Macro > Security and change it to "Warnings for all macros". Then restart Outlook.
Hope this helps
Try isolating the exact problem:
Check macro security settings. At maximum, it must be set no higher than "Warnings for all macros".
Try creating a new module with a single test sub:
Sub Test(Item as Outlook.MailItem)
MsgBox "test"
End Sub
Then set up a new rule to handle _all_ incoming messages running this sub as the only action, and temporarily disable all other rules. Then send a message to yourself. If you get no popup box as a result, this may be an indication of a bad Outlook install. Try reinstalling it, or calling MS up directly for support.
If the previous test was successful, try working with the `Scripting.FileSystemObject` object instead of `Freefile()` to create and populate files. This is just to test if there's some odd bug you're encountering here. Worth a shot, right?
Make sure your rule conditions are set correctly. There could be a glitch or misspelling in a condition which just drops all messages you want this script to run on.
I was experiencing the same issue, and it seems to me that if there is an error in your code, the script will not even start. This is as opposed to standard VBA where the debugger pops up for runtime errors. For example, I had a label at the end of my function that was missing the colon after it. This caused the script to not run at all (As opposed to running up to this line and then failing). I would suggest commenting out all of your code and starting with just a msgbox "hello world". I would leave this in your code as you debug it to know the code is running, but you will probably have to dismiss the message box many times. Iteratively add back lines of code until you discover where the problem is.