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









share|improve this question
























  • 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










  • 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















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









share|improve this question
























  • 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










  • 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













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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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










  • 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


















  • 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










  • 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
















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












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





share|improve this answer




























    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





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      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

























      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





      share|improve this answer

























        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





        share|improve this answer























          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





          share|improve this answer












          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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 9 at 0:35









          craig P

          1




          1
























              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





              share|improve this answer

























                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





                share|improve this answer























                  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





                  share|improve this answer












                  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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 9 at 20:24









                  niton

                  5,38171941




                  5,38171941






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      這個網誌中的熱門文章

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Xamarin.form Move up view when keyboard appear

                      JBPM : POST request for execute process go wrong