dcsimg
Results 1 to 12 of 12

Thread: eFile Move to Folder-No longer remembers last external folder path and resets to same

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2018
    Posts
    3

    eFile Move to Folder-No longer remembers last external folder path and resets to same

    Hi,

    Newbie here!

    I have been using a macro for years that allows me to externally file selected e-mails to any network folder, and when launched it remembered the last folder location used, saving you having to browse the entire folder structure again!

    Since a windows update earlier this year this functionality has been lost and it keeps defaulting to a fixed folder annoyingly. This is on a windows 7 machine. I tried installing this macro on my windows 10 laptop though it always had this same problem. I think its to do with Microsoft changing how folders are referenced for security reasons, though this is beyond me to fix.

    I'm happy to share the final version with anyone who wants a copy of this script which is extremely useful. I can send you the entire .OTM file ir required.

    Really hoping somebody can help.

    Thanks in advance.

    Robsbee



    Here is the main body of the macro:

    Code:
    Private Sub ComboBox1_Change()
    
    End Sub
    
    Private Sub ComboBox2_Change()
    
    End Sub
    
    Private Sub CommandButton1_Click()
    Dim sPath As String
    sPath = modBrowseFolder.BrowseForFolder
    If Len(sPath) > 0 Then
    UserForm1.textbox1.Value = sPath
    End If
    folderdef = UserForm1.textbox1
    Set fs = CreateObject("Scripting.FileSystemObject")
    If fs.folderExists(folderdef) Then
        Set f = fs.Getfolder(folderdef)
        a = f.Attributes
        Select Case a
        Case 17, 49
            msg1 = Chr(13) & "Folder " & folderdef & "is set as read only"
            msg2 = Chr(13) & "Please check the permissions or select a new folder"
            msg3 = Chr(13) & "No messages have been moved or deleted"
            Msg = (msg1 & msg2 & msg3)
            Style = vbOKOnly + vbInformation  ' Define buttons.
            Title = "FileEmail-Bad Access"    ' Define title.
            Response = MsgBox(Msg, Style, Title)
        End Select
    End If
    
    End Sub
    
    Private Sub Cancel_Click()
    UserForm1.hide
    End Sub
    
    Private Sub Help_Click()
    UserForm3.Show
    End Sub
    
    Private Sub FileEmail_Click()
    On Error Resume Next
    Dim myOlApp As New Outlook.Application
    Dim myOlExp As Outlook.Explorer
    Dim myOlSel As Outlook.Selection
    Dim Msg, Style, Title, Help, Ctxt, Response, MyString
    Dim MsgTxt As String
    Dim TextTo As String
    txtfilter = """:;/\,.?*<>|&"
    folderdef = UserForm1.textbox1
    'check for last \
    If Right(folderdef, 1) <> "\" Then folderdef = folderdef & "\"
    Set fs = CreateObject("Scripting.FileSystemObject")
    If fs.folderExists(folderdef) Then
        a = ""
        testfile = folderdef & "test.txt"
        Open testfile For Output As #1
        Write #1, "writetest"
        Close #1
        Open testfile For Input As #1
        Input #1, a
        Close #1
        Kill testfile
        If a = "writetest" Then
            Open "C:\emailfolder.txt" For Output As #1    ' Open file for output.
            Write #1, folderdef   ' Write valid folder name
            Close #1    ' Close file.
            'process selection
            Set myOlExp = myOlApp.ActiveExplorer
            Set myOlSel = myOlExp.Selection
            'check source folder
            Set txtparent = myOlSel.Item(1).Parent
    
            For x = 1 To myOlSel.Count
                filname = myOlSel.Item(x).SenderName & "-" & myOlSel.Item(x).Subject
                filname = Trim(Left(filname, 60))
                For n = 1 To Len(txtfilter)
                    filname = Replace(filname, Mid(txtfilter, n, 1), "")
                Next n
                txtdate = myOlSel.Item(x).SentOn
                myOlSel.Item(x).SaveAs folderdef & Format(txtdate, "yymmdd@hhmm") & "-" & filname & ".msg", olMSG
                UserForm1.Label1 = (Str(x) & " files copied from " & txtparent)
                myOlSel.Item(x).Delete
                UserForm1.Repaint
            Next x
            UserForm1.hide
            x = x - 1
            'end with message
            msg1 = Chr(13) & Str(x) & " files from " & txtparent & " are stored in " & folderdef & Chr(13)
            msg2 = Chr(13) & "Original Email messages from " & txtparent & " deleted"
            Msg = (msg1 & msg2) ' Define message.
            Style = vbOKOnly + vbInformation    ' Define buttons.
            Title = "FileEmail"    ' Define title.
            Response = MsgBox(Msg, Style, Title)
        Else
            msg1 = Chr(13) & "Unable to write to Folder " & folderdef
            msg2 = Chr(13) & "Please check the name and permissions"
            msg3 = Chr(13) & "or create a new folder"
            msg3 = Chr(13) & "No messages have been moved or deleted"
            Msg = (msg1 & msg2 & msg3 & msg4)
            Style = vbOKOnly + vbInformation  ' Define buttons.
            Title = "FileEmail-Bad Folder Name"    ' Define title.
            Response = MsgBox(Msg, Style, Title)
        End If
    End If
    
    End Sub
    
    
    
    Private Sub image_Click()
    
    End Sub
    
    Private Sub Image1_Click()
    
    End Sub
    
    Private Sub Label1_Click()
    
    End Sub
    
    Private Sub Label2_Click()
    
    End Sub
    
    Private Sub textbox1_Change()
    
    End Sub
    
    Private Sub UserForm_Click()
    
    End Sub
    Last edited by dday9; May 31st, 2018 at 08:33 PM.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,668

    Re: eFile Move to Folder-No longer remembers last external folder path and resets to

    Private Sub CommandButton1_Click()
    Dim sPath As String
    sPath = modBrowseFolder.BrowseForFolder
    i assume this is calling the folder browser you are referring to, but you have not included the code in the module, can you post that module?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2018
    Posts
    3

    Re: eFile Move to Folder-No longer remembers last external folder path and resets to

    Hi Westconn1,

    Thanks for your quick reply.

    Apologies - I really am a newbie, with only but the basic of understanding!

    Here is the first module (1 of 2) called modBorwseFolder:
    Code:
    Option Explicit
    
    '---------------------------------------------------
    ' WinAPI Declarations
    '---------------------------------------------------
    Private Declare Sub CoTaskMemFree _
        Lib "ole32.dll" ( _
            ByVal hMem As Long _
        )
    Private Declare Function SHBrowseForFolder _
        Lib "shell32" ( _
            lpbi As BrowseInfo _
        ) As Long
    Private Declare Function SHGetPathFromIDList _
        Lib "shell32" ( _
            ByVal pidList As Long, _
            ByVal lpBuffer As String _
        ) As Long
    Private Declare Function GetActiveWindow _
        Lib "user32" ( _
        ) As Long
    
    '-------------------------------------------------
    ' User-Defined Types
    '-------------------------------------------------
    Private Type BrowseInfo
        hwndOwner As Long
        pIDLRoot As Long
        pszDisplayName As String
        lpszTitle As String
        ulFlags As Long
        lpfnCallback As Long
        lParam As Long
        iImage As Long
    End Type
    
    '-------------------------------------------------
    ' Module-level Constants
    '-------------------------------------------------
    'used for SHBrowseForFolder APIs
    Const BIF_RETURNONLYFSDIRS = 1
    Const MAX_PATH = 260
    
    Function BrowseForFolder() As String
        Dim iNull As Integer
        Dim lpIDList As Long
        Dim lResult As Long
        Dim sPath As String
        Dim udtBI As BrowseInfo
        
        With udtBI
            .hwndOwner = GetActiveWindow&
            .lpszTitle = "Choose a folder for the email"
            .ulFlags = BIF_RETURNONLYFSDIRS
        End With
        
        lpIDList = SHBrowseForFolder(udtBI)
        If lpIDList Then
            sPath = String$(MAX_PATH, 0)
            lResult = _
                SHGetPathFromIDList _
                (lpIDList, sPath)
            Call CoTaskMemFree(lpIDList)
            iNull = InStr(sPath, _
                vbNullChar)
            If iNull Then _
                sPath = Left$(sPath, iNull - 1)
        End If
        BrowseForFolder = sPath
    End Function
    and here is the second 'module 1':
    Code:
    Sub MoveToFolder()
    On Error Resume Next
    Dim myOlApp As New Outlook.Application
    Dim myOlExp As Outlook.Explorer
    Dim myOlSel As Outlook.Selection
    UserForm2.Show
    'process selection
    Set myOlExp = myOlApp.ActiveExplorer
    Set myOlSel = myOlExp.Selection
    'check source folder
    Set txtparent = myOlSel.Item(1).Parent
    If myOlSel.Count Then
        UserForm2.hide
        UserForm1.Label1 = Str(myOlSel.Count) + " files selected from " & txtparent
        folderdef = "C:\"
        Open "C:\emailfolder.txt" For Input As #1    ' Open file for input.
        Input #1, folderdef   ' Read data.
        Close #1    ' Close file.
        UserForm1.textbox1 = folderdef
        UserForm1.Show
    Else
        MsgBox "Please select Mail items to move and restart"
    End If
    End Sub
    Private Sub UserForm_Activate()
        
    
    
    End Sub

    I can send you the whole VBA file, if that would help?

    Thanks so much for your help.

    Robsbee
    Last edited by si_the_geek; Jun 1st, 2018 at 04:30 PM. Reason: added Code tags

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,668

    Re: eFile Move to Folder-No longer remembers last external folder path and resets to

    you can set the starting folder (initial directory) in the code, using a callback, there are many hits if you google for it or search this or the vb6 forums here

    i generally use a shell object to display a browse for folder dialog, which i find to be much easier, so i do not have any code to hand to post for you, i would only copy some from a search to add to your existing browseforfolder procedure
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,024

    Re: eFile Move to Folder-No longer remembers last external folder path and resets to

    From my own code-collection:
    Code:
    'Add this code to your module
    Private Declare Function SendMessage Lib "user32" _
      Alias "SendMessageA" ( _
      ByVal hwnd As Long, _
      ByVal wMsg As Long, _
      ByVal wParam As Long, _
      lParam As Any) As Long
    'Consts for SendMessag
    Private Const BFFM_ENABLEOK = &H465
    Private Const BFFM_SETSELECTION = &H466
    Private Const BFFM_SETSTATUSTEXT = &H464
    'Consts for Callback-Events
    Private Const BFFM_INITIALIZED = 1
    Private Const BFFM_SELCHANGED = 2
    Private Const BFFM_VALIDATEFAILED = 3
    
    
    ' Address for Callback
    Public Function GetAddress(ByVal FuncAddress As Long) As Long
      GetAddress = FuncAddress
    End Function
    
    ' Receiving the Callbacks
    Public Function BCallbackProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal lParam As Long, ByVal lpData As Long) As Long
    Dim Retval As Long
    Dim TmpSelected As String * 260
    Static LastPath As String
        
        ' Dialogue-Events
        Select Case uMsg
            ' Show Dialogue
            Case BFFM_INITIALIZED
                ' Set the Startfolder
                SendMessage hwnd, BFFM_SETSELECTION, ByVal 1&, ByVal vbNullString
            
            ' User selects different folder
            Case BFFM_SELCHANGED
                ' Get Entry
                SHGetPathFromIDList lParam, TmpSelected
                LastPath = Left$(TmpSelected, InStr(1, TmpSelected, vbNullChar) - 1)
                
                ' Show selected folder in status-bar
                SendMessage hwnd, BFFM_SETSTATUSTEXT, ByVal 0&, ByVal "You've set " & LastPath & " as Folder"
            
            ' invalid entry in the textbox
            Case BFFM_VALIDATEFAILED
            ' get back to last chosen folder
                SendMessage hwnd, BFFM_SETSELECTION, ByVal 1&, ByVal LastPath
          
        End Select
    
    End Function
    
    'Change your code with:
    Function BrowseForFolder() As String
        Dim iNull As Integer
        Dim lpIDList As Long
        Dim lResult As Long
        Dim sPath As String
        Dim udtBI As BrowseInfo
        
        With udtBI
            .hwndOwner = GetActiveWindow&
            .lpszTitle = "Choose a folder for the email"
            .ulFlags = BIF_RETURNONLYFSDIRS
    .lpfnCallBack = GetAddress(AddressOf BCallbackProc)
        End With
    This is a snippet from my own codebank, which works.
    Obviuosly, i couldn't test it with your parameters
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,668

    Re: eFile Move to Folder-No longer remembers last external folder path and resets to

    i do not see anywhere in your code that you set an initial browse folder?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,024

    Re: eFile Move to Folder-No longer remembers last external folder path and resets to

    You're right!

    It's the lParam in the SendMessage under BFFM_INITIALIZED where you can assign the StartFolder
    Code:
       ' Dialogue-Events
        Select Case uMsg
            ' Show Dialogue
            Case BFFM_INITIALIZED
                ' Set the Startfolder
                SendMessage hwnd, BFFM_SETSELECTION, ByVal 1&, ByVal HereIsMyStartFolder
    EDIT: Since it's a Callback-function, "HereIsMyStartFolder" has to be a module-wide variable, assigned in the initial call
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,668

    Re: eFile Move to Folder-No longer remembers last external folder path and resets to

    works for me

    HereIsMyStartFolder needs to be dimensioned as string, else it will crash excel
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,024

    Re: eFile Move to Folder-No longer remembers last external folder path and resets to

    Quote Originally Posted by westconn1 View Post
    works for me

    HereIsMyStartFolder needs to be dimensioned as string, else it will crash excel
    Now there's an idea how to annoy the hell out of my boss..... *evilgrin*
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,668

    Re: eFile Move to Folder-No longer remembers last external folder path and resets to

    Now there's an idea
    annoyed me anyhow............

    as i said above, i like to use a shell object
    Code:
    Function sb()
    sb = CreateObject("shell.application").BrowseForFolder(0, "Choose a folder for the email", &H1, "c:\temp")
    End Function
    change c:\temp to your desired start folder
    Last edited by westconn1; Jun 12th, 2018 at 06:35 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11

    Thread Starter
    New Member
    Join Date
    May 2018
    Posts
    3

    Re: eFile Move to Folder-No longer remembers last external folder path and resets to

    Hi Wetconn1 and Zvoni,

    Thanks for all your replies and advise.

    What I really want is for it to remember the last folder location used, not to start at the same folder each time, and to start in that previous location whatever it was when last used. This used to work, but has somehow been disabled since a Mircrosoft update!

    Is the option to revert to last used folder no longer able to be implemented in Outlook?

    Thanks again for all your help,

    Robsbee

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,668

    Re: eFile Move to Folder-No longer remembers last external folder path and resets to

    you can save the last used folder to some persistent storage, then supply that to a variable for the dialog call

    you can either find some property in outlook (or even the subject of a dummy email) to store the folder or just create a textfile that you can reopen later, something like
    Code:
    ' get previous from file if it exists
    myfile = "c:\somefolder\lastpath.txt
    if len(dir(myfile)) > 0 then
        open myfile for input as 1
        HereIsMyStartFolder = input(lof(1), #1)   ' assign previous folder to variable in modBrowseFolder
        close 1
    end if
    sPath = modBrowseFolder.BrowseForFolder
    ' save selected path for next time
    open myfile for output as 1   ' overwrite if exists
    print #1, spath  
    close 1
    just change the path and filenames to suit yourself, as mentioned previously the variable for the callback must be publec and in the general section of the module and of type string, else your application will crash
    if you add this code in it will automatically save the path for next time after you select the folder
    you will also need to have added the code from zvoni of course to implement the callback
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width