Solved

Excel object stays open

Posted on 2016-09-08
19
79 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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
 

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 30

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

820 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