Consulting

Results 1 to 5 of 5

Thread: Exisiting VBA not working quite right after Win7 update

  1. #1
    VBAX Newbie
    Joined
    Sep 2014
    Posts
    3
    Location

    Exisiting VBA not working quite right after Win7 update

    Howdy all,

    I have an existing VBA module that moves service reports I get to folders on my hard drive.
    I recently got a new work laptop with Win7 (was on WinXP).
    Someone helped me with this code long ago, so I don't take the credit for it.

    Instead of saving service reports to a folder the VBA created if it doesn't already exist like this:

    C:/Service Reports/Customer Name/System ID


    It now creates and saves the service report to:
    C:/Service ReportCustomer Name/System ID <-- where Service Report and Customer Name are jammed together into 1 folder name.

    Any thoughts?

    Here is the code:

    [vba]
    Option Explicit
    Private olAttach As Attachment
    Private strFilename As String
    Private vFolderName As Variant
    Private strFolderName As String
    Private strSubFolderName As String
    Private Const strRootPath As String = "C:\SERVICE REPORTS"
    Sub SavePDFAttachments(olItem As Outlook.MailItem)
    On Error GoTo CleanUp
    If InStr(1, olItem.Subject, "Beckman Coulter Service Report Task - Service Request") > 0 Then
    vFolderName = Split(olItem.Subject, ",")
    strFolderName = Trim(vFolderName(2)) & "\"
    strSubFolderName = Replace(vFolderName(1), "System ID", "") & "\"
    strFolderName = strRootPath & strFolderName & "\"
    If olItem.Attachments.Count > 0 Then
    For Each olAttach In olItem.Attachments
    If Right(UCase(olAttach.FileName), 3) = "PDF" Then
    If Not FolderExists(strFolderName) Then MkDir strFolderName
    If Not FolderExists(strFolderName & strSubFolderName) Then MkDir strFolderName & strSubFolderName
    olAttach.SaveAsFile strFolderName & strSubFolderName & olAttach.FileName
    End If
    Next olAttach
    End If
    End If
    CleanUp:
    Set olAttach = Nothing
    End Sub
    Private Function FolderExists(ByVal PathName As String) As Boolean
    Dim nAttr As Long
    On Error GoTo NoFolder
    nAttr = GetAttr(PathName)
    If (nAttr And vbDirectory) = vbDirectory Then
    FolderExists = True
    End If
    NoFolder:
    End Function
    Sub Test()
    Dim olMsg As MailItem
    On Error Resume Next
    Set olMsg = ActiveExplorer.Selection.Item(1)
    SavePDFAttachments olMsg
    End Sub
    [/vba]
    Last edited by WolfB; 09-10-2014 at 09:15 AM. Reason: vba

  2. #2
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    Try

    Private Const strRootPath As String = "C:\SERVICE REPORTS\"
    If that is not it, then provide a sample of a complete subject line.
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

  3. #3
    VBAX Newbie
    Joined
    Sep 2014
    Posts
    3
    Location
    Skatonni,

    That worked, mostly.
    I can't believe I missed that \.

    Now it works, but when it creates the System ID folder, it is placing a space in front of the System ID, thus creating a whole new folder instead of placing the report in the existing folder.

    Subject line sample:
    Service Report Task - Service Request #22611093, System ID 1538083, DOCTORS CLINIC

  4. #4
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    Remove spaces at the ends with Trim.

    strSubFolderName = Trim(Replace(vFolderName(1), "System ID", "")) & "\"
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

  5. #5
    VBAX Newbie
    Joined
    Sep 2014
    Posts
    3
    Location
    Thanks again, skatonni.

    That looks to have fixed it.

    Would that have been different in WinXP?
    I had just copied the code over, and missed the \ when changing the main directory.
    Last edited by WolfB; 09-10-2014 at 03:22 PM. Reason: solved

Posting Permissions

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