i'm trying to solve an issue that is popping up when running a code to save attachments to a local folder. Below the error message, which translated is saying: Error -2147024891 (80070005) during execution: Cannot save the file. You are not authorized for this folder.
error message
When looking for the error this following appears:
[error message 2][2]
The funny thing is, until a certain point last month i was able to run the code and as far as i know there was no change in authorization. Does anyone have a clue where i can find the solution?
Are you sure the folder specified in the Excel worksheet exists on the disk?
In your code it is not clear what is the actual file path is passed to the SaveAsFile method. First of all, I'd recommend making sure a valid file path is provided as a parameter in the code, for example:
Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat
saveFolder = "C:\Users\USERNAME\Saved\"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
Related
I have a VB script that is triggered by a rule that saves the email's attachment to a specific folder. The rule runs ok for a period and then I receive the Outlook error message: "Unexpected error has occurred" and the rule is switched off.
Whilst I can't definitively say, from observations it seems that the error is more likely to occur when I receive more than one of these emails in the same send/receive batch. The attachments are usually 2MB to 5MB in size, and at times the Email Subject lines & file names on each of the consecutive emails' attachments can be the same. The same script on another rule which is usually only triggered once daily doesn't throw the error and continues working without interruption. This is occurring on multiple machines - so I deduce this might be related to the way Outlook handles these specific emails.
I don't know how to debug this situation and more frustratingly I haven't been able to find a post with a similar problem from my web searches.
Would appreciate any hints on what might be going on here... or even how you would approach debugging?
Public Sub SaveAttachmentsToDisk(MItem As Outlook.MailItem)
Dim oAttachment As Outlook.Attachment
Dim sSaveFolder As String
sSaveFolder = "C:\RawFiles\"
For Each oAttachment In MItem.Attachments
If UCase(oAttachment.DisplayName) Like "*.XML" Then
oAttachment.SaveAsFile sSaveFolder & oAttachment.DisplayName
MItem.UnRead = False
MItem.Delete
End If
Next
End Sub
OUTLOOK RULE:
I'm still sort of a beginner in using VBA and I've been trying to figure out how to upload a file via VBA in MS Outlook to a Sharepoint. I've tried mapping the Sharepoint to my Network Drive and such but to no avail.
My code is as follows:
Public Sub saveAttachSentDate(MItem As Outlook.MailItem)
Dim oAttachment As Outlook.Attachment
Dim sSaveFolder As String
Dim file As String
Dim DateFormat As String
sSaveFolder = "(URL of the sharepoint along with the folder to save it on)"
For Each oAttachment In MItem.Attachments
DateFormat = Format(MItem.SentOn - 1, "mm.dd.yy ")
file = sSaveFolder & DateFormat & oAttachment.DisplayName
oAttachment.SaveAsFile sSaveFolder & DateFormat & oAttachment.DisplayName
Next
End Sub
My File Name is labelled like this: "[My Department] - (Client Name) Telephony Summary"
I always get this error
"Run-time error '2147024735 (800700a1)': Cannot save the attachment. File name or directory name is not valid."
I'm thinking that the probable cause is that the sharepoint I'm uploading to requires a username and password every time you access it. I tried another sharepoint using the same exact code that doesn't require login credentials and it works just fine. I can't seem to find a work-around and I'd appreciate any help!
The path passed to the SaveAsFile method can't be represented by the URL string or network location. You need to specify a local folder from which you can start uploading files. The Outlook object model doesn't provide anything for uploading files to any web servers, so you will have to do that on your own. To get that working I'd recommend developing a COM add-in. For example, VSTO based add-ins are built on top of .net framework and can use BCL classes to deal with that. See Walkthrough: Create your first VSTO Add-in for Outlook to get started quickly.
I'm trying to write a fairly simple macro that will add the received date to the subject line of an email. I've compiled the code below and it works for a single e-mail. However, it appears the For Each loop ends after the first email, and I'm at a total loss on how to solve this. I've tried changing the the PickFolder line with an ActiveExplorer.Selection line - exactly the same problem.
Sub SelectedMailItemsSubjectWithDate()
Dim objMainFolder As Outlook.Folder
Dim MObj As Outlook.MailItem
Set objMainFolder = Outlook.Application.Session.PickFolder
For Each MObj In objMainFolder.Items
'Adds the date to the subject
MObj.Subject = Format(MObj.ReceivedTime, "YYYYMMDD") & " - " & MObj.Subject
Next MObj
End Sub
Is there anyone who can help me with this? I would be much obliged!
The reason I'm trying to add the date is that we will need to archive thousands of e-mail in the .msg format, in an online storage that doesn't show anything except the subject line of the file.
Wait a sec, how do you know only one item is processed? Are you stepping through your code and see that the loop exits prematurely? Or that only one item is modified? In the latter case you need to save the item:
MObj.Subject = Format(MObj.ReceivedTime, "YYYYMMDD") & " - " & MObj.Subject
MObj.Save
Declare MObj as a generic Object rather than Outlook.MailItem - you can have other items in a folder, such as ReportItem, MeetingItem, etc.
I'm currently using an Outlook VBA script to save PDF attachment from an automated report email onto the company shared drive. The problem is I'm taking 2 weeks off for vacation soon, so I wonder if there's anyway I can make the VBA run when my PC is in sleep mode?
From my research I understand the VBA can't run at all if my PC is turned off, but what if I leave Outlook open and put my PC into sleep?
Below is my simple VBA script for your reference. (It is run as filtering rule)
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder_1 As String
saveFolder_1 = "my path 1"
Dim saveFolder_2 As String
saveFolder_2 = "my path 2"
Dim dateFormat
dateFormat = Format(itm.ReceivedTime, "yyyymmdd")
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder_1 & "\" & "file name" & dateFormat & ".pdf"
objAtt.SaveAsFile saveFolder_2 & "\" & "file name" & dateFormat & ".pdf"
Set objAtt = Nothing
Next
End Sub
No, Outlook will not run when your computer is sleeping. Normally, you can have server side rules that are executed by the Exchange server regardless of whether Outlook is connected to it, but it will not be able to run VB script or save attachments. The best you can do is move messages to other folders or to forward/delegate messages to other mailboxes.
Another alternative (which is how I do it) is to run outlook on a Virtual Machine residing on an always on server. This way it doesn't matter what the state of a local machine is, Outlook will always be running and able to execute code on the server.
If you can't run your code on a server, you can use the Windows Task Scheduler to wake your workstation from sleep, run the macro, and go back to sleep. Reasonably good instructions are at this page.
I have a script within Outlook that moves an attachment to a folder. The folder is actually SharePoint Online so the file in the attachment is posted to our SharePoint site daily. It works perfectly Monday through Friday when the email is received.
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "C:\Users\xxx\SharePoint\Systems-Information Technolog - SYS 1\xxx"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
However, on Monday when the attachment is received and the rule is activated the attachment for Monday is posted to SharePoint, then the attachment for Sunday is posted and then the attachment for Saturday is posted making the only file available on SharePoint 2 days old.
I realize I can have the report stop arriving on Saturday and Sunday but can the script be modified to only retrieve the most recent attachment and then not run again? The reason this could be important is the attachment in the future could be created multiple times a day so I would need the most recent and no other to be posted.
Thanks for your help!
You can check out the RecievedTime property of the MailItem class which returns a Date indicating the date and time at which the item was received. So, you can check out the received time of each incoming email and save the attachment only if the mail item is the most recent one.
What kind of rule? After the message arrives? Keep in mind that if Outlook is not running, it will not receive new mail motivations (Saturday and Sunday). When Exchange cached store syncs with the server (on Monday), you will see new unread messages in the Inbox, but there will be no new mail motivations and the new mail rules will not fire.
You will need to change your code to run in response to the Items.ItemAdd event on the Inbox folder or run your code manually and process unread messages in the Inbox.