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.
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 am using windows 10 on my pc and use chrome most of the time for outlook. I am trying to get a script that automatically opens a link received in a email. I have found vba scripts but they do not work i keep getting errors. If any of you could help me out with this?
You can use the HTMLBody property of the MailItem class to find all hyperlinks there. Then you can use the following method to run Chrome with a link found:
Sub LaunchInChrome (url as String)
Dim chromePath As String
chromePath = """C:\Program Files\Google\Chrome\Application\chrome.exe"""
Shell (chromePath & " -url " & url)
End Sub
It is easy to save emails in Outlook VBA with MailItem.SaveAs
But I don't see any option to save additional details like i.e. the Author and Categories.
The 3rd party program MessageSave allows to save mails with Categories and Author in .msg format. In Windows Explorer the columns Author and Categories show the same information like in Outlook.
Does anybody know how to save messages using Outlook VBA including these additional information?
I bought MessageSave and it's a good program but they don't allow their save function to be used in VBA. The only workaround is to let MessageSave save messages when they "arrive" in a specific folder. If necessary I can use this function but this is just a workaround.
Here is a sample how the emails saved with MessageSave are shown in Windows Explorer:
here is a process i followed: (win7 64)
web search "windows vba set extended file property"
first hit: StackOverfow 16989882
web search: "DSOFile.OleDocumentProperties"
hit microsoft: The Dsofile.dll files lets you edit Office document properties when you do not have Office installed
https://support.microsoft.com/en-us/help/224351/the-dsofile.dll-files-lets-you-edit-office-document-properties-when-yo
that is not a typo ... it ends in "when-yo"
download: DsoFileSetup_KB224351_x86.exe
open DsoFileSetup_KB224351_x86.exe using 7-zip program (from 7-zip.org)
copy dsofile.dll from DsoFileSetup_KB224351_x86.exe (using 7-zip) into a folder desktop (named "testFiles" in this example) (this could be anywhere ... maybe windows system32 or syswow64 ... i only tried on desktop )
open command prompt window as administrator
navigate to folder that contains dsofile.dll
execute following: regsvr32 dsofile.dll
should receive success confirmation
start outlook ... vba editor ... tools ... references
and find "DSO OLE Document Properties Reader 2.1" and check the box on left
back to vba editor ... create new module
paste in the following: (this is just a minimal test script)
Sub extendedProperties()
Dim objFile As OleDocumentProperties
Set objFile = CreateObject("DSOFile.OleDocumentProperties")
objFile.Open ("C:\Users\js\Desktop\testFiles\myMessage.msg") ' adjust to match your system
objFile.SummaryProperties.Subject = "My Subject"
objFile.Save
Set objFile = Nothing
End Sub
copy (drag&drop) an email "myMessage" from outlook to folder (on desktop in this example)
right-click on folder column header ... click on more ... find "subject" ...
click checkbox
ran script
subject column should contain "My Subject" next to myMessage.msg (or whatever your message is named)
there may be a simpler way ... maybe windows PowerShell has a command that could be called from vba
here is a more usable script
it has no error checking
no check for duplicate message names
no check for illegal filenames (except for ":" character)
just select a bunch of emails in any outlook folder and run this
' make sure you have a reference to "DSO OLE Document Properties Reader"
Sub extendedProperties()
Dim msg As mailItem
Dim objFile As OleDocumentProperties
' Set objFile = CreateObject("DSOFile.OleDocumentProperties")
Set objFile = New OleDocumentProperties
Dim fileName As String
Dim subjectText As String
' !!!!!!!! select a bunch of messages before running this !!!!!!!!
For Each msg In ActiveExplorer.Selection
subjectText = Replace(msg.Subject, ":", "_") ' get rid of illegal file name character (there are others)
' adjust the destination folder for your liking
fileName = "C:\Users\js\Desktop\testFiles\" & subjectText & ".msg"
Debug.Print fileName
msg.SaveAs fileName
objFile.Open fileName
objFile.SummaryProperties.Subject = "My Subject"
'objFile.Save
objFile.Close True ' save and close !!!!! duplicate filenames get overwritten !!!!!
' stop ' uncomment this line and the code will stop. press F5 to run, F8 to single-step
Next msg
Set msg = Nothing
Set objFile = Nothing
End Sub
I have been using folder browser for VBA, I could paste the code of it, but bottom line is that I get returned file name as a string.
Is there any way to access drawing properties (i.e number of layouts) without open?
Public Sub TestFileDialog()
dwgname = FileBrowseOpen("C:", "*", ".dwg", 1) 'dwgname is typeof string
End Sub
Its only the first step (use of FileBrowseOpen function is shown, but also i can use FolderBrowse and collect all .dwg inside of folder),actually i had in mind to batch export all layouts of selected .dwgs to currenty open one. Is there any chance for that?
To effectively read a .dwg file you'll need to open AutoCAD, otherwise the information is not accessible. Some properties may be, such as author, but not number of layouts...
But you can use AutoCAD Console (accoreconsole.exe) to run a headless AutoCAD and use APIs to read any information you need. This is really fast for reading lot's of files and the user will not see it running (but it needs to be installed anyway).
http://aucache.autodesk.com/au2012/sessionsFiles/3338/3323/handout_3338_CP3338-Handout.pdf
you could stay in VBA and use ObjectDBX
it leads to a very similar approach as accoreconsole.exe on in .NET does, i.e you won't see any drawing open in UI since it works on the database itself
It requires adding library reference (Tools->References) to "AutoCAD/ObjectDBX Common XX.Y Type Library", where "XX.Y" is "19.0" for AutoCAD 2014
a minimal functioning code is
Sub main()
Dim myAxDbDoc As AxDbDocument
Dim FullFileName As String
FullFileName = "C:\..\mydrawing.dwg" '<== put here the full name of the file to be opened
Set myAxDbDoc = AxDb_SetDrawing(FullFileName)
MsgBox myAxDbDoc.Layers.Count
End Sub
Function AxDb_SetDrawing(FullFileName As String) As AxDbDocument
Dim DBXDoc As AxDbDocument
Set DBXDoc = Application.GetInterfaceObject("ObjectDBX.AxDbDocument.19") '<== place correct AutoCAD version numeber ("19" works for AutoCAD 2014)
On Error Resume Next
DBXDoc.Open FullFileName
If Err <> 0 Then
MsgBox "Couldn't open" & vbCrLf & vbCrLf & FullFileName, vbOKOnly + vbCritical, "AxDB_SetDrawing"
Else
Set AxDb_SetDrawing = DBXDoc
End If
On Error GoTo 0
End Function
Still, you must have one AutoCAD session running from which make this sub run! But you should have it since talked about "currently open" drawing
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.