Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Print Macro : Object not set

Hello everyone,

Thanks in advance for reading my post. I'm very new to Excel VBA
programming and having some problem with a code snippet that prints
selected sheets.

I have a form with two list boxes. The left one shows the all the
worksheets and the user can add any or all of these sheets to the list
box on the right. Then I pass the list from the second list box to a
sub procedure taht prints. It prints the selected worksheets, but
everytime I get an error saying " Object variable or with block
variable not set". Here's my code...


Private Sub cmdPrint_Click()


'create an instanece of excel to initiate printing
Dim objXLSApp As Object
Dim wbkXLSBook As Workbook
Set objXLSApp = GetObject(, "Excel.Application")
Set wbkXLSBook = objXLSApp.ThisWorkbook

With wbkXLSBook

For i = 0 To lstPrintQue.ListCount - 1
wbkXLSBook.Sheets(lstPrintQue.List(i)).Activate
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
Next i

End With

'release object
Set objXLSApp = Nothing

Unload Me


End Sub



I created an Excel appliocation object and then added the selected
worksheets to that application and then printed them out. This looks a
round about way of doing something as simple as printing a few sheets.
Is there a better way? If somebody code show me a code sample I'd
greatly appreciate it. Or, maybe some of you can suggest a few changes
to my existing code?

Again, I really appreciate the help.

Thanks!

A. Rahman
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Print Macro : Object not set

You don't need to use createobject or getobject. Application already refers
to the instance of excel with your workbook and thisworkbook is already
defined.

Private Sub cmdPrint_Click()


'create an instanece of excel to initiate printing

With ThisWorkbook

For i = 0 To lstPrintQue.ListCount - 1
if i = 0 then
.Sheets(lstPrintQue.list(i)).Select
else
.Sheets(ListPrintQue.List(i)).Select False
end if
Next i
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
.Worksheets(1).Select
End With
Unload Me
End Sub

--
Regards,
Tom Ogilvy


"Ugleeduck" wrote in message
om...
Hello everyone,

Thanks in advance for reading my post. I'm very new to Excel VBA
programming and having some problem with a code snippet that prints
selected sheets.

I have a form with two list boxes. The left one shows the all the
worksheets and the user can add any or all of these sheets to the list
box on the right. Then I pass the list from the second list box to a
sub procedure taht prints. It prints the selected worksheets, but
everytime I get an error saying " Object variable or with block
variable not set". Here's my code...


Private Sub cmdPrint_Click()


'create an instanece of excel to initiate printing
Dim objXLSApp As Object
Dim wbkXLSBook As Workbook
Set objXLSApp = GetObject(, "Excel.Application")
Set wbkXLSBook = objXLSApp.ThisWorkbook

With wbkXLSBook

For i = 0 To lstPrintQue.ListCount - 1
wbkXLSBook.Sheets(lstPrintQue.List(i)).Activate
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
Next i

End With

'release object
Set objXLSApp = Nothing

Unload Me


End Sub



I created an Excel appliocation object and then added the selected
worksheets to that application and then printed them out. This looks a
round about way of doing something as simple as printing a few sheets.
Is there a better way? If somebody code show me a code sample I'd
greatly appreciate it. Or, maybe some of you can suggest a few changes
to my existing code?

Again, I really appreciate the help.

Thanks!

A. Rahman



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Print Macro : Object not set

It works fine now. Thank you so much for all your help and patience!

"Tom Ogilvy" wrote in message ...
there was a typo where lstprintque was spelled listprintque in the second
leg of the if statement.

I ran this in excel and it caused no error. The code is in the Userform
module (so ME refers to the userform).

Private Sub cmdPrint_Click()


'create an instanece of excel to initiate printing
Unload Me '<== move here to prevent interference
' with printpreview which I used to save paper
ThisWorkbook.Activate
With ThisWorkbook

For i = 0 To lstPrintQue.ListCount - 1
If i = 0 Then
.Sheets(lstPrintQue.List(i)).Select
Else
.Sheets(lstPrintQue.List(i)).Select False
End If
Next i
ActiveWindow.SelectedSheets.PrintPreview
.Worksheets(1).Select
End With

End Sub


I don't think it is anything in this code that is causing your problem.

Regards,
Tom Ogilvy

Ugleeduck wrote in message
m...
Thanks for the help. I tried the code. The result is same. It prints ,
but also gives me the error "Object variable or with block variable
not set". Just like before :(



"Tom Ogilvy" wrote in message

...
You don't need to use createobject or getobject. Application already

refers
to the instance of excel with your workbook and thisworkbook is already
defined.

Private Sub cmdPrint_Click()


'create an instanece of excel to initiate printing

With ThisWorkbook

For i = 0 To lstPrintQue.ListCount - 1
if i = 0 then
.Sheets(lstPrintQue.list(i)).Select
else
.Sheets(ListPrintQue.List(i)).Select False
end if
Next i
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
.Worksheets(1).Select
End With
Unload Me
End Sub

--
Regards,
Tom Ogilvy


"Ugleeduck" wrote in message
om...
Hello everyone,

Thanks in advance for reading my post. I'm very new to Excel VBA
programming and having some problem with a code snippet that prints
selected sheets.

I have a form with two list boxes. The left one shows the all the
worksheets and the user can add any or all of these sheets to the list
box on the right. Then I pass the list from the second list box to a
sub procedure taht prints. It prints the selected worksheets, but
everytime I get an error saying " Object variable or with block
variable not set". Here's my code...


Private Sub cmdPrint_Click()


'create an instanece of excel to initiate printing
Dim objXLSApp As Object
Dim wbkXLSBook As Workbook
Set objXLSApp = GetObject(, "Excel.Application")
Set wbkXLSBook = objXLSApp.ThisWorkbook

With wbkXLSBook

For i = 0 To lstPrintQue.ListCount - 1
wbkXLSBook.Sheets(lstPrintQue.List(i)).Activate
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
Next i

End With

'release object
Set objXLSApp = Nothing

Unload Me


End Sub



I created an Excel appliocation object and then added the selected
worksheets to that application and then printed them out. This looks a
round about way of doing something as simple as printing a few sheets.
Is there a better way? If somebody code show me a code sample I'd
greatly appreciate it. Or, maybe some of you can suggest a few changes
to my existing code?

Again, I really appreciate the help.

Thanks!

A. Rahman

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I print an object in Excel 2007 MAF Excel Discussion (Misc queries) 2 April 4th 09 03:40 AM
Line appears - not border, not object, does not print - what is it Licardi Excel Discussion (Misc queries) 2 January 15th 08 06:51 PM
Print Word Object in Excel Mac Excel Discussion (Misc queries) 7 March 2nd 07 05:12 PM
Why does a pasted object in Excel sometimes print upside down? Mel Excel Discussion (Misc queries) 2 December 2nd 05 02:07 PM
Print more then one page in excel from a word object Alex Martinez Excel Worksheet Functions 0 October 14th 05 06:22 AM


All times are GMT +1. The time now is 04:49 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"