Printing a Word document from Access

G

Guest

I am trying to create a command button in an Access form to open a Word
document and print it to file. I have copied the Application.PrintOut command
which works in a Word macro, but Access VBA gives error message 'Compile
error. Method or data member not found.' I have checked that all the
references in Word VBA are also installed in Access VBA. Any suggestions? The
code is:

Private Sub Command157_Click()
Dim PrnDocName As String
PrnDocName = "c:\my documents 2\word
documents\electronicincorporation\memarts\DM" & _
Me![EnvelopeNumber] & ".MEM"

MergeNoPrompts "MEMART"

If Len(Dir$(PrnDocName)) > 0 Then
SetAttr PrnDocName, vbNormal
Kill PrnDocName
End If

ActivePrinter = "HP LaserJet IIP"
Application.PrintOut FileName:="MEMART", Range:=wdPrintRangeOfPages,
Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="2-20", PageType:= _
wdPrintAllPages, ManualDuplexPrint:=False, Collate:=True,
Background:= _
True, PrintToFile:=True, PrintZoomColumn:=0, PrintZoomRow:=0, _
PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0,
OutputFileName:=PrnDocName, _
Append:=False
ActiveDocument.Close (wdDoNotSaveChanges)

End Sub

Thanks
Dudley
 
G

Guest

Hi,
you first have to establish an instance of word which you can automate with
VBA.
I don't see you do this anywhere, so try something like this:

Set objWord = CreateObject("Word.Application")
objWord.Documents.Open (Yourpath/Filename)
objWord.Visible = False
objWord.PrintOut

objWord.Documents.Close
objWord.Quit
Set objWord = Nothing

This is a sample of how to automate word from within access to print the
document specified in 'YourPath/FileName'!
HTH
Good luck
 
G

Guest

Hi, Dudley.
I have checked that all the
references in Word VBA are also installed in Access VBA. Any suggestions?

The references that are higher in the list of references in the References
window take precedence over those references which are lower when both
references contain classes of the same name. Both Access and Word contain
the Application class, so if you want to use the Word Application class,
you'll need to disambiguate it by using the full name of the class, which
includes the library name. The standard programming convention in VB is to
declare a variable of the correct data type and use that variable throughout
the procedure. For example:

Dim wordApp As Word.Application

' . . . Other code.

wordApp.PrintOut FileName:="MEMART", Range:=wdPrintRangeOfPages, _
Item:= wdPrintDocumentContent, Copies:=1, Pages:="2-20", PageType:= _
wdPrintAllPages, ManualDuplexPrint:=False, Collate:=True,
Background:= _
True, PrintToFile:=True, PrintZoomColumn:=0, PrintZoomRow:=0, _
PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0, _
OutputFileName:=PrnDocName, _
Append:=False
wordApp.ActiveDocument.Close (wdDoNotSaveChanges)

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
http://www.Access.QBuilt.com/html/expert_contributors2.html for contact info.


Dudley said:
I am trying to create a command button in an Access form to open a Word
document and print it to file. I have copied the Application.PrintOut command
which works in a Word macro, but Access VBA gives error message 'Compile
error. Method or data member not found.' I have checked that all the
references in Word VBA are also installed in Access VBA. Any suggestions? The
code is:

Private Sub Command157_Click()
Dim PrnDocName As String
PrnDocName = "c:\my documents 2\word
documents\electronicincorporation\memarts\DM" & _
Me![EnvelopeNumber] & ".MEM"

MergeNoPrompts "MEMART"

If Len(Dir$(PrnDocName)) > 0 Then
SetAttr PrnDocName, vbNormal
Kill PrnDocName
End If

ActivePrinter = "HP LaserJet IIP"
Application.PrintOut FileName:="MEMART", Range:=wdPrintRangeOfPages,
Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="2-20", PageType:= _
wdPrintAllPages, ManualDuplexPrint:=False, Collate:=True,
Background:= _
True, PrintToFile:=True, PrintZoomColumn:=0, PrintZoomRow:=0, _
PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0,
OutputFileName:=PrnDocName, _
Append:=False
ActiveDocument.Close (wdDoNotSaveChanges)

End Sub

Thanks
Dudley
 
G

Guest

Hi Oliver and Gunny,

Many thanks to both of you. I combined the advice of both as

Dim objWord As Word.Application
Set objWord = CreateObject("Word.Application")

and it worked fine.

Regards
Dudley

'69 Camaro said:
Hi, Dudley.
I have checked that all the
references in Word VBA are also installed in Access VBA. Any suggestions?

The references that are higher in the list of references in the References
window take precedence over those references which are lower when both
references contain classes of the same name. Both Access and Word contain
the Application class, so if you want to use the Word Application class,
you'll need to disambiguate it by using the full name of the class, which
includes the library name. The standard programming convention in VB is to
declare a variable of the correct data type and use that variable throughout
the procedure. For example:

Dim wordApp As Word.Application

' . . . Other code.

wordApp.PrintOut FileName:="MEMART", Range:=wdPrintRangeOfPages, _
Item:= wdPrintDocumentContent, Copies:=1, Pages:="2-20", PageType:= _
wdPrintAllPages, ManualDuplexPrint:=False, Collate:=True,
Background:= _
True, PrintToFile:=True, PrintZoomColumn:=0, PrintZoomRow:=0, _
PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0, _
OutputFileName:=PrnDocName, _
Append:=False
wordApp.ActiveDocument.Close (wdDoNotSaveChanges)

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
http://www.Access.QBuilt.com/html/expert_contributors2.html for contact info.


Dudley said:
I am trying to create a command button in an Access form to open a Word
document and print it to file. I have copied the Application.PrintOut command
which works in a Word macro, but Access VBA gives error message 'Compile
error. Method or data member not found.' I have checked that all the
references in Word VBA are also installed in Access VBA. Any suggestions? The
code is:

Private Sub Command157_Click()
Dim PrnDocName As String
PrnDocName = "c:\my documents 2\word
documents\electronicincorporation\memarts\DM" & _
Me![EnvelopeNumber] & ".MEM"

MergeNoPrompts "MEMART"

If Len(Dir$(PrnDocName)) > 0 Then
SetAttr PrnDocName, vbNormal
Kill PrnDocName
End If

ActivePrinter = "HP LaserJet IIP"
Application.PrintOut FileName:="MEMART", Range:=wdPrintRangeOfPages,
Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="2-20", PageType:= _
wdPrintAllPages, ManualDuplexPrint:=False, Collate:=True,
Background:= _
True, PrintToFile:=True, PrintZoomColumn:=0, PrintZoomRow:=0, _
PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0,
OutputFileName:=PrnDocName, _
Append:=False
ActiveDocument.Close (wdDoNotSaveChanges)

End Sub

Thanks
Dudley
 
6

'69 Camaro

You're welcome. Glad you got it working.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
http://www.Access.QBuilt.com/html/expert_contributors2.html for contact
info.


Dudley said:
Hi Oliver and Gunny,

Many thanks to both of you. I combined the advice of both as

Dim objWord As Word.Application
Set objWord = CreateObject("Word.Application")

and it worked fine.

Regards
Dudley

'69 Camaro said:
Hi, Dudley.
I have checked that all the
references in Word VBA are also installed in Access VBA. Any
suggestions?

The references that are higher in the list of references in the
References
window take precedence over those references which are lower when both
references contain classes of the same name. Both Access and Word
contain
the Application class, so if you want to use the Word Application class,
you'll need to disambiguate it by using the full name of the class, which
includes the library name. The standard programming convention in VB is
to
declare a variable of the correct data type and use that variable
throughout
the procedure. For example:

Dim wordApp As Word.Application

' . . . Other code.

wordApp.PrintOut FileName:="MEMART", Range:=wdPrintRangeOfPages, _
Item:= wdPrintDocumentContent, Copies:=1, Pages:="2-20",
PageType:= _
wdPrintAllPages, ManualDuplexPrint:=False, Collate:=True,
Background:= _
True, PrintToFile:=True, PrintZoomColumn:=0, PrintZoomRow:=0, _
PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0, _
OutputFileName:=PrnDocName, _
Append:=False
wordApp.ActiveDocument.Close (wdDoNotSaveChanges)

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
http://www.Access.QBuilt.com/html/expert_contributors2.html for contact
info.


Dudley said:
I am trying to create a command button in an Access form to open a Word
document and print it to file. I have copied the Application.PrintOut
command
which works in a Word macro, but Access VBA gives error message
'Compile
error. Method or data member not found.' I have checked that all the
references in Word VBA are also installed in Access VBA. Any
suggestions? The
code is:

Private Sub Command157_Click()
Dim PrnDocName As String
PrnDocName = "c:\my documents 2\word
documents\electronicincorporation\memarts\DM" & _
Me![EnvelopeNumber] & ".MEM"

MergeNoPrompts "MEMART"

If Len(Dir$(PrnDocName)) > 0 Then
SetAttr PrnDocName, vbNormal
Kill PrnDocName
End If

ActivePrinter = "HP LaserJet IIP"
Application.PrintOut FileName:="MEMART",
Range:=wdPrintRangeOfPages,
Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="2-20", PageType:= _
wdPrintAllPages, ManualDuplexPrint:=False, Collate:=True,
Background:= _
True, PrintToFile:=True, PrintZoomColumn:=0, PrintZoomRow:=0, _
PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0,
OutputFileName:=PrnDocName, _
Append:=False
ActiveDocument.Close (wdDoNotSaveChanges)

End Sub

Thanks
Dudley
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top