Is there a Special Method for extracting PKZip files with VBA? - excel

I'm attempting to use a fairly standard method for pulling emails from Outlook and then extracting Zip files. The File names and the folder locations are correct. I wonder if PKZip files (our corporate standard for Zip files) requires a special technique? Here's my code so far... It works perfectly up to the point where files are extracted from the Zip files where it fails. (oApp.Namespace(fDest).CopyHere oApp.Namespace(fDest & Fname).Items)
Sub SaveAttachments()
Dim ol As Outlook.Application
Dim ns As Outlook.Namespace
Dim fol As Outlook.Folder
Dim i As Object
Dim mi As Outlook.MailItem
Dim at As Outlook.Attachment
Dim Inbox As MAPIFolder
Dim strDate As String
Dim oApp As Object
Dim fDest As String
Dim fZip As String
strDate = InputBox("Enter Date in format dd-Mmm-yyyy", "User Date", Format(Now(), "dd-Mmm-yyyy"))
Set ol = New Outlook.Application
Set ns = ol.GetNamespace("MAPI")
Set fol = ns.Folders("GCMNamLogs").Folders("Inbox")
fDest = "C:\Users\jb76991\Desktop\0_SWPA 50011 CORP Violations\"
For Each i In fol.Items.Restrict("#SQL=urn:schemas:httpmail:subject LIKE '%" & strDate & "%'")
If i.Class = olMail Then
Set mi = i
For Each at In mi.Attachments
If InStr(at.Filename, ".zip") > 0 Then
If InStr(mi.Subject, "Daily SWPA swpaViolRPT REPORT for DOMAIN:CORP") > 0 Then
'Set oApp = CreateObject("Shell.Application")
FileNameFolder = fDest
Fname = at.Filename
at.SaveAsFile fDest & Fname
Set oApp = CreateObject("Shell.Application")
Debug.Print fDest & Fname
oApp.Namespace(fDest).CopyHere oApp.Namespace(fDest & Fname).Items
Application.Wait (Now + TimeValue("0:00:02"))
End If
If InStr(mi.Subject, "Daily SWPA swpaViolRPT REPORT for DOMAIN:INFRA") > 0 Then
Set oApp = CreateObject("Shell.Application")
FileNameFolder = fDest
Fname = at.Filename
at.SaveAsFile fDest & Fname
Debug.Print fDest & Fname
oApp.Namespace(fDest).CopyHere oApp.Namespace(fDest & Fname).Items
Application.Wait (Now + TimeValue("0:00:01"))
End If
If InStr(mi.Subject, "Daily SWPA swpaSumRPT REPORT for DOMAIN:CORP") > 0 Then
Set oApp = CreateObject("Shell.Application")
FileNameFolder = fDest
Fname = at.Filename
at.SaveAsFile fDest & Fname
Debug.Print fDest & Fname
oApp.Namespace(fDest).CopyHere oApp.Namespace(fDest & Fname).Items
Application.Wait (Now + TimeValue("0:00:01"))
End If
If InStr(mi.Subject, "Daily SWPA swpaSumRPT REPORT for DOMAIN:INFRA") > 0 Then
Set oApp = CreateObject("Shell.Application")
FileNameFolder = fDest
Fname = at.Filename
at.SaveAsFile fDest & Fname
Debug.Print fDest & Fname
oApp.Namespace(fDest).CopyHere oApp.Namespace(fDest & Fname).Items
Application.Wait (Now + TimeValue("0:00:01"))
End If
End If
Next at
End If
Next i
MsgBox ("Done")
End Sub

The string/variant is the fix, but you'd also benefit from reducing the repetition in the code
(untested)
Sub SaveAttachments()
Dim ol As Outlook.Application
Dim ns As Outlook.Namespace
Dim fol As Outlook.Folder
Dim i As Object
Dim mi As Outlook.MailItem
Dim at As Outlook.Attachment
Dim Inbox As MAPIFolder
Dim strDate As String
Dim fDest As Variant, FName As Variant, e, arrZips
strDate = InputBox("Enter Date in format dd-Mmm-yyyy", "User Date", Format(Now(), "dd-Mmm-yyyy"))
Set ol = New Outlook.Application
Set ns = ol.GetNamespace("MAPI")
Set fol = ns.Folders("GCMNamLogs").Folders("Inbox")
arrZips = Array("Daily SWPA swpaViolRPT REPORT for DOMAIN:CORP", _
"Daily SWPA swpaViolRPT REPORT for DOMAIN:INFRA", _
"Daily SWPA swpaSumRPT REPORT for DOMAIN:CORP", _
"Daily SWPA swpaSumRPT REPORT for DOMAIN:INFRA")
fDest = "C:\Users\jb76991\Desktop\0_SWPA 50011 CORP Violations\"
For Each i In fol.items.Restrict("#SQL=urn:schemas:httpmail:subject LIKE '%" & strDate & "%'")
If i.Class = olMail Then
Set mi = i
For Each at In mi.Attachments
FName = at.Filename
If InStr(FName, ".zip") > 0 Then
For Each e In arrZips
If InStr(mi.Subject, e) > 0 Then
at.SaveAsFile fDest & FName
ExtractZip fDest & FName, fDest, 2
Exit For
End If
Next e
End If
Next at
End If
Next i
MsgBox ("Done")
End Sub
Sub ExtractZip(ZipPath, DestFolder, Optional waitsecs As Long = 0)
Debug.Print "Extracting '" & ZipPath & "' to '" & DestFolder & "'"
With CreateObject("Shell.Application")
.Namespace(DestFolder).copyhere .Namespace(ZipPath).items
End With
If waitsecs > 0 Then Application.Wait Now + waitsecs / (24 * 60 * 60)
End Sub

Related

Get outlook email items with excel VBA, restrict by date

I wrote the below code and it works perfect when I want to extract the outlook email items in my excel sheet, but it does not work when I want to get the emails that were received on a certain date:
Sub getMail()
Dim i As Long
Dim arrHeader As Variant
Dim olNS As Namespace
Dim olInboxFolder As MAPIFolder
Dim olItems As Items
Dim olItem As Variant
Set olNS = GetNamespace("MAPI")
Set olInboxFolder = olNS.PickFolder 'Pick folder
Set olItems = olInboxFolder.Items
arrHeader = Array("Date Created", "SenderEmailAddress", "Subject", "Body")
ThisWorkbook.Worksheets("Output").Range("A1").Resize(1, UBound(arrHeader) + 1).Value = arrHeader
ActiveSheet.Range("E2", Range("E2").End(xlDown)).NumberFormat = "mm/dd/yyyy h:mm AM/PM"
i = 1
sFilter = InputBox("Enter Date")
FilterString = "[ReceivedTime] > sFilter "
For Each olItem In olItems.Restrict(FilterString)
' MailItem
If olItem.Class = olMail Then
Set mi = olItem
Debug.Print mi.ReceivedTime
ThisWorkbook.Worksheets("Output").Cells(i + 1, "A").Value = olItems(i).ReceivedTime
If olItems(i).SenderEmailType = "SMTP" Then
ThisWorkbook.Worksheets("Output").Cells(i + 1, "B").Value = olItems(i).SenderEmailAddress
ElseIf olItems(i).SenderEmailType = "EX" Then
ThisWorkbook.Worksheets("Output").Cells(i + 1, "B").Value = olItems(i).Sender.GetExchangeUser.PrimarySmtpAddress
End If
ThisWorkbook.Worksheets("Output").Cells(i + 1, "C").Value = olItems(i).Subject
ThisWorkbook.Worksheets("Output").Cells(i + 1, "D").Value = olItems(i).Body
i = i + 1
On Error Resume Next
' ReportItem
ElseIf olItem.Class = olReport Then
ThisWorkbook.Worksheets("Output").Cells(i + 1, "A").Value = olItems(i).CreationTime
ThisWorkbook.Worksheets("Output").Cells(i + 1, "B").Value = _
olItems(i).PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0E04001E") 'PR_DISPLAY_TO
ThisWorkbook.Worksheets("Output").Cells(i + 1, "C").Value = olItems(i).Subject
i = i + 1
End If
Next olItem
ThisWorkbook.Worksheets("Output").Cells.EntireColumn.AutoFit
MsgBox "Export complete.", vbInformation
Set olItems = Nothing
Set olInboxFolder = Nothing
Set olNS = Nothing
End Sub
For example I want to get all the emails that were sent starting with 08/16/2020 date, Or get all the emails on a certain date range.
Private Sub getMail_InputBoxDate()
Dim olNS As namespace
Dim olFilterFolder As Folder
Dim olItems As Items
Dim olItem As Object
Dim mi As mailItem
Dim filterString As String
Dim sDate1 As String
Dim filterString1 As String
Dim sDate2 As String
Dim filterString2 As String
Dim olItemsRes As Items
Set olNS = GetNamespace("MAPI")
Set olFilterFolder = olNS.PickFolder 'Pick folder
Set olItems = olFilterFolder.Items
olItems.Sort "[ReceivedTime]", True
Debug.Print vbCr & "olItems.Count: " & olItems.Count
sDate1 = InputBox("Enter Start Date", , "2020-09-14")
'Debug.Print sDate1
sDate1 = Format(sDate1 & " 00:00 AM", "DDDDD HH:NN")
Debug.Print vbCr & "sDate1: " & sDate1
' Single quotes around variable.
filterString1 = "[ReceivedTime] >= '" & sDate1 & "'"
Debug.Print " filterString1: " & filterString1
Set olItemsRes = olItems.Restrict(filterString1)
Debug.Print " olItemsRes.Count: " & olItemsRes.Count
sDate2 = InputBox("Enter date, one day after desired range.", , "2020-09-15")
'Debug.Print sDate2
sDate2 = Format(sDate2 & " 00:00 AM", "DDDDD HH:NN")
Debug.Print vbCr & "sDate2: " & sDate2
' With single quotes around variable.
filterString2 = "[ReceivedTime] < '" & sDate2 & "'"
Debug.Print " filterString2: " & filterString2
' Option 1 - Restrict the previously restricted items
Set olItemsRes = olItemsRes.Restrict(filterString2)
Debug.Print " olItemsRes.Count: " & olItemsRes.Count
Debug.Print
For Each olItem In olItemsRes
' MailItem
If olItem.Class = olMail Then
Set mi = olItem
Debug.Print mi.ReceivedTime & " " & mi.Subject
End If
Next olItem
' Option 2 - Combine two working filters into one
filterString = filterString1 & " AND " & filterString2
Debug.Print vbCr & "filterString combined: " & filterString
' Restrict the original items once
Set olItemsRes = olItems.Restrict(filterString)
Debug.Print "olItemsRes.Count: " & olItemsRes.Count
Debug.Print
For Each olItem In olItemsRes
' MailItem
If olItem.Class = olMail Then
Set mi = olItem
Debug.Print mi.ReceivedTime & " " & mi.Subject
End If
Next olItem
Debug.Print vbCr & "Done."
End Sub
This is Restrict Outlook Items by Date but adds time to the user-input date.

Send mail category data to Excel using Outlook VBA

I count the number of emails in Outlook by Category.
I am getting the output in a MsgBox.
I want the output in Excel.
Example-
Category No of Emails
Material(blue) 42
Vendor(green) 5
Macro used as below
Sub CategoriesEmails()
Dim oFolder As MAPIFolder
Dim oDict As Object
Dim sStartDate As String
Dim sEndDate As String
Dim oItems As Outlook.Items
Dim sStr As String
Dim sMsg As String
On Error Resume Next
Set oFolder = Application.ActiveExplorer.CurrentFolder
Set oDict = CreateObject("Scripting.Dictionary")
sStartDate = Date - 365
sEndDate = Date
Set oItems = oFolder.Items.Restrict("[Received] >= '" & sStartDate & "' And [Received] <= '" & sEndDate & "'")
oItems.SetColumns ("Categories")
For Each aitem In oItems
sStr = aitem.Categories
If Not oDict.Exists(sStr) Then
oDict(sStr) = 0
End If
oDict(sStr) = CLng(oDict(sStr)) + 1
Next aitem
sMsg = ""
For Each aKey In oDict.Keys
sMsg = sMsg & aKey & ": " & oDict(aKey) & vbCrLf
Next
MsgBox sMsg
Set oFolder = Nothing
End Sub
Based on your code, I've updated my code, you can paste all and run it:
Sub CategoriesEmails()
Dim oFolder As MAPIFolder
Dim oDict As Object
Dim sStartDate As String
Dim sEndDate As String
Dim oItems As Outlook.Items
Dim sStr As String
Dim sMsg As String
Dim strFldr As String
Dim OutMail As Object
Dim xlApp As Object
On Error Resume Next
Set oFolder = Application.ActiveExplorer.CurrentFolder
Set oDict = CreateObject("Scripting.Dictionary")
sStartDate = Date - 365
sEndDate = Date
Set oItems = oFolder.Items.Restrict("[Received] >= '" & sStartDate & "' And [Received] <= '" & sEndDate & "'")
oItems.SetColumns ("Categories")
For Each aItem In oItems
sStr = aItem.Categories
If Not oDict.Exists(sStr) Then
oDict(sStr) = 0
End If
oDict(sStr) = CLng(oDict(sStr)) + 1
Next aItem
sMsg = ""
i = 0
strFldr = "D:\"
Set xlApp = CreateObject("Excel.Application")
xlApp.Application.Visible = True
xlApp.Workbooks.Open strFldr & "test.xlsx"
xlApp.Sheets("Sheet1").Select
For Each aKey In oDict.Keys
xlApp.Range("a1").Offset(i, 0).Value = sMsg & aKey
xlApp.Range("B1").Offset(i, 0).Value = oDict(aKey) & vbCrLf
i = i + 1
Next
xlApp.Save
Set oFolder = Nothing
End Sub
You could change the fileUrl, fileName, Excel field as your actual situation.

Replying to an Outlook mail based on subject, received time and date

I will receive the Journal entries from the client and need to reply to the same mail.
I have tried the below code to execute reply to chain of mails. But the code is returning the all mails which contains the same subject line.
I need to reply which comes recently. I have tried many solutions, but I unable to fix it.
How do I Reply to an email based on subject, received time and date
Sub mail()
Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olFldr As MAPIFolder
Dim olMail ' As Outlook.MailItem
Dim i As Integer
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
Set olFldr = Fldr
For Each olMail In olFldr.Items
If InStr(olMail.Subject, Range("C2")) <> 0 Then
Set ReplyAll = olMail.ReplyAll
With ReplyAll
.HTMLBody = "<font size=""3"" face=""Calibri"">" & _
"Hi Veronica <br><br>" & _
"The " & _
Left(ActiveWorkbook.Name, InStr(ActiveWorkbook.Name, ".") - 1) & "</B> has been posted.<br>" & _
"<br><br>Regards," & _
"<br><br>Rajesh</font>" & .HTMLBody
emailReady = True
.Display
End With
End If
Next olMail
If Not emailReady Then
i = i + 1
If i > Fldr.Folders.Count Then
MsgBox ("The email with the given subject line was not found!")
Exit Sub
Else
Set olFldr = Fldr.Folders(i)
GoTo tryAgain
End If
End If
End Sub
Work with Items.Restrict Method (Outlook) to Filter by Subject and Date & Time
Make sure to convert your subject Range("C2") to string variable then use it on your filter
Example
Option Explicit
Public Sub Example()
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
Dim olNs As Outlook.Namespace
Set olNs = olApp.GetNamespace("MAPI")
Dim Inbox As Outlook.MAPIFolder
Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
Dim Subject As String
Subject = ThisWorkbook.Sheets("Sheet1").Range("C2").Text
Debug.Print Subject
Dim i As Long
Dim Filter As String
Filter = "#SQL=" & Chr(34) & "urn:schemas:httpmail:datereceived" & _
Chr(34) & " >= '02/20/2018' And " & _
Chr(34) & "urn:schemas:httpmail:datereceived" & _
Chr(34) & " < '02/25/2018' And " & _
Chr(34) & "urn:schemas:httpmail:subject" & _
Chr(34) & "Like '%" & Subject & "%'"
Dim Items As Outlook.Items
Set Items = Inbox.Items.Restrict(Filter)
Items.Sort "[ReceivedTime]"
For i = Items.Count To 1 Step -1
DoEvents
If TypeOf Items(i) Is mailitem Then
Dim Item As Object
Set Item = Items(i)
Debug.Print Item.Subject ' Print on Immediate Window
Debug.Print Item.ReceivedTime ' Print on Immediate Window
End If
Next
End Sub
Items.Restrict Method Applies a filter to the Items collection, returning a new collection containing all of the items from the original that match the filter.
Edit - Complete Code
Option Explicit
Public Sub Example()
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
Dim olNs As Outlook.Namespace
Set olNs = olApp.GetNamespace("MAPI")
Dim Inbox As Outlook.MAPIFolder
Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
Dim Subject As String
Subject = ThisWorkbook.Sheets("Sheet1").Range("C2").Text
Debug.Print Subject
Dim i As Long
Dim Filter As String
Filter = "#SQL=" & Chr(34) & "urn:schemas:httpmail:datereceived" & _
Chr(34) & " >= '03/07/2018' And " & _
Chr(34) & "urn:schemas:httpmail:datereceived" & _
Chr(34) & " < '03/25/2018' And " & _
Chr(34) & "urn:schemas:httpmail:subject" & _
Chr(34) & "Like '%" & Subject & "%'"
Dim Items As Outlook.Items
Set Items = Inbox.Items.Restrict(Filter)
Items.Sort "[ReceivedTime]"
For i = Items.Count To 1 Step -1
DoEvents
If TypeOf Items(i) Is MailItem Then
Dim Item As Object
Set Item = Items(i)
Debug.Print Item.Subject ' Print on Immediate Window
Debug.Print Item.ReceivedTime ' Print on Immediate Window
Dim ReplyAll As Outlook.MailItem
Set ReplyAll = Item.ReplyAll
With ReplyAll
.HTMLBody = "<font size=""3"" face=""Calibri"">" & _
"Hi Veronica <br><br>" & _
"The " & Left(ActiveWorkbook.name, _
InStr(ActiveWorkbook.name, ".") - 1) & _
"</B> has been posted.<br>" & _
"<br><br>Regards," & _
"<br><br>Rajesh</font>" & .HTMLBody
.Display
End With
End If
Next
End Sub
https://stackoverflow.com/a/43622710/4539709

Formatting with method TransferSpreadsheet

actually my code is perfectly working. Code is actually creating a temporary query in Access for each supplier, i have in a table. After creating the query, next step is, saving the query as a Excel - file in my submitted path. After that, my code is creating a mail with an PDF as Attachement and with the Excel file as attachement for each supplier.
But is there a possibility to formatting after or into TransferSpreadsheet - method the columns widthness after saving the Excel - file? It would be also nice if first row has a bond. Here's my code
Sub ExcelExportuSenden()
Dim day As Integer
day = Weekday(Date, vbSunday)
Dim olApp As Outlook.Application
Dim toMulti, waarde As String
Dim mItem As Outlook.MailItem ' An Outlook Mail item
Dim dbs As Database
Dim qdfTemp As QueryDef
Dim qdfNew As QueryDef
Dim originalSql As String
Dim Identified_name As Recordset
Dim qdf As DAO.QueryDef
Set dbs = CurrentDb
Set olApp = CreateObject("Outlook.Application")
Set mItem = olApp.CreateItem(olMailItem)
Dim rs As Recordset
Dim filename As String
filename = Me.txt_path_pdf_description
Set rs = CurrentDb.OpenRecordset("Mail") 'Get name for the&nbsp;email recipient
If rs.RecordCount > 0 Then
rs.MoveFirst
Do Until rs.EOF
With mItem
Set mItem = olApp.CreateItem(olMailItem)
.BodyFormat = olFormatHTML
toMulti = rs![eMail]
waarde = toMulti
For Each qdf In dbs.QueryDefs
If qdf.Name = "inquiry" & "_" & rs!supplier Then
dbs.QueryDefs.Delete "inquiry" & "_" & rs!supplier
Exit For
End If
Next
Set qdfTemp = dbs.CreateQueryDef("inquiry" & "_" & rs!supplier) '
With dbs
qdfTemp.SQL = "SELECT * FROM [Filter_inquiry_original] WHERE [supplier] = '" & rs![supplier] & "'"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "inquiry" & "_" & rs!supplier, Me.txt_path & "\inquiry" & "_" & rs!supplier & ".xlsx", True
DoCmd.DeleteObject acQuery, "inquiry" & "_" & rs!Lsupllier
End With
.To = toMulti
' MsgBox toMulti
.Subject = "Anfrage zur Ausschreibung" & "_" & rs!Lieferant
.HTMLBody = "Sehr geehrte Damen und Herren,<br><br>" & _
"anbei erhalten Sie eine Ausschreibung, mit der Bitte um Bearbeitung!"
.Display
' .Send
.Attachments.Add filename
.Attachments.Add (Me.txt_path & "\inquiry" & "_" & rs!supplier & ".xlsx")
End With
rs.MoveNext
Loop
Else
MsgBox "No email address!"
End If
olApp.Quit
Set olApp = Nothing
Exit Sub
End Sub
Many thanks for your help!
Problem is solved. Here my code:
Sub ExcelExportuSenden3()
Dim day As Integer
day = Weekday(Date, vbSunday)
Dim olApp As Outlook.Application
Dim toMulti, waarde As String
Dim mItem As Outlook.MailItem ' An Outlook Mail item
Dim dbs As Database
Dim qdfTemp As QueryDef
Dim qdfNew As QueryDef
Dim originalSql As String
Dim Identified_name As Recordset
Dim qdf As DAO.QueryDef
Set dbs = CurrentDb
Set olApp = CreateObject("Outlook.Application")
Set mItem = olApp.CreateItem(olMailItem)
Dim rs As Recordset
Dim filename As String
' Dim filename3 As String
Dim xlApp As Object, xlWB As Object, xlsheet As Object
Dim TabNam As String
TabNam = "Tabelle1"
filename = Me.txt_Pfad_mitKunde
Set rs = CurrentDb.OpenRecordset("Mailversand") 'Get name for the&nbsp;email recipient
If rs.RecordCount > 0 Then
rs.MoveFirst
Do Until rs.EOF
With mItem
Set mItem = olApp.CreateItem(olMailItem)
.BodyFormat = olFormatHTML
toMulti = rs![eMail]
waarde = toMulti
For Each qdf In dbs.QueryDefs
If qdf.Name = "Anfrage" & "_" & rs!Lieferant Then
dbs.QueryDefs.Delete "Anfrage" & "_" & rs!Lieferant
Exit For
End If
Next
Set qdfTemp = dbs.CreateQueryDef("Anfrage" & "_" & rs!Lieferant) '
With dbs
qdfTemp.SQL = "SELECT * FROM [_Anfragematrix] WHERE [Lieferant] = '" & rs![Lieferant] & "'"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "Anfrage" & "_" & rs!Lieferant, Me.txt_Speicherpfad & "\Anfrage" & "_" & rs!Lieferant & ".xlsx", True _
, TabNam
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWB = xlApp.Workbooks.Open(Me.txt_Speicherpfad & "\Anfrage" & "_" & rs!Lieferant & ".xlsx")
Set xlsheet = xlWB.Sheets(TabNam)
With xlsheet
.Columns.AutoFit
With .Range("A1:O1")
With .Interior
.ColorIndex = 15
.Pattern = xlSolid
End With
End With
End With
xlWB.Save
xlWB.Close True
Set xlWB = Nothing
xlApp.Quit
Set xlApp = Nothing
DoCmd.DeleteObject acQuery, "Anfrage" & "_" & rs!Lieferant
End With
.To = toMulti
' MsgBox toMulti
.Subject = "Anfrage zur Ausschreibung" & "_" & rs!Lieferant
.HTMLBody = "Sehr geehrte Damen und Herren,<br><br>" & _
"anbei erhalten Sie eine Ausschreibung, mit der Bitte um Bearbeitung!"
.Display
' .Send
.Attachments.Add filename
.Attachments.Add (Me.txt_Speicherpfad & "\Anfrage" & "_" & rs!Lieferant & ".xlsx")
End With
rs.MoveNext
Loop
Else
MsgBox "No email address!"
End If
olApp.Quit
Set olApp = Nothing
Exit Sub
End Sub

Attaching a file

The idea is to attach an Excel file using Attachment.Add.
A macro reads the files within a folder and displays it in a column. I would like to attach those files via Excel.
I get an error on
.Attachments.Add Filelist & "\" & "Attch"
Sub Sendemailusingword()
Dim Olapp As Outlook.Application
Dim Olemail As Outlook.MailItem
Dim olmail As Object
Dim olinsp As Outlook.Inspector
Dim wddoc As Word.Document
Dim count As Integer
Dim x As Integer
Dim Filelist As String
Dim Attch As String
x = 1
row_number = 7
count = Sheet1.Range("K1")
For x = 1 To count
row_number = row_number + 1
Attch = Sheet1.Range("D" & row_number).Value
Filelist = "K:\3SHARE\2016 Plan\Statment Email Send"
Set Olapp = New Outlook.Application
Set Olemail = Olapp.CreateItem(olMailItem)
With Olemail
.Display
.To = Sheet1.Range("G" & row_number)
.Subject = Sheet1.Range("D6") & Sheet1.Range("F" & row_number)
.SentOnBehalfOfName = "ComdataCommissions#comdata.com"
.BodyFormat = olFormatHTML
.CC = Sheet1.Range("H" & row_number) & ";" & Sheet1.Range("I" & row_number)
Set olinsp = .GetInspector
Set wddoc = olinsp.WordEditor
Sheet1.Activate
Range("B2").CurrentRegion.Copy
wddoc.Range.Paste
.Attachments.Add Filelist & "\" & "Attch"
End With
Next x
End Sub
.Attachments.Add Filelist & "\" & Attch
Assuming the variable Attch contains the filename of the file to be attached.
EDIT: noticed you tagged with excel-vba-mac, in which case I don't think backslash will work as a path separator. : or maybe / should work, or use Application.PathSeparator

Resources