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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
tureConnect With a Mentor Commented:
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
 
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:
Just a friendly reminder. It's so easy to forget about old questions...
0
All Courses

From novice to tech pro — start learning today.