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.
Related
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.
I have an intermittent updating problem with an outlook mailitem in the explorer mode on a public folder of an exchange server. The subject (or perhaps any other part as well) of a mailitem does not seem to update itself at times.
Specifically, the subject of the mailitem shown in the explorer mode is dissimilar to that shown in the inspector mode. In other words, the explorer mode is old, therefore needs updating or synchronization.
Hitting F9 (send/receive) to update the public folder does not seem to help.
I would like to know if this anomaly can be detected (and perhaps perform manual updates/synchronization) using an outlook macro.
Any ideas are welcome.
Some properties, factors unknown, update on saving only.
The subject of the mail in the explorer view should change when you manually close the item, if you agree to save changes. Should be the same if you manually saved regularly.
If you want more than a simple save:
Option Explicit
Sub MarkInUseSaveCurrentItem()
Dim currItem As Object
Set currItem = ActiveInspector.currentItem
If InStr(LCase(currItem.subject), LCase("Barok is working on this")) = 0 Then
currItem.subject = "Barok is working on this. To avoid conflicts do not open. " & currItem.subject
End If
currItem.Save
End Sub
I have a macro in Excel. Part of that macro opens up other workbooks using Workbooks.Open(Filepath).
Some of the workbooks I'm opening have (badly done) VBA code inside of them, that try to then calculate a UDF and fail horribly.
Without resorting to an On Error Resume Next or an On Error GoTo, how do I say "Open this file, DO NOT RUN ANY CODE".
Trying to avoid letting the code execute in the first place due to possible security concerns. I feel comfortable enough opening these files, and there's a decent chance that they're not going to be compromised, but breaches can happen, and why have a hole when I don't need one? I also don't want error messages interrupting my code executing.
Searching the web just shows me shift+open opens the file without executing code, which is not at all what I'm looking to do. I'm looking for the equivalent VBA method.
AutomationSecurity is likely what you want:
https://learn.microsoft.com/en-us/office/vba/api/Excel.Application.AutomationSecurity
MsoAutomationSecurity can be one of these MsoAutomationSecurity
constants.
msoAutomationSecurityByUI . Uses the security setting specified in the
Security dialog box.
msoAutomationSecurityForceDisable . Disables all
macros in all files opened programmatically without showing any
security alerts. Note This setting does not disable Microsoft Excel
4.0 macros. If a file that contains Microsoft Excel 4.0 macros is opened programmatically, the user will be prompted to decide whether
or not to open the file.
msoAutomationSecurityLow . Enables all
macros. This is the default value when the application is started.
You can disable the macros for newly opened files, open the workbook, and then re-enable the macros:
Private Sub OpenWorkBookMacroDisabled(wbPath As String)
Application.AutomationSecurity = msoAutomationSecurityForceDisable
Workbooks.Open (wbPath)
Application.AutomationSecurity = msoAutomationSecurityByUI
End Sub
I currently have a form with a couple of buttons, text boxes and a AcroPDF. AcroPDF is an additional control I have added to my toolbox and I get it from "Adobe PDF Reader".
This macro is being used on various computers and I have found out that, for some reason, the macro does not work on all computers. But when I delete the AcroPDF control from the form, it works for all computers. For the computers that do get an error, it happens when I first open the file I automatically get an error msg and the form does not automatically show up (like how I programmed it to).
Is there a way I can program this into my "Thisworkbook.open" so that if there is an error, I can somehow delete or disable this additional control? something similar to how you are able to turn on and off references? (see below)
ThisWorkbook.VBProject.References.AddFromFile ("libraryName")
UPDATED: the error message I get from the computers that don't work is the following:
system error &H80004005 (-2147467259). unspecified error
Thanks.
UserForm.Controls.Remove -- but this only works for controls added at runtime (dynamically). You cannot use the Remove method on a control that was added from the Designer.
Probably you need to reverse this logic, and only add the control when the reference exists on the user's machine.
Dim ctrl
If Len(Dir("libraryname")) <> 0 Then
Set ctrl = UserForm1.Controls.Add ...
End If
You would also then want to use the Remove method when closing the form, that way the form remains in a state that could be opened on either computer.
HOWEVER, the error message you get on the other machines may shed additional light on the source of the problem. Depending on what the error is, there may be other solutions that don't involve removing the control.
If you don't mind creating a duplicate form without the AcroPDF control you might be able to use labels and the GoTo statement.
Sub Workbook_Open()
' do stuff
On Error GoTo AcroPDFError
ThisWorkbook.VBProject.References.AddFromFile ("libraryName")
' load your form with the AcroPDF control here.
Continue:
On Error GoTo 0
' the rest of your Workbook_Open sub is here
Exit Sub
AcroPDFError:
' load your form without the AcroPDF control here.
GoTo Continue
End Sub
I am trying to open a .bat file from outlook VBA. My code worked until this last week.
Here is the Code:
Sub Daily_batch_file(Mail As MailItem)
Call Shell("P:\my path\my bat.bat", vbNormalFocus)
End Sub
I have a rule that kicks this script off when I receive a particular email. I am getting an invalid call error but haven't been able to figure it out. Any suggestions?
Instead of call .bat file from VBA, maybe you can open it using Rule from outlook. It's pretty simple.
Just create a blank rule with the specific word in the subject/body (or you can choose any condition based on your situation). And then in the Select Action window, tick the "Start Application" and then find your .bat/.exe file.
I hope it's clear enough.
Thanks