Results 1 to 8 of 8

Thread: MS Word Command button help

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    5

    MS Word Command button help

    Hey Everyone,

    I hope someone can help with this and I apologize if there is something on here just like this, I did try searching.

    I have a form in a Word Document that has a Command Button that is acting as a " Submit " button that when is clicked will attach the document to a new mail message in Outlook to be returned to me through email. I was hoping there is a way to make it so that when the button is clicked, it attaches the document as well as inserts the email address that I am reached at automatically so that the sender only has to click " SEND."

    The VB code I have for the Word Document so far is this:

    Private Sub CommandButton1_Click()
    Application.Options.ButtonFieldClicks = 1
    Options.SendMailAttach = True
    ActiveDocument.SendMail

    End Sub

    Any help with this would be greatly appreciated. I have practically no experience working with VB.

    -Joe

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: MS Word Command button help

    This is VBA stuff, not VB.Net or Classic VB
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

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

    Re: MS Word Command button help

    Thread Moved
    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    5

    Re: MS Word Command button help

    Oh, Sorry about that. Like I said I don't really know anything about visual basic. Thanks for pointing me in the right direction though.

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

    Re: MS Word Command button help

    I havent seem a way to populate everything using it but if you were to use Outlook code you could but its more code. If you use the ShellExecute API you can but then you get issues with translating the attachment path with proper url encoding characters.

    Code:
    Option Explicit
    
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
                        ByVal hwnd As Long, _
                        ByVal lpOperation As String, _
                        ByVal lpFile As String, _
                        ByVal lpParameters As String, _
                        ByVal lpDirectory As String, _
                        ByVal nShowCmd As Long) As Long
    
    Private Const SW_HIDE As Long = 0
    Private Const SW_SHOWNORMAL As Long = 1
    Private Const SW_SHOWMAXIMIZED As Long = 3
    Private Const SW_SHOWMINIMIZED As Long = 2
    
    Private Sub CommandButton1_Click()
        Application.Options.ButtonFieldClicks = 1
        Options.SendMailAttach = True
        ShellExecute 0&, "open", "mailto:[email protected]?subject=spam&body=blah&attach=C%3A%5CMyDoc2.docx", vbNullString, "C:\", SW_SHOWNORMAL
    End Sub
    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
    New Member
    Join Date
    Jun 2008
    Posts
    8

    Re: MS Word Command button help

    When using the code provided - now the document is not attached to the email. I apologize if this is an overlook on my part, but I am very new to VB.

    Thank You

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

    Re: MS Word Command button help

    The attachment file path needs to be reflective of your system and change the invalid url characters into their encoded equilivalents.

    &attach=C:\MyDoc.docx

    &attach=C%3A%5CMyDoc2.docx
    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

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: MS Word Command button help

    i saw a thread a few days ago that showed how to use, the routingslip property of the document to set the email address to send to using activedocument.sendmail, this is only for word prior to 2007
    http://www.vbforums.com/showthread.p...94#post3272494
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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