CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    [RESOLVED] VBA Access Macro to Print Word Document

    Hey guys!

    I have an Access 2003 database. In this database we input start dates for when a student starts with a certain course, and end dates. What I want to do ( to make my life so much easier ) is to write a Macro to automatically print that particular subject's Exam.

    The Macros Option Access provides are not very helpful here. So, I decided to write the VBA code inside a module. The code I wrote was this :
    Code:
    Public Sub RunWordMacroOrSub()
    
    'Check Tools/Reference Microsoft Word Object Library
    'declare variables
    Dim wdApp As Word.Application
    Dim wdDoc As Word.Document
    
    'Word application stuff
    Set wdApp = New Word.Application
    wdApp.Visible = False
    Set wdDoc = wdApp.Documents.Open("Blah Blah Blah.doc")
    wdDoc.PrintOut
    
    'done
    wdApp.Quit
    Set wdDoc = Nothing
    Set wdApp = Nothing
    
    End Sub
    Problem now is, How do I Run / execute this code from an Access Macro ¿
    I have tried the RunCode option like this :
    Code:
    Modules![Test1]!RunWordMacroOrSub
    Nothing happens, not even an error.

    Does anyone have any idea what I did wrong, or can anyone suggest a better way to print a Word document from Access ¿

    Any help would be greatly appreciated.

    Hannes

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: VBA Access Macro to Print Word Document

    OK, solved. I always get insecure and too excited too soon

    The RunCode method of the Access Macro, cannot run Subs, it has to be a Function. I changed my code in the Module to :
    Code:
    Public Function RunWordMacroOrSub()
    
    'Check Tools/Reference Microsoft Word Object Library
    'declare variables
    Dim wdApp As Word.Application
    Dim wdDoc As Word.Document
    
    'Word application stuff
    Set wdApp = New Word.Application
    wdApp.Visible = False
    Set wdDoc = wdApp.Documents.Open("C:\Documents and Settings\hannes\Desktop\Blah Blah Blah.doc")
    wdDoc.PrintOut Copies:=1
    
    'done
    wdApp.Quit
    Set wdDoc = Nothing
    Set wdApp = Nothing
    
    End Function
    And then Used RunCode like this :
    Code:
    RunWordMacroOrSub()
    Voila! It works
    I just don't understand the whole logic of having it as a Function, after all, it does not return any value

    Thanx anyways everyone !

  3. #3
    Join Date
    Feb 2002
    Location
    Makati City, Philippines
    Posts
    1,054

    Re: [RESOLVED] VBA Access Macro to Print Word Document

    Opps, I was late. I was actually previewing the post (reply) when i noticed it has been resolved. Anyway I suggested a commandbutton ...
    Code:
    Private Sub Command0_Click()
        RunWordMacroOrSub
    End Sub
    Yes youre right in what you have discovered. This "Modules![Test1]!RunWordMacroOrSub" will run Macro but not a VBA sub (which you have converted to).
    Marketing our skills - please participate in the survey and share your insights
    -

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: [RESOLVED] VBA Access Macro to Print Word Document

    Thanx anyways for your efforts!

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

Featured