|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Import Macro
Hi,
I am trying to create an email survey, the survey data returned is in the form of an outlook attachment postdata.att. What I would like to do is search the email inbox for emails containing these attachments and then take the data from the attachment and import it into an excel spreadsheet. I managed to find the following code: Public Sub ReadEmails() ' remember to include a reference to Outlook library Dim olApp As Outlook.Application Dim olNamespace As Outlook.Namespace Dim olFolder As Outlook.MAPIFolder Dim olMail As Outlook.MailItem Dim lngRow As Long Dim intAtt As Integer Dim wbkTemp As Workbook Dim strTempFile As String Set olApp = New Outlook.Application Set olNamespace = olApp.GetNamespace("MAPI") Set olFolder = olNamespace.GetDefaultFolder(olFolderInbox) lngRow = 1 ' go thru all mail in Inbox For Each olMail In olFolder.Items ' only check those with attachments For intAtt = 1 To olMail.Attachments.Count ' only those with xls files If InStr(1, olMail.Attachments(intAtt).Filename, ".xls", vbTextCompare) > 0 Then ' get folder and filename for att file strTempFile = ThisWorkbook.Path & Application.PathSeparator & olMail.Attachments(intAtt).Filename ' save it so we can open and read it olMail.Attachments(intAtt).SaveAsFile strTempFile Set wbkTemp = Workbooks.Open(strTempFile) With ThisWorkbook.ActiveSheet .Cells(lngRow, 1) = olMail.Attachments(intAtt).Filename .Cells(lngRow, 2) = wbkTemp.Sheets(1).Range("A1") End With lngRow = lngRow + 1 ' close and destroy temporary excel file wbkTemp.Close False Set wbkTemp = Nothing Kill strTempFile End If Next Next Set olMail = Nothing Set olFolder = Nothing Set olNamespace = Nothing Set olApp = Nothing End Sub Obviously this is for xls attachments but mine is a .att attachment which would simply include data like moreinfo=yes rating=good i can import the data manually in excel using the following macro: ActiveWorkbook.Worksheets.Add With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;C:\Documents\POSTDATA.ATT" _ , Destination:=Range("A1")) .Name = "POSTDATA" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 850 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileOtherDelimiter = "=" .TextFileColumnDataTypes = Array(2, 2) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With but how do I combine these 2 into something which will take each email attachment in turn and import the data into excel, copying the certain details? Thank you for all your help |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > Import Macro |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|