[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 ¿
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
Bookmarks