ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Getting "Object Variable or With Block Variable not set" error (https://www.excelbanter.com/excel-programming/354173-getting-object-variable-block-variable-not-set-error.html)

Suraj[_2_]

Getting "Object Variable or With Block Variable not set" error
 
Hi,

I am getting the error "Object Variable or With Block Variable not set"
when I try to execute following VBA code.

Dim a As Excel.Application
Dim wb As Workbook

For Each wb In a.Workbooks
wb.Save
Next wb

Please let me know what is the problem

Thanks,
suraj


Jim Thomlinson[_5_]

Getting "Object Variable or With Block Variable not set" error
 
Why are you wanting an new instance of the excel application?

Dim a As Excel.Application

You never create the instance. So it does not know what a is. You can
probably just remove the a...

Dim wbk As Workbook

For Each wbk In Workbooks
MsgBox wbk.Name
Next wbk
--
HTH...

Jim Thomlinson


"Suraj" wrote:

Hi,

I am getting the error "Object Variable or With Block Variable not set"
when I try to execute following VBA code.

Dim a As Excel.Application
Dim wb As Workbook

For Each wb In a.Workbooks
wb.Save
Next wb

Please let me know what is the problem

Thanks,
suraj



Andrew Taylor

Getting "Object Variable or With Block Variable not set" error
 
The line
Dim a As Excel.Application
only declares a as being of that type, it doesn't create an Excel
object
(so a is Nothing, which is the reason for the error)

If you're using VBA within Excel the Application object already exists,
so you can just say

For Each wb In Application.Workbooks
' or even just For Each wb In Workbooks
wb.Save
Next wb

Hope this helps
Andrew

Suraj wrote:
Hi,

I am getting the error "Object Variable or With Block Variable not set"
when I try to execute following VBA code.

Dim a As Excel.Application
Dim wb As Workbook

For Each wb In a.Workbooks
wb.Save
Next wb

Please let me know what is the problem

Thanks,
suraj



Suraj[_2_]

Getting "Object Variable or With Block Variable not set" error
 
Thanks Jim and Andrew.
Jim,
I tried in the way you suggested, but the statements inside the For
Loop are not being executed. This time I didn't get any error. Pls let
me know what to do. I am using a tool called "TestPartner" to test the
applications. I am using the VBA in that tool.

Andrew,
I am using VBA but not within ExcelApplication. I am using a tool
called "TestPartner" to test the applications. I am using the VBA in
that tool.

Please suggest me.

Thanks,
Suraj


Jim Thomlinson[_5_]

Getting "Object Variable or With Block Variable not set" error
 
I have to admit that I am unfamilliar with "TestPartner", but what you are
trying to do is not going to work. What you need to do is to set "a" to the
current instance of Excel. That being said it is possible to have multiple
instances of Excel running at the same time, so you are going to have
difficulty attaching to the instace that you want. You can create a new
instance of Excel something like this

Dim xlApp As Object

Set xlApp = CreateObject("Excel.Application")

With xlApp
.Visible = True
.Workbooks.Add
.Workbooks.Open "C:\MyBook.xls"
End With
--
HTH...

Jim Thomlinson


"Suraj" wrote:

Thanks Jim and Andrew.
Jim,
I tried in the way you suggested, but the statements inside the For
Loop are not being executed. This time I didn't get any error. Pls let
me know what to do. I am using a tool called "TestPartner" to test the
applications. I am using the VBA in that tool.

Andrew,
I am using VBA but not within ExcelApplication. I am using a tool
called "TestPartner" to test the applications. I am using the VBA in
that tool.

Please suggest me.

Thanks,
Suraj



Suraj[_2_]

Getting "Object Variable or With Block Variable not set" error
 
Thanks Jim. I got the logic. I have one more query.

Using the VBA I want to save an Excel file which is already opened. I
tried to do this by ActiveWorkBook.SaveAs(" ").
But not working.
Please let me know the correct way of doing that or correct way of
using the above command.

Thanks,
Suraj


Andrew Taylor

Getting "Object Variable or With Block Variable not set" error
 
Using Jim's code, you would say
xlApp.ActiveWorkBook.SaveAs(" ")
or
.ActiveWorkBook.SaveAs(" ")
if it's inside the With xlApp block.

(I presume there's actually a file name in the " ")

You might also want to put
.Application.DisplayAlerts = False
and
.Application.DisplayAlerts = True
around the SaveAs line to prevent a prompt if the file already exists
(or you could handle this possibility another way, e.g. by checking
whether it exists before trying to save)

Andrew

Suraj wrote:
Thanks Jim. I got the logic. I have one more query.

Using the VBA I want to save an Excel file which is already opened. I
tried to do this by ActiveWorkBook.SaveAs(" ").
But not working.
Please let me know the correct way of doing that or correct way of
using the above command.

Thanks,
Suraj



Suraj[_2_]

Getting "Object Variable or With Block Variable not set" error
 
Thanks Andrew.
But, Still I am getting the error. I don't know why?

-Suraj


Suraj[_2_]

Getting "Object Variable or With Block Variable not set" error
 
Thanks Andrew,

But Still I am getting he same error. When I debug the code it is
showing that the error lies in the following code.
xlApp.ActiveWorkBook.SaveAs("c:\ ")

-Suraj


Andrew Taylor

Getting "Object Variable or With Block Variable not set" error
 
You need to give the name of the file to Save As - e.g.
xlApp.ActiveWorkBook.SaveAs("c:\myWorkbook.xls")



Suraj wrote:
Thanks Andrew,

But Still I am getting he same error. When I debug the code it is
showing that the error lies in the following code.
xlApp.ActiveWorkBook.SaveAs("c:\ ")

-Suraj




All times are GMT +1. The time now is 01:22 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com