Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default 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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 486
Default 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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 225
Default 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


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default 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

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 486
Default 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




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default 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

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 225
Default 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


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
Why "object variable or with block variable not set" error? SSjmg2477 Excel Programming 2 February 14th 06 11:02 PM
"Object Variable or With Block Variable Not Set" error help request Ken Loomis Excel Programming 8 June 27th 05 10:13 PM
Run-time error '91': "Object variable or With block variable not set Mike[_92_] Excel Programming 2 December 30th 04 10:59 AM
"Run-time error 91: Object variable or With block not set" Snedker Excel Programming 0 January 10th 04 09:37 AM
Ogilvy Help :) - "Object variable or With block variable not set" Mike Taylor Excel Programming 1 December 16th 03 07:21 AM


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

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

About Us

"It's about Microsoft Excel"