[RESOLVED] VBA Access Macro to Print Word Document
Hey guys! :wave:
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
Re: VBA Access Macro to Print Word Document
OK, solved. I always get insecure and too excited too soon :lol: :D
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 :confused:
Thanx anyways everyone ! :) :thumb:
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).
Re: [RESOLVED] VBA Access Macro to Print Word Document
Thanx anyways for your efforts! :) :thumb: