Link to home
Create AccountLog in
Windows OS

Windows OS

--

Questions

--

Followers

Top Experts

Avatar of wimzieguy
wimzieguy

Printing Excel sheet from command line
I want to print MS-Excel sheet from command line. I have this script:

PRINT.EXE /D:"\\WRHMAIN\P_IS_WOFFICE" C:\projects\floor.xls

This command line is printing garbage. Any ideas how to make it work.

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of KJHDI12KJHDI12


PRINT.EXE only prints textfile. It treats the excel document as a text document, and the excel document contains alot of binary codes that reult it printing "garbage". Im looking into a solution for the printing.


This VB code will print a XSL document specified as a parameter. ie..
"myapp.exe" c:\test.xls"
will print the excel docuement test.xsl

If you want I can compile you a exe you can use.

Code:

Option Explicit

Public Sub Main()

   ' Variables
   Dim mybook As Excel.Workbook
   Dim xlApp As Excel.Application
   
   ' Error trapping
   On Error GoTo errTrap
   
   ' Quit if no file was specified
   If Command = "" Then Exit Sub
   
   ' Open excel document
   Set xlApp = New Excel.Application
   Set mybook = xlApp.Workbooks.Open(Command, , , , , "")
   
   ' Print
   mybook.PrintOut
   
   ' Close excel
   mybook.Close
   
   Exit Sub
errTrap:
   MsgBox Err.Description, vbInformation, "Excel document printing"
   
End Sub

Avatar of wimzieguywimzieguy

ASKER

Please.... I am too new for visual basic

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


ASKER CERTIFIED SOLUTION
Avatar of KJHDI12KJHDI12

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

I am getting this error when I run this app with my excel sheet:

The instruction at "0x00000000" referenced memory at "0x00000000". The memory could not be "read".
Click blah blah...

Any idea what is going on??


Strange. Anything special about that excel file?

What version of Excel do you use?

Excel 97 SR-2

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.



Ah. This method wont work unless you got Excel 2000 or newer. :/

hmmmm... let me try then.

Wow! It worked with Excel 2000 :-)
Thanks a lot!

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


I am also fairly new to VB programming and needed a helping hand in figuring out how to print an excel document via VB code.  The above URL to the compiled application no longer works (it's 3 years old ..., might be why :) )  So I have re-written it in VB2005, compiled it, and am hosting it here http://www.bxstudio.com/bxdls/ExcelPrinter.exe

See attached code snippet for updated vb2005 code. Again, not a pro at VB...so the code might not be the best way to accomplish this...but it gets the job done and that is all that matters.
Also note, this code is directly derived from the above post by KJHDI12.  I don't want to take credit for his initial work.

Thanks,

Degen

Option Explicit On
 
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Variables
        Dim mybook As Microsoft.Office.Interop.Excel.Workbook
        Dim xlApp As Microsoft.Office.Interop.Excel.Application
 
        ' Error trapping
        On Error GoTo errTrap
 
        ' Quit if no file was specified
        If Command() = "" Then Exit Sub
 
        ' Open excel document
        xlApp = New Microsoft.Office.Interop.Excel.Application
        mybook = xlApp.Workbooks.Open(Command, , , , , "")
 
        ' Print
        mybook.PrintOut()
 
        ' Close excel
        mybook.Close()
        Close()
        Exit Sub
errTrap:
        MsgBox(Err.Description, vbInformation, "Excel document printing")
        Close()
    End Sub
 
End Class

Open in new window

Windows OS

Windows OS

--

Questions

--

Followers

Top Experts

This topic area includes legacy versions of Windows prior to Windows 2000: Windows 3/3.1, Windows 95 and Windows 98, plus any other Windows-related versions including Windows Mobile.