Dir generates compile error looping through .msg files in a folder
up vote
0
down vote
favorite
I have a folder of email files from which I'm trying to extract sender details using Dir
. I need help in determining why the following code doesn't compile.
Sub UpdateReturns()
Dim fso As Object, fld As Object, olApp As Object, MailFile As Object, MsgDetail As Object
Dim stSearch As String, stPath As String, stFile As String, EmailFrom As String
stPath = "C:10. Working Docs"
stSearch = "Approve"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(stPath)
Set olApp = CreateObject("Outlook.Application")
MailFile = Dir(stPath & "*.msg")
Do While MailFile <> ""
Set MailFile = olApp.Session.OpenSharedItem(MailFile)
Set MsgDetail = Application.ActiveInspector.CurrentItem
EmailFrom = MsgDetail.SenderEmailAddress
Sheets("Settings").Cells(41, 4).Value = EmailFrom
'need to insert standard code to itterate down the list
'and match sender names to recipient names and votes ("Approve") etc
Loop
End Sub
excel vba outlook
add a comment |
up vote
0
down vote
favorite
I have a folder of email files from which I'm trying to extract sender details using Dir
. I need help in determining why the following code doesn't compile.
Sub UpdateReturns()
Dim fso As Object, fld As Object, olApp As Object, MailFile As Object, MsgDetail As Object
Dim stSearch As String, stPath As String, stFile As String, EmailFrom As String
stPath = "C:10. Working Docs"
stSearch = "Approve"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(stPath)
Set olApp = CreateObject("Outlook.Application")
MailFile = Dir(stPath & "*.msg")
Do While MailFile <> ""
Set MailFile = olApp.Session.OpenSharedItem(MailFile)
Set MsgDetail = Application.ActiveInspector.CurrentItem
EmailFrom = MsgDetail.SenderEmailAddress
Sheets("Settings").Cells(41, 4).Value = EmailFrom
'need to insert standard code to itterate down the list
'and match sender names to recipient names and votes ("Approve") etc
Loop
End Sub
excel vba outlook
You're trying to useMailFile
as both aString
- inMailFile = Dir...
, and as anObject
- inSet MailFile = ....
. Add a new variable for the first perhaps.
– BigBen
Nov 8 at 4:27
Yeah i tried that after i posted this, but i then get an error on the MsgDetail line saying "Object doesn't support this property or method"
– craig P
Nov 8 at 4:55
That's because you need to useolApp
.Application
refers to the Excel application.
– BigBen
Nov 8 at 5:00
And you never do anything with the message returned by OpenSharedItem - you always use the message from ActiveInspector.CurrentItem
– Dmitry Streblechenko
Nov 8 at 17:39
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a folder of email files from which I'm trying to extract sender details using Dir
. I need help in determining why the following code doesn't compile.
Sub UpdateReturns()
Dim fso As Object, fld As Object, olApp As Object, MailFile As Object, MsgDetail As Object
Dim stSearch As String, stPath As String, stFile As String, EmailFrom As String
stPath = "C:10. Working Docs"
stSearch = "Approve"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(stPath)
Set olApp = CreateObject("Outlook.Application")
MailFile = Dir(stPath & "*.msg")
Do While MailFile <> ""
Set MailFile = olApp.Session.OpenSharedItem(MailFile)
Set MsgDetail = Application.ActiveInspector.CurrentItem
EmailFrom = MsgDetail.SenderEmailAddress
Sheets("Settings").Cells(41, 4).Value = EmailFrom
'need to insert standard code to itterate down the list
'and match sender names to recipient names and votes ("Approve") etc
Loop
End Sub
excel vba outlook
I have a folder of email files from which I'm trying to extract sender details using Dir
. I need help in determining why the following code doesn't compile.
Sub UpdateReturns()
Dim fso As Object, fld As Object, olApp As Object, MailFile As Object, MsgDetail As Object
Dim stSearch As String, stPath As String, stFile As String, EmailFrom As String
stPath = "C:10. Working Docs"
stSearch = "Approve"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(stPath)
Set olApp = CreateObject("Outlook.Application")
MailFile = Dir(stPath & "*.msg")
Do While MailFile <> ""
Set MailFile = olApp.Session.OpenSharedItem(MailFile)
Set MsgDetail = Application.ActiveInspector.CurrentItem
EmailFrom = MsgDetail.SenderEmailAddress
Sheets("Settings").Cells(41, 4).Value = EmailFrom
'need to insert standard code to itterate down the list
'and match sender names to recipient names and votes ("Approve") etc
Loop
End Sub
excel vba outlook
excel vba outlook
edited Nov 12 at 7:50
Joe McMahon
2,1521225
2,1521225
asked Nov 8 at 4:12
craig P
1
1
You're trying to useMailFile
as both aString
- inMailFile = Dir...
, and as anObject
- inSet MailFile = ....
. Add a new variable for the first perhaps.
– BigBen
Nov 8 at 4:27
Yeah i tried that after i posted this, but i then get an error on the MsgDetail line saying "Object doesn't support this property or method"
– craig P
Nov 8 at 4:55
That's because you need to useolApp
.Application
refers to the Excel application.
– BigBen
Nov 8 at 5:00
And you never do anything with the message returned by OpenSharedItem - you always use the message from ActiveInspector.CurrentItem
– Dmitry Streblechenko
Nov 8 at 17:39
add a comment |
You're trying to useMailFile
as both aString
- inMailFile = Dir...
, and as anObject
- inSet MailFile = ....
. Add a new variable for the first perhaps.
– BigBen
Nov 8 at 4:27
Yeah i tried that after i posted this, but i then get an error on the MsgDetail line saying "Object doesn't support this property or method"
– craig P
Nov 8 at 4:55
That's because you need to useolApp
.Application
refers to the Excel application.
– BigBen
Nov 8 at 5:00
And you never do anything with the message returned by OpenSharedItem - you always use the message from ActiveInspector.CurrentItem
– Dmitry Streblechenko
Nov 8 at 17:39
You're trying to use
MailFile
as both a String
- in MailFile = Dir...
, and as an Object
- in Set MailFile = ....
. Add a new variable for the first perhaps.– BigBen
Nov 8 at 4:27
You're trying to use
MailFile
as both a String
- in MailFile = Dir...
, and as an Object
- in Set MailFile = ....
. Add a new variable for the first perhaps.– BigBen
Nov 8 at 4:27
Yeah i tried that after i posted this, but i then get an error on the MsgDetail line saying "Object doesn't support this property or method"
– craig P
Nov 8 at 4:55
Yeah i tried that after i posted this, but i then get an error on the MsgDetail line saying "Object doesn't support this property or method"
– craig P
Nov 8 at 4:55
That's because you need to use
olApp
. Application
refers to the Excel application.– BigBen
Nov 8 at 5:00
That's because you need to use
olApp
. Application
refers to the Excel application.– BigBen
Nov 8 at 5:00
And you never do anything with the message returned by OpenSharedItem - you always use the message from ActiveInspector.CurrentItem
– Dmitry Streblechenko
Nov 8 at 17:39
And you never do anything with the message returned by OpenSharedItem - you always use the message from ActiveInspector.CurrentItem
– Dmitry Streblechenko
Nov 8 at 17:39
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
I ended up working a way around it. I was just spinning my wheels with the outlook controls and I decided to work with something I know better, end result was i set it up to read from a save folder where the files are saved as text files, and i have a formula where the contents of the searched files drop to match them against sent emails.
Sub UpdateReturns()
Dim stPath As String, mailfile As String, Fname As String, Ename As String
Dim fso As Object, fld As Object
Dim i As Integer, count As Integer
Const strSearch = "From:"
stPath = "C:10. Working Docs"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(stPath)
mailfile = Dir(stPath & "Approve*.txt")
count = 0
Do While mailfile <> ""
count = count + 1
mailfile = Dir()
Loop
i = 0
mailfile = Dir(stPath & "Approve*.txt")
Do While count >= 1
Open mailfile For Input As #1
Do Until EOF(1)
Line Input #1, textline
If InStr(textline, "From:") > 0 Then
Fname = mailfile
Ename = textline
End If
Loop
Close #1
Range("C" & (40 + count)).Value = Fname
Range("D" & (40 + count)).Value = Ename
mailfile = Dir()
count = count - 1
Loop
End Sub
add a comment |
up vote
0
down vote
Dir returns a string that represents the file name.
Sub UpdateReturns()
Dim fso As Object
Dim fld As Object
Dim olApp As Object
Dim MailFile As Object
Dim MailFileStr As String
Dim MailPathFileStr As String
Dim stPath As String
Dim EmailFrom As String
stPath = "C:10. Working Docs"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(stPath)
Set olApp = CreateObject("Outlook.Application")
MailFileStr = Dir(stPath & "*.msg")
' file name only, no path
Debug.Print "MailFileStr: " & MailFileStr
Do While MailFileStr <> ""
' path and file
MailPathFileStr = stPath & "" & MailFileStr
Debug.Print vbCr & "MailPathFileStr: " & MailPathFileStr
Set MailFile = olApp.Session.OpenSharedItem(MailPathFileStr)
EmailFrom = MailFile.SenderEmailAddress
Debug.Print "EmailFrom: " & EmailFrom
Set MailFile = Nothing
MailFileStr = Dir ' Get next entry.
Loop
End Sub
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I ended up working a way around it. I was just spinning my wheels with the outlook controls and I decided to work with something I know better, end result was i set it up to read from a save folder where the files are saved as text files, and i have a formula where the contents of the searched files drop to match them against sent emails.
Sub UpdateReturns()
Dim stPath As String, mailfile As String, Fname As String, Ename As String
Dim fso As Object, fld As Object
Dim i As Integer, count As Integer
Const strSearch = "From:"
stPath = "C:10. Working Docs"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(stPath)
mailfile = Dir(stPath & "Approve*.txt")
count = 0
Do While mailfile <> ""
count = count + 1
mailfile = Dir()
Loop
i = 0
mailfile = Dir(stPath & "Approve*.txt")
Do While count >= 1
Open mailfile For Input As #1
Do Until EOF(1)
Line Input #1, textline
If InStr(textline, "From:") > 0 Then
Fname = mailfile
Ename = textline
End If
Loop
Close #1
Range("C" & (40 + count)).Value = Fname
Range("D" & (40 + count)).Value = Ename
mailfile = Dir()
count = count - 1
Loop
End Sub
add a comment |
up vote
0
down vote
I ended up working a way around it. I was just spinning my wheels with the outlook controls and I decided to work with something I know better, end result was i set it up to read from a save folder where the files are saved as text files, and i have a formula where the contents of the searched files drop to match them against sent emails.
Sub UpdateReturns()
Dim stPath As String, mailfile As String, Fname As String, Ename As String
Dim fso As Object, fld As Object
Dim i As Integer, count As Integer
Const strSearch = "From:"
stPath = "C:10. Working Docs"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(stPath)
mailfile = Dir(stPath & "Approve*.txt")
count = 0
Do While mailfile <> ""
count = count + 1
mailfile = Dir()
Loop
i = 0
mailfile = Dir(stPath & "Approve*.txt")
Do While count >= 1
Open mailfile For Input As #1
Do Until EOF(1)
Line Input #1, textline
If InStr(textline, "From:") > 0 Then
Fname = mailfile
Ename = textline
End If
Loop
Close #1
Range("C" & (40 + count)).Value = Fname
Range("D" & (40 + count)).Value = Ename
mailfile = Dir()
count = count - 1
Loop
End Sub
add a comment |
up vote
0
down vote
up vote
0
down vote
I ended up working a way around it. I was just spinning my wheels with the outlook controls and I decided to work with something I know better, end result was i set it up to read from a save folder where the files are saved as text files, and i have a formula where the contents of the searched files drop to match them against sent emails.
Sub UpdateReturns()
Dim stPath As String, mailfile As String, Fname As String, Ename As String
Dim fso As Object, fld As Object
Dim i As Integer, count As Integer
Const strSearch = "From:"
stPath = "C:10. Working Docs"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(stPath)
mailfile = Dir(stPath & "Approve*.txt")
count = 0
Do While mailfile <> ""
count = count + 1
mailfile = Dir()
Loop
i = 0
mailfile = Dir(stPath & "Approve*.txt")
Do While count >= 1
Open mailfile For Input As #1
Do Until EOF(1)
Line Input #1, textline
If InStr(textline, "From:") > 0 Then
Fname = mailfile
Ename = textline
End If
Loop
Close #1
Range("C" & (40 + count)).Value = Fname
Range("D" & (40 + count)).Value = Ename
mailfile = Dir()
count = count - 1
Loop
End Sub
I ended up working a way around it. I was just spinning my wheels with the outlook controls and I decided to work with something I know better, end result was i set it up to read from a save folder where the files are saved as text files, and i have a formula where the contents of the searched files drop to match them against sent emails.
Sub UpdateReturns()
Dim stPath As String, mailfile As String, Fname As String, Ename As String
Dim fso As Object, fld As Object
Dim i As Integer, count As Integer
Const strSearch = "From:"
stPath = "C:10. Working Docs"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(stPath)
mailfile = Dir(stPath & "Approve*.txt")
count = 0
Do While mailfile <> ""
count = count + 1
mailfile = Dir()
Loop
i = 0
mailfile = Dir(stPath & "Approve*.txt")
Do While count >= 1
Open mailfile For Input As #1
Do Until EOF(1)
Line Input #1, textline
If InStr(textline, "From:") > 0 Then
Fname = mailfile
Ename = textline
End If
Loop
Close #1
Range("C" & (40 + count)).Value = Fname
Range("D" & (40 + count)).Value = Ename
mailfile = Dir()
count = count - 1
Loop
End Sub
answered Nov 9 at 0:35
craig P
1
1
add a comment |
add a comment |
up vote
0
down vote
Dir returns a string that represents the file name.
Sub UpdateReturns()
Dim fso As Object
Dim fld As Object
Dim olApp As Object
Dim MailFile As Object
Dim MailFileStr As String
Dim MailPathFileStr As String
Dim stPath As String
Dim EmailFrom As String
stPath = "C:10. Working Docs"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(stPath)
Set olApp = CreateObject("Outlook.Application")
MailFileStr = Dir(stPath & "*.msg")
' file name only, no path
Debug.Print "MailFileStr: " & MailFileStr
Do While MailFileStr <> ""
' path and file
MailPathFileStr = stPath & "" & MailFileStr
Debug.Print vbCr & "MailPathFileStr: " & MailPathFileStr
Set MailFile = olApp.Session.OpenSharedItem(MailPathFileStr)
EmailFrom = MailFile.SenderEmailAddress
Debug.Print "EmailFrom: " & EmailFrom
Set MailFile = Nothing
MailFileStr = Dir ' Get next entry.
Loop
End Sub
add a comment |
up vote
0
down vote
Dir returns a string that represents the file name.
Sub UpdateReturns()
Dim fso As Object
Dim fld As Object
Dim olApp As Object
Dim MailFile As Object
Dim MailFileStr As String
Dim MailPathFileStr As String
Dim stPath As String
Dim EmailFrom As String
stPath = "C:10. Working Docs"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(stPath)
Set olApp = CreateObject("Outlook.Application")
MailFileStr = Dir(stPath & "*.msg")
' file name only, no path
Debug.Print "MailFileStr: " & MailFileStr
Do While MailFileStr <> ""
' path and file
MailPathFileStr = stPath & "" & MailFileStr
Debug.Print vbCr & "MailPathFileStr: " & MailPathFileStr
Set MailFile = olApp.Session.OpenSharedItem(MailPathFileStr)
EmailFrom = MailFile.SenderEmailAddress
Debug.Print "EmailFrom: " & EmailFrom
Set MailFile = Nothing
MailFileStr = Dir ' Get next entry.
Loop
End Sub
add a comment |
up vote
0
down vote
up vote
0
down vote
Dir returns a string that represents the file name.
Sub UpdateReturns()
Dim fso As Object
Dim fld As Object
Dim olApp As Object
Dim MailFile As Object
Dim MailFileStr As String
Dim MailPathFileStr As String
Dim stPath As String
Dim EmailFrom As String
stPath = "C:10. Working Docs"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(stPath)
Set olApp = CreateObject("Outlook.Application")
MailFileStr = Dir(stPath & "*.msg")
' file name only, no path
Debug.Print "MailFileStr: " & MailFileStr
Do While MailFileStr <> ""
' path and file
MailPathFileStr = stPath & "" & MailFileStr
Debug.Print vbCr & "MailPathFileStr: " & MailPathFileStr
Set MailFile = olApp.Session.OpenSharedItem(MailPathFileStr)
EmailFrom = MailFile.SenderEmailAddress
Debug.Print "EmailFrom: " & EmailFrom
Set MailFile = Nothing
MailFileStr = Dir ' Get next entry.
Loop
End Sub
Dir returns a string that represents the file name.
Sub UpdateReturns()
Dim fso As Object
Dim fld As Object
Dim olApp As Object
Dim MailFile As Object
Dim MailFileStr As String
Dim MailPathFileStr As String
Dim stPath As String
Dim EmailFrom As String
stPath = "C:10. Working Docs"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(stPath)
Set olApp = CreateObject("Outlook.Application")
MailFileStr = Dir(stPath & "*.msg")
' file name only, no path
Debug.Print "MailFileStr: " & MailFileStr
Do While MailFileStr <> ""
' path and file
MailPathFileStr = stPath & "" & MailFileStr
Debug.Print vbCr & "MailPathFileStr: " & MailPathFileStr
Set MailFile = olApp.Session.OpenSharedItem(MailPathFileStr)
EmailFrom = MailFile.SenderEmailAddress
Debug.Print "EmailFrom: " & EmailFrom
Set MailFile = Nothing
MailFileStr = Dir ' Get next entry.
Loop
End Sub
answered Nov 9 at 20:24
niton
5,38171941
5,38171941
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53201461%2fdir-generates-compile-error-looping-through-msg-files-in-a-folder%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You're trying to use
MailFile
as both aString
- inMailFile = Dir...
, and as anObject
- inSet MailFile = ....
. Add a new variable for the first perhaps.– BigBen
Nov 8 at 4:27
Yeah i tried that after i posted this, but i then get an error on the MsgDetail line saying "Object doesn't support this property or method"
– craig P
Nov 8 at 4:55
That's because you need to use
olApp
.Application
refers to the Excel application.– BigBen
Nov 8 at 5:00
And you never do anything with the message returned by OpenSharedItem - you always use the message from ActiveInspector.CurrentItem
– Dmitry Streblechenko
Nov 8 at 17:39