VBA open workbook attached to outlook template - excel

Is there any way that one can open a workbook attached to an email template, edit, and save it prior to sending the message? I've created the mailitem object using Set Mesg = OutlookAp.CreateItemFromTemplate("C:\Template.oft") and I can see the attachment, but I can't see a way to open it thus far. If anyone has suggestions, or knows that this simply can't be done, I'm all ears.
Looks like I may have to save and edit the file prior to sending... Still open to ideas, but it looks like it simply isn't possible to open the attachment through VBA

I assume you are automating Outlook from Excel. This solution may work for you, but as you note it does rely on saving the attachment and re-attaching the manipulated version of the file. Assuming you can write the code which will "edit" the Workbook attachment, this should work for you.
Sub TestOutlookTemplate()
Dim MyOutlook As Outlook.Application
Dim MyMail As Outlook.MailItem
Dim att As Outlook.Attachment
Dim templatePath As String
Dim tempFileName As String
Dim attWorkbook As Workbook
templatePath = "C:\users\david_zemens\desktop\Untitled.oft"
tempFileName = "C:\users\david_zemens\desktop\tempexcelfile.xlsx"
Set MyOutlook = CreateObject("Outlook.Application")
Set MyMail = MyOutlook.CreateItemFromTemplate(templatePath)
MyMail.Display
For Each att In MyMail.Attachments
If att.DisplayName Like "*.xls*" Then
att.SaveAsFile tempFileName
'Now that you have saved the file, delete the attachment
att.Delete
'Open the file
Set attWorkbook = Workbooks.Open(tempFileName)
'Perform manipulation on the file
attWorkbook.Sheets(1).Name = "Sheet ONE"
'Save fhe file
attWorkbook.Save
'Close the file
attWorkbook.Close
MyMail.Attachments.Add tempFileName
End If
Next
'Send your mail (make sure you have added a recipient
MyMail.Send
Set attWorkbook = Nothing
Set MyMail = Nothing
Set MyOutlook = Nothing
End Sub

Related

Download email attachment to a specific folder then open an excel file using VBA in outlook

I've set outlook to run the following script when an email is received from a specific email adderess. I want the script to download the attachment then open an excel file but I get the following error: Compile error: User-defined type not defined. I'm not sure what it is that I'm doing wrong, can someone please help. Below is the code:
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim source As Excel.Workbook
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim ExApp As Excel.Application
Dim ExWbk As Workbook
Set ExApp = New Excel.Application
saveFolder = "C:\Reports\Daily Traffic Report per Site\Source"
On Error Resume Next
Kill "C:\Reports\Source\*.*"
On Error GoTo 0
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
Next
Set ExWbk = ExApp.Workbooks.Open("C:\Reports\Calculation.xlsm")
End Sub
To use early binding, you first need to reference the available Outlook object library. To do this from Visual Basic (VB) or Visual Basic for Applications, follow these steps if you try to run the code in Outlook:
In the Visual Basic Editor, on the Tools menu, click References.
Click to select the Microsoft Excel XX.0 Object Library check box, and then click OK.

How to pull Excel file path using Outlook VBA?

I am trying to create a macro in Outlook to pull the file path for the open Excel workbook into a hyperlink in my email.
For example, if a workbook with file path "C:\Desktop\Documents\Phones.xlsx" was open, there would be a link created in my email to that workbook.
If you just want the link to be the body of the email this should make it.
Sub hyperlink_zu_email()
'get path of active document
Dim spath As String
Dim sname As String
Dim scomplete As String
spath = Excel.Application.ActiveDocument.Path
sname = Excel.Application.ActiveDocument.Name
scomplete = Excel.Application.ActiveDocument.Path + "\" + Application.ActiveDocument.Name
'send Email
Dim mailmsg As Object
Dim OutLkApp As Object
Set OutLkApp = CreateObject("Outlook.Application")
Set mailmsg = OutLkApp.CreateItem(0)
With mailmsg
.To = "he#do.at"
.Subject = scomplete
.HTMLBody = "<a href='file:///" & scomplete & "'>" & sname & "</a>"
.Display
End With
End Sub
The "sname" part can be whatever you want, in this case it would take the name of the active document. This is what the link looks like for the guy who gets the mail.
But as #Foxfire And Burns And Burns mentioned, it will only work correctly if there is only one workbook opened.

How to open a sheet I've just unzipped in Outlook

I am trying to write some macros in both Excel and Outlook that in the end will automatically unzip and open a CSV, process the data, and sends it where it needs to go when a new email arrives in a specific folder. I have everything worked out on the Excel side but I am having difficulties with Outlook. The below code unzips the file. How would i go about opening the unzipped file and triggering an Excel macro (which is always open in another workbook)?
Another issue I am running into: this code only seems to work when i actually open the target email in it's own window.
Public Sub OpenZippedSheet()
Dim objMail As Outlook.MailItem
Dim objAttachments As Outlook.Attachments
Dim objAttachment As Outlook.Attachment
Dim objShell As Object
Dim objFileSystem As Object
Dim strTempFolder As String
Dim strFilePath As String
Dim strFileName As String
Set objMail = Outlook.Application.ActiveInspector.CurrentItem
Set objAttachments = objMail.Attachments
'Save & Unzip the zip file in local drive
Set objShell = CreateObject("Shell.Application")
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
strTempFolder = objFileSystem.GetSpecialFolder(2).Path & "\Temp" & Format(Now, "yyyy-mm-dd-hh-mm-ss")
MkDir (strTempFolder)
For Each objAttachment In objAttachments
If Right(objAttachment.FileName, 3) = "zip" Then
strFilePath = strTempFolder & "\" & objAttachment.FileName
objAttachment.SaveAsFile (strFilePath)
objShell.NameSpace((strTempFolder)).CopyHere objShell.NameSpace((strFilePath)).Items
End If
Next
End Sub
I'm assuming I would do some sort of object.open but I don't know what the syntax would be to get it to actually open in Excel. And then is there a way to trigger an Excel macro from Outlook?
Thanks so much in advance!
this code only seems to work when i actually open the target email in it's own window.
That is because you rely on the ActiveInspector window. If you want to handle items selected in the Explorer windows you need to check the Selection object (see the corresponding property).
To open an Excel file you can:
Use the Shell.ShellExecute method. This method is equivalent to launching one of the commands associated with a file's shortcut menu. Each command is represented by a verb string. The set of supported verbs varies from file to file. The most commonly supported verb is "open", which is also usually the default verb. Other verbs might be supported by only certain types of files.
Automate Excel from your VBA macro to do the required actions. See How to automate Microsoft Excel from Visual Basic for more information.
To run your VBA macro code from other applications you can use the Application.Run method. Read more about that in the How do I use Application.Run in Excel article.
Application.Run "'" & TestWkbk.Name & "'!MacroNameHere", "parm1", "parm2"
Something like this (untested so may need some fixes):
'Note - any paths passed to objShell should be
' passed as *Variants*, not Strings
Dim oXL As Object, wbCSV As Object, fileNameInZip As Variant
Set objShell = CreateObject("Shell.Application")
For Each objAttachment In objAttachments
If Right(objAttachment.Filename, 3) = "zip" Then
strFilePath = strTempFolder & "\" & objAttachment.Filename
objAttachment.SaveAsFile strFilePath
Set oNS = oApp.Namespace(strFilePath)
For Each fileNameInZip In oNS.items 'loop over the files in the zip
Debug.Print fileNameInZip
If LCase(fileNameInZip) Like "*.csv" Then 'csv file?
'extract the file
objShell.Namespace(strTempFolder).copyhere oNS.items.Item(CStr(fileNameInZip))
If oXL Is Nothing Then Set oXL = GetObject(, "Excel.Application") 'assumes excel is running
Set wbCSV = oXL.Workbooks.Open(strTempFolder & "\" & fileNameInZip)
oXL.Run "'YourMacroFile.xlsm'!YourMacroName" 'run the macro
'clean up stuff...
End If 'is a csv file
Next 'file in zip
End If 'attachment is a zip file
Next 'attachment

Attaching unsaved Excel file to email

I have an Excel form for users to fill and send as an attachment (without having to save it locally on their computer).
The code works.
Dim Names()
Names = Array("testmail#gmail.com")
ActiveWorkbook.SendMail _
Recipients:=Names(), _
Subject:="Test subject"
I would like the email just to be created and not sent until the users have attached an additional file (found on their local computer).
I wrote the following code:
Dim olapp As Object
Dim olmail As Object
Dim wb As Workbook
Set olapp = CreateObject("outlook.application")
Set olmail = olapp.CreateItem(olMailItem)
Set wb = ActiveWorkbook
With olmail
.To = "testmail#gmail.com"
.Subject = "Test Subject"
.Body = ""
.Attachments.Add wb.FullName
.Display
My problem is that only the latest saved copy will be attached to the created email, and since the users will not have the form/Excel file stored locally on their computer, an empty form (or the last saved form) will be attached to the email.
Is there any way for an email to be created, with a copy of the workbook, but not to send it?
i tried this one-liner in the immediate window and it managed to send an unsaved file: Application.Workbooks("Book2").SendMail("my.email#company.com","Test Subject") you can use wb from your code instead of Application.Workbooks("Book2") in my example. Note that this will send the email, without the possibility to edit it.

Attaching embedded xl sheet into Outlook in VBA

I am trying to attach the embedded xl object into outlook mail in vb.
my existing code is
Sheets("Doc Repository").Shapes.Range(Array("Object 1")).Select
Selection.Copy
Here "Doc Repository" is a sheet which has embedded .zip file.
with the above code, Object 1(zip folder) is getting copied to clipboard.
I am not sure how to paste the the copied zip file into outlook mail.
I'd suggest saving the zip file to the disk and then add it as an attachment to the MailItem object using the Add mehtod of the Attachments class (see the corresponding property of the MailItem class).
Sub AddAttachment()
Dim myItem As Outlook.MailItem
Dim myAttachments As Outlook.Attachments
Set myItem = Application.CreateItem(olMailItem)
Set myAttachments = myItem.Attachments
myAttachments.Add "D:\Test.zip", _
olByValue, 1, "Test"
myItem.Display
End Sub

Resources