Results 1 to 7 of 7

Thread: [RESOLVED] VBA Macro to save a document then email as an attachment

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    36

    Resolved [RESOLVED] VBA Macro to save a document then email as an attachment

    Hi, is it possible to have one button on a word document that asks the user for a filename, saves the document using the filename and then emails the document as an attachment to a specific person.

    If it is, could someone please explain, thanks

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VBA Macro to save a document then email as an attachment

    Yes, if you have an email client installed on th system.

    First, use the .SendMailAttach to identify that it needs to be attached and not part of the email body.
    Then, invoke a SendMail method.

    VB Code:
    1. ActiveDocument.SaveAs FileName:="C:\Test.doc" 'Add  more code for the saveas dialog browse
    2. Options.SendMailAttach = True
    3. ActiveDocument.SendMail
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    36

    Re: VBA Macro to save a document then email as an attachment

    Hi, thanks for the reply

    I must be doing soemthing wrong, entered the code;

    Private Sub Commandbutton2.Click()
    SaveFileName= _
    InputBox( _
    "Enter a name for the letter", _
    "Letter name ?")
    If SaveFileName <> "" _
    Then
    ActiveDocument.SaveAs (SaveFileName)
    Options.SendMailAttach = True
    ActiveDocument.SendMail
    End Sub

    The document is saved as a .dot file, so when I double click on the file it loads it as Document.doc, but when I click on the button it doesn't do much, when I double click on it it loads the text above in Microsoft Visual Basic interface.

    ANy ideas where I'm going wrong?

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    36

    Re: VBA Macro to save a document then email as an attachment

    Ignore me, macro's were set to high, sorry about that, everything now works wonderfully and your code example is just what I need, thanks again, appreciate it.

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] VBA Macro to save a document then email as an attachment

    Why are you trying to save it as a template?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    36

    Re: [RESOLVED] VBA Macro to save a document then email as an attachment

    No I think you misunderstand, the document is a template, selectable by the user which when the double click on it then opens it has a document.doc file rather than the .dot, just easier than risking the user saving over the original. Thanks again for your input, now working fine, cheers

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] VBA Macro to save a document then email as an attachment

    Gotcha
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width