Results 1 to 9 of 9

Thread: Word automation

  1. #1

    Thread Starter
    Hyperactive Member Davadvice's Avatar
    Join Date
    Apr 2007
    Location
    Glasgow (Scotland)
    Posts
    440

    Question Word automation

    hi,

    I'm looking to automate word to allow users to send letters to customers. i am using access 2k and work 2k.

    I have automated excel and outlook functions with VBA before however i have never done anything with word.

    Is it worth while using word for the task, i have seen word automated previously and it always seemed to fail and the code looked gastly. i do have to say the guy the coded it was not know for best practice.


    I have a mege document and only need to use 5 items from a dataset.

    is there an easy way to run the mailmerge using VBA ? does anyone have any good tut's on the subject ?

    thanks in advance

    David
    This is Blank

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

    Re: Word automation

    see the tutorial in the faq at the top of the forum
    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

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

    Re: Word automation

    Basic Word Mailmerge setup:
    http://www.vbforums.com/showthread.php?t=402097

    I have another thread where I get into the automation of the mailmerge sql and invoking etc. I'll look for it.
    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
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Word automation

    I couldnt find it so I wrote up a basic FAQ on executing the mailmerge from VB6 but its the same code for from within Access.

    How do I execute a Word MailMerge document?
    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

  5. #5

    Thread Starter
    Hyperactive Member Davadvice's Avatar
    Join Date
    Apr 2007
    Location
    Glasgow (Scotland)
    Posts
    440

    Re: Word automation

    Rob,

    thanks for your help on this, i will take a proper look later once i get 5 minutes.

    David
    This is Blank

  6. #6

    Thread Starter
    Hyperactive Member Davadvice's Avatar
    Join Date
    Apr 2007
    Location
    Glasgow (Scotland)
    Posts
    440

    Re: Word automation

    Rob,

    Thanks for the code above, worked a treat for the most part.

    I have one issue, when I open my Datasource (an MDB file) the database window is being left open after the print job has completed and all the word objects are closed and destroyed.

    The issue I have is that the application name for the datasource and the application initiating the mail merge are the same and if I close it using an API call then it may close the access session that I wish to leave open. Do you know if there is a way to alter the merge properties to prevent it showing the source?


    edit:I'm looking down the ODBC route now see if i can get round it that way.

    Thanks for your help

    David
    Last edited by Davadvice; Jul 20th, 2009 at 09:30 AM.
    This is Blank

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

    Re: Word automation

    So you are wanting to close and quit access after word prints? If so then try..

    Application.CloseCurrentDatabase
    Application.Quit

    Application will refer to your Access application
    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

    Thread Starter
    Hyperactive Member Davadvice's Avatar
    Join Date
    Apr 2007
    Location
    Glasgow (Scotland)
    Posts
    440

    Re: Word automation

    hi Rob,

    Not the instance of access running the process but the access db instance that is used for the datasource.

    for instance when i open the main app "debt manager" i navigate to the form for reporting

    I select the mailmerge item from a Combo and click run reports. this then opens a new instance of the Debt manager Application (to get the source for the mail merge) and it is this instance (the second instance) i want to close.

    it's not very clear i know sorry if you don't get what i'm saying.

    thanks

    David
    This is Blank

  9. #9
    Addicted Member
    Join Date
    Feb 2009
    Posts
    146

    Re: Word automation

    If I can borrow this thread:

    When I create a Word macro to create an Outlook item, I just couldnt have several words after & (only the next one) in the subject field

    example:
    .Subject = "Open file, " & Kon1 & " " & Rapn

    This results in only (in the subject field): Open file, BlaForKon1

    What I am trying to tell is that nothing after Kon1 is in the subject field when the email is created.

    But when I macro in outlook to create an email item everything is included.


    Why is there a problem with this in Word? How can I include more than one string in the subject field after the &:s (and)?

    See the full code of email item:

    Code:
        Dim OutlookMessage As Object
        Set OutlookMessage = CreateObject("Outlook.application").createitem(0)
        
        With OutlookMessage
    
        .Subject = "Open file " & Kon1 & " " & Rapn
        .body = Wordtext
        .to = "SUES" 
        .SentOnBehalfOfName = "SUES"
        .HTMLBody = mailBody
        .Display 
        End With
        Set OutlookMessage = Nothing
    Best Regards
    Groom

Tags for this Thread

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