Here is code for printing a report in Access and Word from within VB
Note you must first reference 'Microsoft Access 8.0 Object Library' and 'Microsoft Word 9.0 Object'
The version may be different depending on what version of Access and Word you have (You must have these programs already installed for this to work)
Here is for WordCode:Dim appAccess As Access.Application Set appAccess = New Access.Application'these set up the object appAccess.OpenCurrentDatabase "I:\Payroll\Payroll.mdb"'put the database path here appAccess.Run "SetParam", CDate(txtPPE.Text)'this runs a macro in access named setparam appAccess.DoCmd.OpenReport "Payroll New Full", acViewNormal'this actually prints the report appAccess.Quit'quits access Set appAccess=Nothing'frees up memory
Of course this is just printing but once you open the db or doc you can use any VBA commands like to maximize a form in Access add this after the OpenForm (where my openReport is) line:Code:Dim appWord As Word.Application Set appWord = New Word.Application'these set up the object appWord.Documents.Open "I:\Payroll\Payroll Forms Access.doc", , , , , , , , , , , True'put document path here appWord.Run "Payroll", CDate(txtPPE.Text)'runs a macro in word appWord.ActiveDocument.Close'closes appWord.Quit'quits word set appWord=Nothing'frees memory
Does that help, if it is confusing just email meCode:appAccess.DoCmd.Maximize
Note: Things to watch for with subclassing (that what this is called) Word and printing if you try to quit while it is still sending to the printer then it prompts the user about aborting the print.




Reply With Quote