Results 1 to 5 of 5
  1. #1
    kestefon is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2013
    Posts
    108

    Opening Excel Workbook From Access with VBA

    I am working on an Access VBA project, and one step involves creating an Excel workbook.



    I pulled a function from a book as a starting point, but when I try to execute it I get an error that says, "An error occurred trying to add new workbook: Object variable or With block variable not set". I think this might have something to do with my references (i.e. Tools > References), but I'm not sure what I'm doing wrong. I'll attach a screenshot of my selections. Also, I'm using Excel and Access 2013.

    .Click image for larger version. 

Name:	References_Screenshot.png 
Views:	12 
Size:	19.7 KB 
ID:	15222

    And here is the VBA function I'm referring to.

    Code:
    Function modExcel_CreateNewWorkBook() As Boolean
    ' Open an existing word document
        On Error Resume Next
        Set wkbExcel = appExcel.Workbooks.Add
        If Err <> 0 Then
            MsgBox "An error occured trying to add new workbook : " & Err.Description, vbCritical, "Unable To Create Workbook"
            modExcel_CreateNewWorkBook = False
        Else
            modExcel_CreateNewWorkBook = True
        End If
    End Function

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    That is the proper library reference.

    The variable appExcel is not declared nor Set.

    If you got this example from a book, I am not impressed by it. The example does not declare variables.

    What do you want to happen with workbook once it is created? Should it be named and saved? Populated with data? Should Excel be made visible and allow working with the workbook directly?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    kestefon is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2013
    Posts
    108
    Oops, I actually forgot to paste the variable declarations, but they were indeed part of the code:

    Code:
    Option Compare Database
    Option Explicit
    
    
    Dim appExcel As Excel.Application
    Dim wkbExcel As Excel.Workbook
    Dim wksExcel As Excel.Worksheet
    As for what will be done in Excel, I have some existing Excel VBA procedures that I would run. That part is already complete...it's just a matter of integrating that with Access.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    And the Set lines?

    Sub modExcel_CreateNewWorkBook()
    Dim appExcel As Excel.Application
    Dim wkbExcel As Excel.Workbook
    Dim wksExcel As Excel.Worksheet
    Set appExcel = New Excel.Application
    Set wkbExcel = appExcel.Workbooks.Add
    appExcel.Visible = True
    wkbExcel.Close False
    appExcel.Quit
    Set wkbExcel = Nothing
    Set appExcel = Nothing
    End Sub
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You might check out examples of Excel automation at ken Snell's site
    http://www.accessmvp.com/kdsnell/EXCEL_MainPage.htm

    It's been helpful to me.......

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 02-03-2013, 11:25 PM
  2. Replies: 4
    Last Post: 12-17-2012, 01:21 PM
  3. Formatting Excel workbook from vba in access 2003
    By nyneave in forum Programming
    Replies: 29
    Last Post: 10-28-2012, 10:32 PM
  4. Send an excel workbook from access
    By haazzaa in forum Access
    Replies: 1
    Last Post: 07-26-2012, 05:40 PM
  5. Replies: 1
    Last Post: 11-21-2010, 10:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums