?
Solved

Excel VBA error opening workbook

Posted on 2011-02-27
7
Medium Priority
?
466 Views
Last Modified: 2012-05-11
Hi. I am getting the following error on the line marked with ** in the code below.

"excel vba object variable or with block variable not set"

The strange thing is that it opens the workbook


Sub Main()

    On Error GoTo EH

    MYDOC_DIR = Environ("userprofile") & "\Desktop"
    Dim oWb As Workbook
    oWb = Application.Workbooks.Open(MYDOC_DIR & "\" & "FILE FROM ITS.xlsm") '**
    Windows("FILE FROM ITS.xlsm").Activate
    oWb.Sheets(1).Cells.Copy
    ThisWorkbook.Sheets(1).Cells.Paste
    
    Exit Sub
EH:
    MsgBox Err.Description

End Sub

Open in new window

0
Comment
Question by:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 2000 total points
ID: 34992818
Try:

Set oWb = Application.Workbooks.Open(MYDOC_DIR & "\" & "FILE FROM ITS.xlsm") '**
instead of
oWb = Application.Workbooks.Open(MYDOC_DIR & "\" & "FILE FROM ITS.xlsm") '**

Chris
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34992826
Do you need to declare MYDOC_DIR as a local variable?

Dim MYDOC_DIR As String

Open in new window

0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34992838

chris_bottomley:  Good thinking, but "set" is not needed in VB assignment statements.

http://msdn.microsoft.com/en-us/library/z2wkh0tk(v=vs.80).aspx

Quite a mystery.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

by:Murray Brown
ID: 34992860
Hi. The "Set" worked - the declaration of the variable made no difference.
Thanks
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34992892
chris_bottomley, any idea why the Set is needed on that line but not on the previous line that sets the value of MYDOC_DIR?  Or why it is needed at all?

Great you solved the problem but it is still a question why it worked.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34992906
Simply put objects need to be set, variables do not ... therefore a reference to an application, workbook or worksheet are objects and therefore are always set.

Chris
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34993036
Thanks!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
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…

741 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