Executing Excel's Addshape Method thru VB

I have been handed a task to program an Excel spreadsheet using VB/VBA.  I have no exprience programming Excel and little exprience using Excel.  I have ordered two Excel books, but in the mean time I've been using the Excel help and VB Object browser to duplicate the sample spreadsheet using VB.  I am having problems adding shapes to my worksheet.  I am getting the following error when I use the Addshape Method:

Run-time error '1004': The specified value is out of range.

Here is my VB module code. What is wrong?

Private appExcel As Object
Private xlsWorkbooks As Object
Private myWorkbook As Object
Private myWorksheet As Object

Public Sub Main()
  Dim fName As String
 
  Set appExcel = CreateObject("Excel.Application")
  appExcel.Visible = False
  fName = appExcel.GetOpenFilename
 
  Set xlsWorkbooks = appExcel.Workbooks.Open(fName)
  Set myWorkbook = appExcel.Workbooks(1)
  Set myWorksheet = appExcel.Workbooks(1).Worksheets(1)
 
  myWorksheet.Range("A1").Select
  myWorksheet.Shapes.AddShape msoShapeRectangle, Selection.Left, Selection.Top, 10, 10
  'With myWorksheet.Shapes.AddShape(msoShapeRectangle, 50, 50, 100, 200)
   ' .Fill.ForeColor.RGB = RGB(100, 0, 0)
  'End With
  appExcel.Visible = True
  myWorkbook.Show
 
  xlsWorkbooks.Close False
  appExcel.Quit
End Sub
baileysAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

billinbCommented:
What version of Excel? if I commented out the myworkbook.show line above, it worked fine, and I'm running Excel 97.

Bill
0
baileysAuthor Commented:
I'm running Excel 97 also.

Sheila
0
billinbCommented:
I wonder how I can reproduce your error.  Is the code placed in a separate module or in one of the worksheets/thisworkbook?  My test ran in a worksheet...You might try bringing the variable initialization into the sub, it might be easier at first.  Try a unique name also.

For example,

Sub MainFeb10()
Dim appExcel as Object
Dim xlsWorkbooks As Object
Dim myWorkbook As Object
Dim myWorksheet As Object
'...etc

End sub



0
tureCommented:
Sheila,

A few things:

1. Not bad for a never-programmed-Excel-before-person!
2. It's better to explicitly declare the type of object when Dim-ing variables.
3. xlsWorkbooks was not necessary - it actually referred to the same workbook as myWorkbook
4. The real problem here was probably that you must use appExcel.Selection instead of just Selection.
5. It's good practice to set object variables to Nothing at the end of the procedure.
6. Make sure that you have set up references in Visual Basic to 'Microsoft Excel 8.0 Object Library' AND 'Microsodt Office 8.0 Object Library'
7. I have added a Stop statement so that you can check that the rectangle is actually drawn in cell A1. You'll have to switch to Excel to see it.

Private appExcel As Excel.Application
Private myWorkbook As Excel.Workbook
Private myWorksheet As Excel.Worksheet

Public Sub Main()
  Dim fName As String
   
  Set appExcel = CreateObject("Excel.Application")
  appExcel.Visible = False
  fName = appExcel.GetOpenFilename
  Set myWorkbook = appExcel.Workbooks.Open(fName)
  Set myWorksheet = myWorkbook.Worksheets(1)
   
  myWorksheet.Range("A1").Select
  myWorksheet.Shapes.AddShape msoShapeRectangle, appExcel.Selection.Left, appExcel.Selection.Top, 10, 10
  'With myWorksheet.Shapes.AddShape(msoShapeRectangle, 50, 50, 100, 200)
   ' .Fill.ForeColor.RGB = RGB(100, 0, 0)
  'End With
  appExcel.Visible = True
   
  Stop
 
  myWorkbook.Close False
  appExcel.Quit
 
  Set myWorksheet = Nothing
  Set myWorkbook = Nothing
  Set appExcel = Nothing
End Sub

Ture Magnusson
Karlstad, Sweden
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tureCommented:
Just a friendly reminder. It's so easy to forget about old questions...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.