none
regarding how can attach pdf file in email directly from userform vba RRS feed

  • Question

  • dears 

    good greeting 

    i have the following code for print directly the selected file(pdf,gpeg,etc) from userform

    Private Sub CommandButton46_Click()
    Dim ara As Range
        Dim strFile As String
        Set ara = Sheets("data").Range("A:A").Find(ListBox1, , xlValues, xlWhole)
        If Not ara Is Nothing Then
            strFile = Sheets("data").Cells(ara.Row, 32).Value
            Call emailFile(strFile)
        End If
    End Sub

    Sub PrintFile(strFile As String)
      If ShellExecute(Application.hwnd, "Print", _
          strFile, 0&, 0&, SW_SHOWNORMAL) < 33 Then
        MsgBox "Couldn't print file.", vbInformation
      End If
    End Sub

    but i want to creat a code to give me the following order

    when excute command button attach file directly to email (outlook)

    and became ready to send email with file attached

    how can i made this code ?

    regards...........


    Monday, March 12, 2018 6:31 AM

Answers

All replies

  • Hello TAREK SHARAF,

    Please check below code.

    Sub emailFile(STRfILE As String)
    Set olApp = CreateObject("Outlook.Application")
    Set olMail = olApp.createItem(olMailItem)
    olMail.Attachments.Add STRfILE
    olMail.display
    End Sub

    For more information about automation outlook from Excel, please refer to below link.

    Automating Outlook from a Visual Basic Application

    By the way, what's the current statue of below threads? If they are resolved, I would suggest you mark helpful reply or provide your solution and mark it to close the thread. If not, please follow up to let us know your current issue. Thanks for understanding.

    how can save the hijri date from textbox userform vba to excel sheet like a date not like a text

    regarding how saving the date from text to excel sheet without change it's format

    regarding how can viewing the date formt inside textbox as it without changing format

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, March 12, 2018 7:14 AM
  • dear MR.terry

    it's worked well

    thanks for your help my dear friend

    accept my greeting

    Monday, March 12, 2018 7:33 AM
  • dear terry 

    good greeting

    i want the last code to give me awrong message in case of there is no attachment

    regards........

    Monday, March 12, 2018 7:45 AM
  • Hello TAREK SHARAF,

    You could check if the file exist.

    You could do the check while calling the emailFile. If the file does not exist, it won't create the mail.

    If Dir(strFile) <> "" Then
        emailFile strFile
    Else
        MsgBox "File doesn't exist."
    End If

    You could also do the check while adding attachment. If the file does not exist, it will still create the mail but won't add attachments.

    Sub emailFile(strFile As String)
    Set olApp = CreateObject("Outlook.Application")
    Set olMail = olApp.createItem(olMailItem)
    If Dir(strFile) <> "" Then
        olMail.Attachments.Add strFile
    Else
        MsgBox "File doesn't exist."
    End If
    olMail.display
    End Sub

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 12, 2018 7:52 AM
  • dear MR/terry 

    i have made modified for the code same like below:

    Sub emailFile(strFile As String)
    Set olApp = CreateObject("Outlook.Application")
    Set olMail = olApp.createItem(olMailItem)
    If Dir(strFile) <> "" Then
        olMail.Attachments.Add strFile
    Else
        MsgBox "لايوجد مرفق."
    End If


    End Sub

    but it give the following error in case of there is no attachment,

    "path does not exist"

    can you modified to me please

    regards.......


    Monday, March 12, 2018 8:13 AM
  • Hello,

    Except missing "olMail.display", I did not see any error. What's wrong you get?

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, March 12, 2018 8:17 AM
  • DEAR MR TEERY

    THIS IS THE WRONG MESSAGE I GET WHEN THERE IS NO ATTACHMENT AND ALSO AFTER INSERT THE MISSING "OLMAIL.DISPLAY"

    Monday, March 12, 2018 8:50 AM
  • dear mr terry

    good greeting

    did you find any solution for this wrong message

    thanks alot for your help

    accept my greeting brother

    Tuesday, March 13, 2018 4:58 AM
  • Hello TAREK SHARAF,

    This issue is a little away from original issue. I would suggest you post a new thread for it.

    I would suggest you provide the full code which is called when you click the button.

    Besides, sharing a simply excel file with code will be more helpful.

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, March 13, 2018 6:02 AM