using the Excel object and ActivePrinter

Posted on 2003-10-23
Last Modified: 2008-02-01

I am getting an error when trying to set the ActivePrinter using Excel (9.0 object library).  I'm on W2000 using VB6.

Here is my code in a test project:

Private Sub Timer1_Timer()
    Timer1.Enabled = False
End Sub

Function fnPrintExcelTest()
    On Error GoTo Error_Handler:
    Dim xl As Excel.Application
    Dim wb As Excel.Workbook
    Set xl = New Excel.Application
    Set wb = xl.Workbooks.Open("c:\test.xls")
    xl.ActivePrinter = "Print-2-Image"   'Name of the printer i wish to use  ' ERROR HERE!!!!!!!!!!!!!!!!!!
    wb.Close SaveChanges:=False
    Set xl = Nothing

    If Err.Number <> 0 Then
        MsgBox CStr(Err.Number) & ": " & Err.Description
    End If

End Function

The error that I get is:  "1004: Application-defined or object-defined error"

A few things to note...

-- I am using Print-2-Image as the printer, so the Excel file will be saved as a TIF.
-- I can do the same exact thing using the Word 9.0 object library and get no problems.
-- When I debug this code, on the line that errors, when I hover on the ActivePrinter, it says "unknown printer (check your Control Panel)"  pretty odd message there
-- I can manually open my test file in Excel and print it.  It does find Print-2-Image and creates the expected tif...
although, if I open the Print... diaolg window, there is no default printer, but i can choose Print-2-Image from a list

Anyone have a clue on this?

Question by:corduroy9

Accepted Solution

TwistD_PissR earned 100 total points
ID: 9607988
Here is what you can do:
1.) set "Print-2-Image" printer as the Default Printer (only temporary)
2.) Try this (and be sure to write down everything in the MSGBOX
      Function fnPrintExcelTest()
                On Error GoTo Error_Handler:
                Dim xl As Excel.Application
                Dim wb As Excel.Workbook
                Set xl = New Excel.Application
                Set wb = xl.Workbooks.Open("c:\test.xls")
                msgbox xl.ActivePrinter
            wb.Close SaveChanges:=False
                Set xl = Nothing

                If Err.Number <> 0 Then
                    MsgBox CStr(Err.Number) & ": " & Err.Description
                End If
      End Function
3.) Set the printer value equal to what appeared in the msgbox
      i.e.  xl.ActivePrinter = "Print-2-Image on LPT1"  ' Local Port
            xl.ActivePrinter = "Print-2-Image on Ne01"  ' Network
4.) Run Code and test
5.) Reset default printer to whatever it was before
6.) Done

  In a WIN32 system you have to define where the device is located (local or network) when using
excel's activeprinter funtion


Author Comment

ID: 9613472

I'm accepting your answer, but I think I had other issues that were screwing up my program.  I was trying to print from Word, PowerPoint, and Excel object in the same project and they seemed to conflict with each other, especially with the Printer stuff.

So what I did is kinda hokey...I'm doing a SaveAs on the Excel file to make it a txt file, then opening it in Word to print, Word which seems to be the most stable.  Doing something similar with PowerPoint also.



Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now