+ Reply to Thread
Results 1 to 5 of 5

compile error User-defined type not defined Set objXL = New Excel.Application

Hybrid View

  1. #1
    Registered User
    Join Date
    08-14-2013
    Location
    Kentucky, United States
    MS-Off Ver
    03,07,10
    Posts
    61

    compile error User-defined type not defined Set objXL = New Excel.Application

    Hi All,

    I am fairly new to Access VBA. In Access I am trying to set a command button to run a Module. I am currently using a Macro to accomplish this (openvisualbasicsmodual >Modual Name: "Module1" > Procedure Name: "sCopyRSToNamedRange")

    This Macro is only opening the Module. I need a command to run the module

    I need the following code to export the table data to an existing, saved excel file. It would have to start inserting in Column C Row 3 and insert the table rows down from there.

    The Module is giving me a Compile error User-defined type not defined on VBA line Set objXL = New Excel.Application in the following Code:

    Sub sCopyRSToNamedRange()
    'Copy records to a named range
    'on an existing worksheet on a
    'workbook
    '
    'Dim objXL As Excel.Application
    Dim objXL As Object
    Set objXL = CreateObject("Excel.Application")
    'Dim objWkb As Excel.Workbook
    Dim objWkb As Object
    Set objWkb = CreateObject("Excel.Workbook")
    'Dim objSht As Excel.Worksheet
    Dim objSht As Object
    Set objSht = CreateObject("Excel.Worksheet")
    Dim db As Database
    Dim rs As Recordset
    Const conMAX_ROWS = 20000
    Const conSHT_NAME = "MySheet"
    Const conWKB_NAME = "c:\temp\Test1.xls"
    Const conRANGE = "RangeForRS"
    
      Set db = CurrentDb
      Set objXL = New Excel.Application
      Set rs = db.OpenRecordset(tblWireRack, dbWebADIItemManagment)
      With objXL
        .Visible = True
        Set objWkb = .Workbooks.Open(conWKB_NAME)
        On Error Resume Next
        Set objSht = objWkb.Worksheets(conSHT_NAME)
        If Not Err.Number = 0 Then
          Set objSht = objWkb.Worksheets.Add
          objSht.Name = conSHT_NAME
        End If
        Err.Clear
        On Error GoTo 0
        objSht.Range(conRANGE).CopyFromRecordset rs
      End With
      Set objSht = Nothing
      Set objWkb = Nothing
      Set objXL = Nothing
      Set rs = Nothing
      Set db = Nothing
    End Sub
    Thank you for Any help you can give on this.

  2. #2
    Forum Contributor mongoose36's Avatar
    Join Date
    06-10-2013
    Location
    Lincoln
    MS-Off Ver
    Excel 2013
    Posts
    389

    Re: compile error User-defined type not defined Set objXL = New Excel.Application

    Delete that line you are already doing this above using late binding method...
    Dim objXL As Object
    Set objXL = CreateObject("Excel.Application")
    . Look here for more info on late vs. early binding.
    If your questions has been answered to your satisfaction please don't forget to do the following:

    Add Reputation ... and ... Mark Thread as Solved

    Thanks,

    Ma 10:8b Freely you have received; freely give.

  3. #3
    Registered User
    Join Date
    08-14-2013
    Location
    Kentucky, United States
    MS-Off Ver
    03,07,10
    Posts
    61

    Re: compile error User-defined type not defined Set objXL = New Excel.Application

    Thank You, that did work however now I receive Run time error '429': ActiveX component can't create object:
    Option Compare Database
    
    Sub sCopyRSToNamedRange()
    'Copy records to a named range
    'on an existing worksheet on a
    'workbook
    '
    'Dim objXL As Excel.Application
    Dim objXL As Object
    Set objXL = CreateObject("Excel.Application")
    'Dim objWkb As Excel.Workbook
    Dim objWkb As Object
    Set objWkb = CreateObject("Excel.Workbook")
    'Dim objSht As Excel.Worksheet
    Dim objSht As Object
    Set objSht = CreateObject("Excel.Worksheet")
    Dim db As Database
    Dim rs As Recordset
    Const conMAX_ROWS = 20000
    Const conSHT_NAME = "MySheet"
    Const conWKB_NAME = "c:\temp\Test1.xls"
    Const conRANGE = "RangeForRS"
    
      Set db = CurrentDb
      'Set objXL = New Excel.Application
      Set rs = db.OpenRecordset(tblWireRack, dbWebADIItemManagment)
      With objXL
        .Visible = True
        Set objWkb = .Workbooks.Open(conWKB_NAME)
        On Error Resume Next
        Set objSht = objWkb.Worksheets(conSHT_NAME)
        If Not Err.Number = 0 Then
          Set objSht = objWkb.Worksheets.Add
          objSht.Name = conSHT_NAME
        End If
        Err.Clear
        On Error GoTo 0
        objSht.Range(conRANGE).CopyFromRecordset rs
      End With
      Set objSht = Nothing
      Set objWkb = Nothing
      Set objXL = Nothing
      Set rs = Nothing
      Set db = Nothing
    End Sub
    The end goal of this module will be to get the excel workbook to open from a specified location and insert the Access table data into the excel sheet1 in text format starting at Column C Row 3.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: compile error User-defined type not defined Set objXL = New Excel.Application

    If posting code please use code tags, see here.

  5. #5
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2407 Win 11 Home 64 Bit
    Posts
    24,084

    Re: compile error User-defined type not defined Set objXL = New Excel.Application

    Your post does not comply with Rule 8 of our Forum RULES. Do not crosspost your question on multiple forums without including links here to the other threads on other forums.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Expect cross-posted questions without a link to be closed and a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Compile error user defined type not defined
    By elloco240866_Alsdorf in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-31-2015, 02:26 AM
  2. Compile error: User- defined type not defined
    By rolex in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-14-2015, 05:08 PM
  3. Compile error: user defined type not defined
    By duhigs in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-23-2014, 07:51 PM
  4. [SOLVED] Compile error: User-defined type not defined
    By XmisterIS in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-21-2014, 09:46 AM
  5. Error message: "Compile error. User-defined type not defined"
    By freckles81 in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 06-13-2012, 09:37 AM
  6. compile error - user defined type not defined
    By TMP123 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-23-2010, 01:42 PM
  7. VB Compile Error - User-defined type not defined
    By dbwiz in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-08-2010, 04:31 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1