+ Reply to Thread
Results 1 to 12 of 12

VBA producing unexpected results

  1. #1
    Registered User
    Join Date
    09-09-2008
    Location
    Troy, MI
    MS-Off Ver
    2010
    Posts
    95

    VBA producing unexpected results

    Hello,

    I am writing VBA coded to execute a macro that attaches a file to an email. Within the code, I use the .Attachments.Add command along with the directory path of where the attachment resides. All works fine and the macro does its job by attaching the chosen file to the email as expected, except when one of the subdirectories is named with two words separated by a space.

    For example, when the directory path is c:\users\tom\documents\test.pdf, the macro attaches the file successfully to the email. However, when the directory path is c:\users\tom\test files\test.pdf and the subdirectory "test files" name contains a space between the two words, the macro is unable, or won't, attach the file to the email.

    Please comment if you know of a reason why the macro does not perform as expected. Does there need to be a character to be placed between the two words when I type the .Attachments.Add command and add the directory path where the file I want to attach is located? I'm stuck.

  2. #2
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Re: VBA producing unexpected results

    Good morning trundle

    Try adding an extra set of quotes around the path, so :
    Please Login or Register  to view this content.
    becomes
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    HTH

    DominicB

  3. #3
    Registered User
    Join Date
    09-09-2008
    Location
    Troy, MI
    MS-Off Ver
    2010
    Posts
    95

    Re: VBA producing unexpected results

    Thanks Dominic,

    Thank you for your reply and suggestion. The line of code is actually written as follows: .Attachments.Add "c:\users\Randle\TWR Documents\HMT Group Results Report Test.pdf". Where the subject space occurs in the name of the subdirectory "TWR Documents". So quotes are actually in use around the path. So I rewrote the line to add additional quotes as you suggest as follows: .Attachments.Add ""c:\users\Randle\TWR Documents\HMT Group Results Report Test.pdf"" and received the following error: Compile Error: Syntax Error.

    Where would I add the text you propose above? Would it be included as part of the above line or somewhere else in the set of code and would I need to include the DirPath = Chr (34) text as well?

  4. #4
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA producing unexpected results

    Hi,

    The code you posted should work, assuming the path is actually correct. Can you provide the actual macro code as well as the specific error message?
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  5. #5
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Re: VBA producing unexpected results

    Hi trundle

    Do as xlnitwit suggested and post more info, as he said, the code should work.

    Before you do just try :
    Please Login or Register  to view this content.
    HTH

    DominicB

  6. #6
    Registered User
    Join Date
    09-09-2008
    Location
    Troy, MI
    MS-Off Ver
    2010
    Posts
    95

    Re: VBA producing unexpected results

    Dominic,

    I tried adding the DirPath line as you demonstrated above and immediately received the following error: "Compile Error: Expected end of statement:" I guess the editor did not like the double quotes together. Then, I removed one set of quotes from the path statement. The error went away and I ran the macro using the new DirPath text. All I got was an email shell set up with the addressees I wanted along with the body of the email, but no attachment. So I'm back to square one.

    Since Dominic's suggestion did not work, I am following xlnitwit's advice and have pasted my macro code below. Also, I have verified that the path is correct. Apologies, but I know I'm supposed to copy and past code a certain way (using some sort of tags I think?) in order for it to be properly read. I did not do that here, so if someone will tell me how I need to paste the code, I will do so and resubmit my response.

    Sub ResultsReporteMail()

    Dim Answer As String
    Dim Question As String

    'opens message box and asks user to confirm they want to send email
    Question = "Do you want to send the HMT Group Weekly Results Report to everyone?"

    Answer = MsgBox(Question, vbQuestion + vbYesNo, "eMail Registration to Regulars")

    If Answer = vbNo Then
    Exit Sub
    End If

    Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next

    'eMail settings have been setup to reference cells in the Configuration worksheet
    With OutMail
    .To = Worksheets("Configuration").Range("E28")
    .CC = Worksheets("Configuration").Range("E30")
    .BCC = Worksheets("Configuration").Range("E31")
    .Subject = Worksheets("Configuration").Range("C32")
    .Body = Worksheets("Configuration").Range("C33")
    DirPath = "c:\users\Randle\TWR Documents\HMT Group Results Report Test.pdf"
    .Attachments.Add DirPath
    .Display
    '.Send
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing


    End Sub

  7. #7
    Registered User
    Join Date
    09-09-2008
    Location
    Troy, MI
    MS-Off Ver
    2010
    Posts
    95

    Re: VBA producing unexpected results

    Dominic,

    I tried adding the DirPath line as you demonstrated above and immediately received the following error: "Compile Error: Expected end of statement:" I guess the editor did not like the double quotes together. Then, I removed one set of quotes from the path statement. The error went away and I ran the macro using the new DirPath text. All I got was an email shell set up with the addressees I wanted along with the body of the email, but no attachment. So I'm back to square one.

    Since Dominic's suggestion did not work, I am following xlnitwit's advice and have pasted my macro code below. Also, I have verified that the path is correct. Apologies, but I know I'm supposed to copy and past code a certain way (using some sort of tags I think?) in order for it to be properly read. I did not do that here, so if someone will tell me how I need to paste the code, I will do so and resubmit my response.

    Please Login or Register  to view this content.

  8. #8
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA producing unexpected results

    Remove the On Error Resume Next line and then run the code again, please, and tell us if you see any error messages.

  9. #9
    Registered User
    Join Date
    09-09-2008
    Location
    Troy, MI
    MS-Off Ver
    2010
    Posts
    95

    Re: VBA producing unexpected results

    I removed the "On Error Resume Next" code as suggested and reran the macro. I received the following error message: "Run-Time error '-2147024893 (80070003)': Path does not exist. Verify the path is correct.". Next I went to where the attachment file is stored and right-clicked on Properties and found the location listed was: c:\Users\Randle\Documents. I revised the macro code to now read "c:\users\Randle\Documents\HMT Group Results Report Test.pdf" and reran the macro. This time it worked perfectly and not only created the email but also attached the HMT Group Results Report Test.pdf file!

    So, you were correct in that I had the path listed incorrectly. However, in the file explorer, the name of the folder where the attachment file is located is "TWR Documents" and not "Documents". It appears the issue has been resolved, yet I have a couple of questions. Why does the name of the folder appear to not match the path listed for the file under the Properties view? Second, I take it the "On Error Resume Next" code is useless and unnecessary?

  10. #10
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA producing unexpected results

    On Error Resume Next is extremely useful when used properly- which should generally be to trap a foreseeable error (such as a sheet not existing) and it should be limited to as short an extent as possible. All too often it is simply slapped into some code to stop an "annoying" message without realising that it will also mask any serious errors that may occur.

    Regarding the folder path, it may be that it is some sort of virtual folder mapping.

  11. #11
    Registered User
    Join Date
    09-09-2008
    Location
    Troy, MI
    MS-Off Ver
    2010
    Posts
    95

    Re: VBA producing unexpected results

    Many thanks to xlnitwit and to Dominicb for your assistance in solving my issue. You really helped me out of a bind and the ultimate solution worked perfectly!

  12. #12
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA producing unexpected results

    You're welcome- glad we could be of help. Thank you for the rep too.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] range.value = var ...unexpected results.
    By excel_joe in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-22-2016, 02:59 PM
  2. [SOLVED] Unexpected results from MSQuery
    By j_Southern in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-10-2014, 11:09 AM
  3. Unexpected results from CurrentRegion
    By ajgully in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-29-2011, 02:55 PM
  4. VLOOKUP with unexpected N/A results
    By cgjones in forum Excel General
    Replies: 2
    Last Post: 03-15-2011, 04:39 PM
  5. Unexpected HLOOKUP results
    By scaffdog845 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-06-2009, 03:01 PM
  6. Unexpected Results with CurrentRegion
    By avveerkar in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-06-2007, 09:57 PM
  7. Autofilter giving unexpected results!
    By Colin Vicary in forum Excel General
    Replies: 3
    Last Post: 09-28-2006, 07:20 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1