I have an Outlook template file stored in SharePoint.
If I sync the file to my PC and use the local path the code works.
How do I, with a VBA macro in Outlook, open the file from SharePoint?
Sub AccessPass()
Set msg = Application.CreateItemFromTemplate("https://xxxxxxxx.sharepoint.com/:u:/s/Admin/Documents/AccessPass.oft")
msg.SentOnBehalfOfName = " xxx#xxx.com"
msg.Display
End Sub
You need to save the template on the local disk, the Outlook object model can deal with a local file path only, passing URL is not a supported scenario.
Related
I need to extract .msg attachments from emails in a range and save these into another outlook sub-folder. This works currently by dragging the attachment into a sub-folder of 'inbox' but is there a quicker way?
I have searched around a bit and found ways to extract them to a local folder but i need them to be contained within outlook.
I appreciate any help and suggestions.
Thanks.
There are two problems here - first is accessing embedded message attachments without saving them first as MSG file. Second is importing the MSG files back - you can use Application.CreateItemFromTemplate, but the item will be unsent. You can use Namespace.OpenSharedItem, and then use MailItem.Move, but it is still a kludge.
There issn't much much you can do in OOM alone. Extended MAPI would work, but it is C++ or Delphi only. If using Redemption is an option (I am its author), you can use EmbeddeedMsg property exposed by the Redemption RDOAttachment object. You can also use RDOMail.CopyTo and pass a folder as a parameter to copy an embedded message attachment to a folder:
Set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
set redItem = Session.GetMessageFromId(OutlookMessage.EntryID)
set redFolder = Session.GetFolderFromId(OutlookFolder.EntryID)
for each attach in redItem.Attachments
if attach.Type = olEmbeddeditem Then
attach.EmbeddedMsg.CopyTo OutlookFolder
End If
next
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'm receiving Outlook mails that have other Outlook mails (*.msg) as attachments. I need them in txt format (or anything else Word can open).
I seem to have two choices:
1) Save the attachments to my drive as text files, rather than msg files. I have no idea how to do that, either manually or by code.
2) Save the attachments as msg files (I got a macro here on SO that does that), then open each file and save it at txt. But File-->Open in Outlook 2010 has no option for opening msg files. The only way I can see to open the file is to (manually) view the folder in File Explorer and double-click it. Once its open, I can use File-->SaveAs.
3) I could open and save the file in VBA. Or can I? (It seems you can't record a macro in Outlook the way you can in Word or Excel, or I would have tried it.)
EDIT: I tried Dmitri's suggestion, and this seems to work:
Dim oNamespace As NameSpace
Dim oFolder As Folder
' Get a reference to a NameSpace object.
Set oNamespace = Application.GetNamespace("MAPI")
' Open the file containing the shared item.
Set oSharedItem = oNamespace.OpenSharedItem("D:\temp.msg")
' Save the item to the folder.
oSharedItem.SaveAs "D:\temp.txt"
Save the embedded message attachments as MSG files (Attachment.SaveAsFile), then open then using Namespace.OpenSharedItem.
If you want to access the embedded message attachments as messages without saving them, you'd need either Extended MAPI (IAttach::OpenProperty(PR_ATTACH_DATA_OBJ, IID_IMessage, ...), C++ or Delphi only) or Redemption (I am its author - it exposes Attachment.EmbeddedMsg property).
I've used Thunderbird and now moving across to Outlook 2010, PC is Windows 7 Professional, 64 bit machine but I use 32 bit Office 2010.
I have 8 email addresses including 4 gmail addresses: all are IMAP. No exchange servers.
I believe that with IMAP, moving emails from the account inbox means they are no longer on the server so other devices can't see them anymore - happy to be corrected on this.
Thunderbird
I copy (not move) all emails to a Local Folders Inbox.
I then have a folder structure under that local folders Inbox.
I click a run rules button and it moves them (not copies) to the relevant sub folder.
The result is that apart from spam and legit emails without a rule, the inbox folder is emptied.
Outlook2010
I have the VBA routine that runs all rules in place and that works fine, I've even added the button to trigger that.
I have recreated my Thunderbird set-up by creating my folder structure under the Outlook Data File Inbox.
I've created for each email account a rule that all messages are copied (not moved) to that Outlook Data File Inbox.
I'm aware that rules must be created under each account and I believe no rules can be created in any Outlook Data File folder or sub folder.
However, if you then go to Rules/Alert pop up and select Run Rules Now you can select the rules to run and they will run on any folder including any Outlook Data File.
Essentially, I want to automate this process of running all rules on the Outlook Data File Inbox.
I cannot work out how to make the VBA code select that Outlook Data File Inbox, then run all rules on just that Outlook Data File Inbox.
Again, I believe this is necessary because if the Move rule runs from the account email inbox, that once the emails are moved from the account email inbox they are no longer available to be viewed on any other device.
I know I could copy all the emails from each account email inbox to the relevant sub folder and not bother copying to the Outlook Data File Inbox first. But this means I still need to regularly check all 8 email account inboxes in case an important email is in there for which I have not created a rule.
Any help would be appreciated.
Nigel
I will not be able to help you with VBA but allow me to propose an alternative.
First of all let me mention that it works for Outlook Data Files and any inbox that you specify. If you are familiar with VBA you should not have any problem with using my solution since the code is fairly simple.
Full solution has been described under similar question on superuser.
You can review it and clone it from Github project p0r. Its free.
To make it more relevant allow me to elaborate. I'm using Powershell to automate outlook and create custom rules within the script.
To connect to Outlook data file you can use following code:
$pstPath = "D:\path\to\pst\file.pst"
# CREATING OUTLOOK OBJECT
$outlook = New-Object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
# GETTING PST FILE THAT WAS SPECIFIED BY THE PSTPATH VARIABLE
$pst = $namespace.Stores | ?{$_.FilePath -eq $pstPath}
# ROOT FOLDER
$pstRoot = $pst.GetRootFolder()
# SUBFOLDERS
$pstFolders = $pstRoot.Folders
# PERSONAL SUBFOLDER
$personal = $pstFolders.Item("Personal")
And you can create your own rule by replacing the condition in the IF statement:
# MOVE EMAILS WITH SPECIFIC STRING IN TITLE TO THE SUBFOLDER /RANDOM/ UNDER PST FILE
# ! DESTINATION FOLDER SPECIFIED INLINE
IF ($Email.Subject -match "SPECIFIC STRING IN TITLE") {
$Email.Move($pstFolders.Item("Random")) | out-null
display ([string]$Email.Subject ) ([string]"Yellow")
continue
}
I'm using $Email.Move method to move email object from inbox to PST file, but you can use $Email.Copy if you prefer. Of course you can move emails between directories in Outlook data store as well.
Hope this helps. Let me know in case of any questions. I will be glad to help.
Re: I cannot work out how to make the VBA code select that Outlook Data File Inbox...
Private Sub ProcessPST()
Dim objNs As Namespace
Dim pstFolder As folder
Dim objItem As Object
Dim i As Long
Set objNs = GetNamespace("MAPI")
Set pstFolder = objNs.Folders("Test") ' <--- Test is the name of the pst
For i = 1 To pstFolder.Items.count
Set objItem = pstFolder.Items(i)
Debug.Print objItem.Subject
Next i
ExitRoutine:
Set objNs = Nothing
Set pstFolder = Nothing
Set objItem = Nothing
End Sub
I've created a Visual Basic project with many forms. In the project I write information to an excel file that I reference the location of on my local (C:) drive where the excel file resides. By reference I mean, I open an excel instance ten provide the source=local path on my hard drive. My question is how do I attach the excel file to the project so I don't have to reference the local location and can run the program on a different computer. In other words how do I bundle the excel file with the program? And how would I change the call to the excel file once I have it "bundled" to the program?
Add the excel file to the project. Go to solution explorer and right click on the excel file and select properties. Change the Copy to Output Directory to Copy Always. Now the file will be in the same directory as your exe. You can use Reflection.Assembly.GetExecutingAssembly to get the directory.
To add the file to the project:
Right click on project > Add > Existing Item > Your Excel File.xls
To include the file in build:
Right click on the file > Properties > Copy to Output Directory. Set this value to either Copy always or Copy if newer.
Here is the code to get the path to the excel file:
Dim exeDir As New IO.FileInfo(Reflection.Assembly.GetExecutingAssembly.FullName)
Dim xlPath = IO.Path.Combine(exeDir.DirectoryName, "Your Excel File.xls")
xlPath should now be the full path to the excel file.
MODIFIED REPLY AFTER USER COMMENTS
1) First create a Setting (named say MyExcelFile) to save the Name and Path of your excel file.
2) Now you can use My.Settings.MyExcelFile to refer to your excel file path.
3) If excel file is not found at the desired location, you can reset it by opening a File Open Dialog and asking the user to specify the file location.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Not IO.File.Exists(My.Settings.MyExcelFile) Then
Dim ofd As New OpenFileDialog
ofd.Filter = "Excel Files (*.xls, *.xlsx)|*.xls; *.xlsx"
ofd.Title = "Specify Excel File Location"
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
My.Settings.MyExcelFile = ofd.FileName
My.Settings.Save()
End If
End If
End Sub
i think iam not really getting what you mean but if you need to open the excel file with one click in the app and you dont want to write the path of the excel file (may be because you want you app portable) so the answer is to
put the excel file anywhere in the project folder
add existing item (*.* filter) and select the excel file
change the properties of the excel file build action: resource Copy to output directory:copy always
open the properties of the solution and click resources the drag the file and drop it
use the (using system.reflection & using system.io & using system.resources)
then write the code like this
string sPath = Path.GetTempFileName();
File.WriteAllBytes(sPath, Properties.Resources.excel File name here);
then take that (spath) and use it as your path so then you can change the project location with the excel file inside it and it will still be working without writing a constant path for it