Link to home
Start Free TrialLog in
Avatar of Sahmara
SahmaraFlag for United States of America

asked on

Runtime error '2147467261 (80004003)': Automation error invalid pointer

Hello Experts:

I have an AutoExec function called ChangePrinter that captures what the current default printer is (global) and then sets it to a specific printer.

On the Switchboard form, in the on close event, it calls GoDefaultPrinter to change it back to the original printer that Windows was set to prior to the db being opened.

I am able to capture the correct printer name if I uncomment the message box in the code.  I am able to get the code (autoexec and the on close) to do what I want without errors if I step line by line.  When I open the database, the autoexec code fires off without a hitch.  When I try to close out of the Switchboard, I receive the error, "Runtime Error '2147267261 (80004003'): Automation error invalid pointer."

Any suggestions on how to correct this?

Thank you!

Option Compare Database
Option Explicit
Global GBL_Printer As String
 
Public Function ChangePrinter()
'Set Reference to Windows Script Host Object Model \System32\wshom.ocx.
Dim w As New WshNetwork, p As String
GBL_Printer = Printer.DeviceName 'Gets the default printer
w.SetDefaultPrinter ("ImageMaster")
Set w = Nothing
End Function
 
Function GoDefaultPrinter()
Dim n As New WshNetwork, p As String
p = GBL_Printer
n.SetDefaultPrinter p
Set n = Nothing
'MsgBox "The original printer was " & GBL_Printer & ".", vbInformation, "Testing"
End Function

Open in new window

Avatar of TextReport
TextReport
Flag of United Kingdom of Great Britain and Northern Ireland image

Personally I do not change the Windows Default Printer anymore, Since Access 2002 (I think) you have the Application Printer and you can set this rather than having to change the Windows Default Printer.

Cheers, Andrew
Dim prtDefault As Printer
Set prtDefault = Application.Printer
 
    Application.Printer = "CutePDF Writer"
    
    DoCmd.OpenReport ...
    
    Application.Printer = prtDefault

Open in new window

Avatar of Sahmara

ASKER

So that would mean I would input that code into every report in this db?  There's over 20 reports.
Avatar of Sahmara

ASKER

I think I'm receiving this error because it's being executed on the "On Close" event of the switchboard, but it should be in the constants of the Switchboard code "conCmdExitApplication".  I'll try there.
No you can set the printer but not reset it

     Application.Printer = "CutePDF Writer"

On the Switchboard Open or Load event should be sufficient. You should not need to reset it as it is only set for the current Access instance.

Cheers, Andrew
Sorry missed a bit of the code

     Application.Printer = Application.Printers("CutePDF Writer")

Cheers, Andrew
ASKER CERTIFIED SOLUTION
Avatar of TextReport
TextReport
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sahmara

ASKER

Lol, leave it to me to make it WAY more complicated than necessary.  Your solution is simple and it's working like a charm after testing extensively.  Thanks!