Save File Macro Working Sporadically

Status
Not open for further replies.

Matt Johnson

Member
Outlook version
Outlook 2013 64 bit
Email Account
Office 365 Exchange
I've used your macro for saving a file as pdf and it works great... buuut... only when I'm forwarding the message to myself. So an email will come in and the rule doesn't run. But then I forward the email to myself and all of a sudden the rule runs. Also I can run the rule on past messages no problem. Here's a screen shot of a sample rule.
upload_2015-1-23_15-46-2.png

Here is some code:
Code:
Sub SaveAttachmentsWilson(Item As Outlook.MailItem)
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String
    ' Get the path to your My Documents folder
    strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
    On Error Resume Next
    ' Instantiate an Outlook Application object.
    Set objOL = CreateObject("Outlook.Application")
    ' Get the collection of selected objects.
    Set objSelection = objOL.ActiveExplorer.Selection
' The attachment folder needs to exist
' You can change this to another folder name of your choice
    ' Set the Attachment folder.
    strFolderpath = "D:\Google Drive\Dropbox - Wilsonville\"
    ' Check each selected item for attachments.
    For Each objMsg In objSelection
    Set objAttachments = objMsg.Attachments
    lngCount = objAttachments.Count
        
    If lngCount > 0 Then
    
    ' Use a count down loop for removing items
    ' from a collection. Otherwise, the loop counter gets
    ' confused and only every other item is removed.
    
    For i = lngCount To 1 Step -1
    
    ' Get the file name.
    strFile = objAttachments.Item(i).FileName
    
    ' Combine with the path to the Temp folder.
    strFile = strFolderpath & strFile
    
    ' Save the attachment as a file.
    objAttachments.Item(i).SaveAsFile strFile
    
    Next i
    End If
    
    Next
    
ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub
 

Attachments

  • upload_2015-1-23_15-45-3.png
    upload_2015-1-23_15-45-3.png
    9.8 KB · Views: 398
  • upload_2015-1-23_15-45-30.png
    upload_2015-1-23_15-45-30.png
    7.7 KB · Views: 412

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
First I'd add
Code:
debug.print now
after the first line to see whether the script is called or not. When it's been called, you see the date & time in the debug window (ctrl+g)
 

Matt Johnson

Member
Outlook version
Outlook 2013 64 bit
Email Account
Office 365 Exchange
Doesn't look like its running, Although its not running at all anymore even if I run rules manually but I know at some point it was.
 

Matt Johnson

Member
Outlook version
Outlook 2013 64 bit
Email Account
Office 365 Exchange
It is running the rule though. I added that it flag the message with high importance and it does this okay. It just doesn't run the script.
 

Matt Johnson

Member
Outlook version
Outlook 2013 64 bit
Email Account
Office 365 Exchange
Maybe it has something to do with the way my project is configured?
upload_2015-1-24_12-33-49.png
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Run-a-scripts can be in a module, so that is not the problem. It works sometimes then quits? Do you get a lot of mail? you might be getting more than the rule can process. An itemadd macro might be better if this is the case.

actually, it looks like it is written to work with selected messages - changing it so it only works with the rule might work better.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
something like this is all you need for a run a script - i removed the selection code but didn't test it, so i might have missed something.

Code:
Sub SaveAttachmentsWilson(Item As Outlook.MailItem)
Dim objOL As Outlook.Application
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String
    ' Get the path to your My Documents folder
    strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
    On Error Resume Next

' The attachment folder needs to exist
' You can change this to another folder name of your choice
    ' Set the Attachment folder.
    strFolderpath = "D:\Google Drive\Dropbox - Wilsonville\"
    ' Check each selected item for attachments.


    Set objAttachments = item.Attachments
    lngCount = objAttachments.Count
       
    If lngCount > 0 Then
   
    ' Use a count down loop for removing items
    ' from a collection. Otherwise, the loop counter gets
    ' confused and only every other item is removed.
   
    For i = lngCount To 1 Step -1
   
    ' Get the file name.
    strFile = objAttachments.Item(i).FileName
   
    ' Combine with the path to the Temp folder.
    strFile = strFolderpath & strFile
   
    ' Save the attachment as a file.
    objAttachments.Item(i).SaveAsFile strFile
   
    Next i
    End If
   
    Next
   
ExitSub:
Set objAttachments = Nothing
End Sub
 

Matt Johnson

Member
Outlook version
Outlook 2013 64 bit
Email Account
Office 365 Exchange
Still struggling. I tried your code. As far as it running sometimes, it used to run, maybe I'll put it that way. But for a long time now it hasn't been running. I tried adding the debug code mentioned above and that doesn't seem to run. So its like the script just isn't running. I don't think I get that much email.
[DOUBLEPOST=1422204949][/DOUBLEPOST]Okay it appears to be working after an outlook restart. Does outlook need to be restarted each time you make changes to VB code?
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
No, this one doesn't require a restart.

An application_startup would require restarting, but you can kick start it by clicking in the startup macro - but this isn't a startup macro.
 

Matt Johnson

Member
Outlook version
Outlook 2013 64 bit
Email Account
Office 365 Exchange
Hmmm, not sure how I fixed it but some change did. However, it still won't run on messages automatically. It works great when I do a "Run Rules Now" but doesn't not apply itself to new messages. Any thoughts?
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
Q A Macro for 'File - Save.As' on multiple selections Outlook VBA and Custom Forms 1
N Save Selected Email Message as .msg File Outlook VBA and Custom Forms 12
S save attachment with date & time mentioned inside the file Outlook VBA and Custom Forms 0
P Outlook pst file is too huge with POP3. How to save more space? Using Outlook 4
S automate save the .xlxs file to share Network Using Outlook 1
R VBA | Chosing path to save file Outlook VBA and Custom Forms 1
Diane Poremsky Save Selected Email Message as .msg File Using Outlook 11
Diane Poremsky Filter and Save Contacts to a CSV File Using Outlook 0
Diane Poremsky Filter and Save Contacts to a CSV File Using Outlook 0
M Save selected email message as .msg file (with user to choose folder location) Outlook VBA and Custom Forms 14
moron save as & file location dialog box popup Outlook VBA and Custom Forms 2
R Outlook 2013 VB rule to auto save attachments with different file types Outlook VBA and Custom Forms 5
D Save pst and attachments as a PDF File Outlook VBA and Custom Forms 3
K Ask about a simple script about save a incoming mail as file on local PC Outlook VBA and Custom Forms 3
A File - Save Attachments does nothing in Outlook 2003 with Exchange 2010 ... Using Outlook 3
A Dialogue box 'Do you want to save this file?' Save or Cancel From: C:\Users\Ad Using Outlook 5
M Save incoming Email as .html file with name of subject line Outlook VBA and Custom Forms 3
H Program email save as text file Outlook VBA and Custom Forms 5
E Auto Save Zip File Attachment to Network Folder Outlook VBA and Custom Forms 1
P Save Email as .msg File with Code Outlook VBA and Custom Forms 5
T How can i save the body as text file Outlook VBA and Custom Forms 1
B Save e-mail to .msg file Outlook VBA and Custom Forms 4
D VBA Macro to Print and Save email to network location Outlook VBA and Custom Forms 1
N VBA Macro To Save Emails Outlook VBA and Custom Forms 1
N Save emails within a certain date range to network drive Outlook VBA and Custom Forms 0
T Outlook 365 Move newly created tasks automatically on save. Outlook VBA and Custom Forms 1
G Save attachment run a script rule Outlook VBA and Custom Forms 0
G Save and Rename Outlook Email Attachments Outlook VBA and Custom Forms 0
G VBA to save selected Outlook msg with new name in selected network Windows folder Outlook VBA and Custom Forms 1
D Outlook 2016 64bit, Cannot Save in 'HTML', format Using Outlook 1
N Save selected messages VBA does not save replies and/or messages that contain : in subject Outlook VBA and Custom Forms 1
L Macro to add Date & Time etc to "drag to save" e-mails Outlook VBA and Custom Forms 17
S Add VBA save code Using Outlook 0
A Edit attachment Save and Reply Outlook VBA and Custom Forms 0
S Outlook (2016 32bit; Gmail IMAP) - Save sent message to Outllook Folder Outlook VBA and Custom Forms 0
D Prevent popup of "Do you want to save changes?" when closing after opening an appointment to view Outlook VBA and Custom Forms 2
A Unable to save recurring Meeting to Documents folder due to error Using Outlook 2
M Outlook 2013 Script Assistance - Save Opened Link with Subject Added Outlook VBA and Custom Forms 1
R Use an ItemAdd to Save Attachments on Arrival Outlook VBA and Custom Forms 0
W Outlook Calendar does not save view any longer! Using Outlook 3
S save email from excel Outlook VBA and Custom Forms 1
Y Open and Save Hyperlink Files in multiple emails Outlook VBA and Custom Forms 9
9 Outlook 2016 How to save an Outlook attachment to a specific folder then delete the email it came from? Using Outlook 1
O Save attachments using hotkey without changing attributes Outlook VBA and Custom Forms 1
geofferyh Cannot get Macro to SAVE more than one message attachment??? Outlook VBA and Custom Forms 5
N Open & Save VBAProject.Otm using VBA Code Outlook VBA and Custom Forms 1
W Save and rename outlook email attachments to include domain name & date received Outlook VBA and Custom Forms 4
V Change default default save location to Quick Access Using Outlook 1
W Save Outlook attachment in network folder and rename to current date and time Outlook VBA and Custom Forms 18
C Change default "Save Sent Item To" folder Outlook VBA and Custom Forms 9

Similar threads

Top