Solved

Excel object stays open

Posted on 2016-09-08
19
70 Views
Last Modified: 2016-09-08
When you run the below code it will crash the 2nd time you click on the command1 button.  With the error "run-time" error 91.  Object variable or with block variable not set"


Private Sub Command1_Click()
    Dim xls     As Excel.Application
     Dim wkb     As Excel.Workbook
     Dim wks     As Excel.Worksheet
     
     Set xls = New Excel.Application
     Set wkb = xls.Workbooks.Open("c:\testii\workbook1.xlsx")
     Set wks = wkb.Worksheets(1)
     
     wks.Name = "My New Name"
     
           ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=True
     wkb.Close True
     
     Set wks = Nothing
     Set wkb = Nothing
     
     xls.Quit
     
     Set xls = Nothing
End Sub
0
Comment
Question by:rjef
  • 9
  • 3
  • 3
  • +3
19 Comments
 
LVL 35

Expert Comment

by:Kimputer
ID: 41789705
There's nothing wrong with the code. What happens if you step through the code twice (F8)? Which line gives the error?
0
 

Author Comment

by:rjef
ID: 41789726
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
         IgnorePrintAreas:=True
0
 
LVL 35

Expert Comment

by:Kimputer
ID: 41789762
Try again with:

           wks.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=True

Open in new window

0
 

Author Comment

by:rjef
ID: 41789770
nothing prints
0
 
LVL 35

Accepted Solution

by:
Kimputer earned 500 total points
ID: 41789786
change again

       wkb.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=True

Open in new window

0
 

Author Comment

by:rjef
ID: 41789794
ok so that worked.  so what is the issue?
0
 

Author Comment

by:rjef
ID: 41789795
I take that back.  nothing printed.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 41789812
I believe line 16 was misplaced.
Private Sub Command1_Click()
    Dim xls     As Excel.Application
     Dim wkb     As Excel.Workbook
     Dim wks     As Excel.Worksheet
     
     Set xls = New Excel.Application
     Set wkb = xls.Workbooks.Open("c:\solutions\Prashansa.xlsm")
     Set wks = wkb.Worksheets(1)
     
     wks.Name = "My New Name"
     
           ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=True
     wkb.Close True
     
     xls.Quit
     
     Set wks = Nothing
     Set wkb = Nothing
     
     Set xls = Nothing
End Sub

Open in new window

0
 
LVL 33

Expert Comment

by:Norie
ID: 41789824
Here you should have a reference to the instance of Excel you created earlier or the sheet(s) you want to print.
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=True

Open in new window

What sheet(s) do you want to print?

PS Where are you running this code?
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:rjef
ID: 41789892
running in vb6
sheet1?
0
 

Author Comment

by:rjef
ID: 41789898
The sheet I want to print is "My New Name"
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41789930
Have you tried?
wks.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=True

Open in new window

0
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 41790055
You might need to activate the sheet before printing it.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 41790061
Did you try the change I posted in post ID: 41789812? I ask because that solved the problem for me.
0
 
LVL 33

Expert Comment

by:Norie
ID: 41790093
wks.PrintOut... should work.

Are you sure nothing is being sent to the printer?

How are you running the code?
0
 

Author Comment

by:rjef
ID: 41790167
this finally worked.  

not sure who came up with it?

you all tell me


Private Sub Command1_Click()

    Dim xls     As Excel.Application
     Dim wkb     As Excel.Workbook
     Dim wks     As Excel.Worksheet
     
     Set xls = New Excel.Application
     Set wkb = xls.Workbooks.Open("c:\testii\workbook1.xlsx")
     Set wks = wkb.Worksheets(1)
     
     wks.Name = "My New Name"
     
wks.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=True
       
       
     wkb.Close True
     
     xls.Quit
     
     Set wks = Nothing
     Set wkb = Nothing
     
     Set xls = Nothing
End Sub
0
 
LVL 33

Expert Comment

by:Norie
ID: 41790179
Well it was Kimputer that originally suggested using wks.PrintOut...
0
 

Author Comment

by:rjef
ID: 41790197
the reason it wasn't printing at one point was because it was empty.!!
0
 

Author Closing Comment

by:rjef
ID: 41790198
thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

919 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

17 Experts available now in Live!

Get 1:1 Help Now