Results 1 to 4 of 4
  1. #1
    JonMulder is offline Novice
    Windows XP Access 2007
    Join Date
    Apr 2011
    Posts
    24

    Adding VB Code to Excel spreadsheet from within Access

    Greetings,



    I have a form that creates an "Excel.Application" object, creates a button on the first sheet ("Sheet1"), and adds an "OnAction" module for the button.

    The code gets so far as adding the button, but when I try to add the CodeModule, I get a Run-time error '9', saying subscript out of range.

    Any ideas? I've got my Excel set to "Enable all macros" and to "Trust access to the VBA project object model".

    Here's my code snippet:

    Code:
     
    Private Sub CmdTest_Click()
    Set x1 = CreateObject("Excel.Application")
    Set x1Wkbk = x1.Workbooks.Add
    x1.Visible = True
    x1.ActiveSheet.Buttons.Add(199.5, 20, 81, 36).Select
    x1.Selection.Name = "New Button"
    x1.Selection.OnAction = "CheckTotals"
    x1.ActiveSheet.Shapes("New Button").Select
    x1.Selection.Characters.Text = "Check Totals"
    x1.Selection.OnAction = "ShowMessage"
    '''Dim sht As Object
    Dim shtCode As String
    '''Set sht = x1.Sheets("Sheet1")
    shtCode = _
       "Sub ShowMessage" & vbNewLine & _
       "Msgbox(" & Chr(34) & "Hello Jonny" & Chr(34) & ")" & vbNewLine & _
       "End Sub"
    x1.Sheets("Sheet1").Select
    x1.ActiveWorkbook.VBProject.VBComponents(x1.Sheets("Sheet1").CodeName).CodeModule.AddFromString shtCode
    End Sub
    Thanks for any help you can provide!

    Jonathan Mulder

  2. #2
    burrina's Avatar
    burrina is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    Do you have any empty cells? If so, delete these and try again.

  3. #3
    JonMulder is offline Novice
    Windows XP Access 2007
    Join Date
    Apr 2011
    Posts
    24
    Actually, the code creates a 3-sheet workbook (which is completely empty except for button).

    It hangs up on the following line:
    x1.ActiveWorkbook.VBProject.VBComponents(x1.Sheets ("Sheet1").CodeName).CodeModule.AddFromString shtCode

    I'm thinking that it's a setting in my excel. When I save spreadsheets, it always saves as "*.xls" format which cannot contain macro codes as "*.xlsm" files can.

    Nope! That didn't work. I changed my Save As to "*.xlsm" and still get the error.

  4. #4
    JonMulder is offline Novice
    Windows XP Access 2007
    Join Date
    Apr 2011
    Posts
    24
    I have solved it! I did a search on "CodeName).CodeModule.AddFromString " and found lots of discussion.

    I had to add another library reference called " Microsoft Visual Basic for Application Extensibility 5.3"

    My best solution came from here:
    http://stackoverflow.com/questions/2...nctions-in-vba

    The test code below adds two buttons, then adds subroutines for each one into the VB Module for the spreadsheet. It's pretty neat!

    Hope this helps other folks!

    Jonathan Mulder
    Engineering Geologist
    California Department of Water Resources

    Code:
     
    Private Sub CmdTest_Click()
    Set x1 = CreateObject("Excel.Application")
    Set x1Wkbk = x1.Workbooks.Add
    x1.Visible = True
    x1.ActiveSheet.Buttons.Add(100, 20, 81, 36).Select
    x1.Selection.Name = "New Button1"
    x1.ActiveSheet.Shapes("New Button1").Select
    x1.Selection.Characters.Text = "Check Totals1"
    x1.Selection.OnAction = "NewSub1"
    x1.ActiveSheet.Buttons.Add(200, 20, 81, 36).Select
    x1.Selection.Name = "New Button2"
    x1.ActiveSheet.Shapes("New Button2").Select
    x1.Selection.Characters.Text = "Check Totals2"
    x1.Selection.OnAction = "NewSub2"
    Dim VBProj As VBIDE.VBProject
    Dim VBComp As VBIDE.VBComponent
    Dim CodeMod As VBIDE.CodeModule
    Set VBProj = x1Wkbk.VBProject
    Set VBComp = VBProj.VBComponents.Add(vbext_ct_StdModule)
    Set CodeMod = VBComp.CodeModule
    With CodeMod
       .DeleteLines 1, .CountOfLines
    End With
    With CodeMod
        .InsertLines 1, "Sub NewSub1()"
        .InsertLines 2, "    MsgBox ""hi from your new sub1!"""
        .InsertLines 3, "End Sub"
    End With
    With CodeMod
        .InsertLines 4, "Sub NewSub2()"
        .InsertLines 5, "    MsgBox ""hi from your new sub2!"""
        .InsertLines 6, "End Sub"
    End With
    End Sub

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

Similar Threads

  1. Format an Excel spreadsheet from Access
    By crowegreg in forum Programming
    Replies: 4
    Last Post: 12-23-2013, 07:12 PM
  2. Creating excel spreadsheet from access vba
    By nyneave in forum Programming
    Replies: 1
    Last Post: 10-12-2012, 09:59 AM
  3. Open Excel spreadsheet in Access
    By carlyd in forum Forms
    Replies: 1
    Last Post: 02-17-2012, 01:09 PM
  4. Email Excel Spreadsheet from Access
    By Nancy in forum Access
    Replies: 2
    Last Post: 11-09-2010, 02:37 PM
  5. Replies: 1
    Last Post: 08-12-2010, 10:04 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