Executing Excel's Addshape Method thru VB

Posted on 2000-02-11
Medium Priority
Last Modified: 2012-06-27
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.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
  xlsWorkbooks.Close False
End Sub
Question by:baileys

Accepted Solution

MTroutwine earned 600 total points
ID: 2512633
You need to add a reference in your project to Microsoft Office 8.0 Object Library.  The shape object constants  (e.g. msoShapeRectangle) are found in that library not the Excel library.  

That should fix you up!


Author Comment

ID: 2512838
Thanks, that worked!!

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

587 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