Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RUN A MACRO IN AN EXCEL OBJECT

Status
Not open for further replies.

IAC

Technical User
Oct 31, 2002
2
MY
I need to run an XL macro within an XL object that has been create in a third party application. Any macro code will work in native XL, but will not run when inside the object...

For example if you run
Range("A3:B8").Select
Selection.Font.Bold = True
inside XL it works as expected. However, if you try and run the same code inside an XL Object you get the following error
RTE 1004 Method "Range" of object "_gloabl" failed.
 
You need to set an explicit reference to the 'Range' object so you can access its methods and attributes:
Code:
Function OpenExcel(ByVal strFile As String) As Boolean
On Error GoTo ErrHandler

  Dim xl As Excel.Application
  Dim Wb As Excel.Workbook
  Dim sht As Excel.Worksheet
  Dim rng As Excel.Range
  
  Set xl = New Excel.Application
  Set Wb = xl.Workbooks.Open(strFile)
  Set sht = Wb.Sheets("Sheet1")
  Set rng = sht.Range("C3")

  rng.Font.Bold = True
  
  xl.Visible = True 
    
ExitHere:
  Exit Function
ErrHandler:
  MsgBox "Error: " & Err & " - " & Err.Description
  Resume ExitHere
End Function
VBSlammer
redinvader3walking.gif

Unemployed in Houston, Texas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top