Consulting

Results 1 to 9 of 9

Thread: Email and attach macro

  1. #1
    VBAX Regular
    Joined
    Feb 2018
    Posts
    7
    Location

    Email and attach macro

    I have the below macro to execute a command that saves document to original location and sends an email using textfield content to determine subject

    I need to change so that it does not save the document just attaches and that the file name is pulled from textbox1 field content

    vba code.docx

  2. #2
    You will have to save the document in order to attach it - even if you save a temporary copy of it and delete it afterwards,
    What sort of field is textbox1?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Feb 2018
    Posts
    7
    Location
    just a text name field, could I set a default save location to auto save them to

  4. #4
    Just a text name field?

    ActiveX?
    Legacy Form Field?
    Content Control?
    Userform TextBox?

    You can save to the Temporary folder

    Dim strPath as String
    strPath = Environ("TEMP") & "\"
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Regular
    Joined
    Feb 2018
    Posts
    7
    Location
    userform textbox

  6. #6
    In that case something like

    Private Sub Submit_Click()
    'No-option email sending
    Dim OL As Object
    Dim EmailItem As Object
    Dim strPath As String
    Dim strName As String
    Dim oDoc As Document
    
        Application.ScreenUpdating = True
        Set OL = CreateObject("Outlook.Application")
        Set EmailItem = OL.CreateItem(0)
        strPath = Environ("TEMP") & Chr(92)
    
        If TextBox1.Text = "" Then GoTo lbl_Exit
        strName = TextBox1.Text
        If Not Right(strName, 5) = ".docx" Then strName = strName & ".docx"
        Set oDoc = ActiveDocument
        oDoc.SaveAs2 FileName:=strPath & strName, AddToRecentFiles:=False
    
        With EmailItem
            .Subject = TextBox1 & "OVERVIEW OF SUPPORT FORM SUBMITTED"
            .Body = "Find attached Overview of support form." & vbCrLf & _
                    "" & vbCrLf & _
                    ""
            .to = "l2@bradfordcollege.ac.uk"
            .Importance = 0
            .Attachments.Add oDoc.FullName
            .send
        End With
        oDoc.Close 0
        Kill strPath & strName
        Application.ScreenUpdating = True
    lbl_Exit:
        Unload Me
        Set oDoc = Nothing
        Set OL = Nothing
        Set EmailItem = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    VBAX Regular
    Joined
    Feb 2018
    Posts
    7
    Location
    thanks for this, where would this save

  8. #8
    It would save in the User's Temp folder - enter %temp% in the address windows of Windows File Explorer.
    It would add the saved document to the message, send the message and then delete the saved document.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  9. #9
    VBAX Regular
    Joined
    Feb 2018
    Posts
    7
    Location
    Fantastic, works a treat. Thanks for your prompt and useful assistance.

Posting Permissions

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