Results 1 to 5 of 5

Thread: Workbook_Open()

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    11

    Workbook_Open()

    I’m opening a workbook from Access, populating and formatting some cells and saving the workbook. I need to put some code in workbook sub so when user opens the spreadsheet code will be triggered. Below is the code that I use to create the workbook.


    VB Code:
    1. Dim xlExcel As Excel.Application
    2. Dim xlWorkbook As Excel.Workbook
    3. Dim xlWorksheet As Excel.Worksheet
    4.  
    5. Set xlExcel = CreateObject("Excel.Application")
    6. Set xlWorkbook = xlExcel.Workbooks.Open(strFilePath)
    7.  
    8. 'do some formating
    9. 'need to change or put some code in Workbook_Open()
    10.  
    11.  
    12. xlWorkbook.Save
    13. xlWorkbook.Close
    14. xlExcel.Application.Quit
    15. Set xlWorkbook = Nothing
    16. Set xlExcel = Nothing
    Please let me know if that is possible to modify the code in the Workbook_Open(). Thanks for your help.

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Workbook_Open()

    Welcome to the forums!

    This code snipped should give you a start...
    VB Code:
    1. Dim sCode As String
    2.     Dim lNextLine As Long
    3.  
    4.     sCode = "Private Sub Workbook_Open()" & vbCrLf
    5.     sCode = sCode & "MsgBox ""Hi aroV"" " & vbCrLf
    6.     sCode = sCode & "End Sub"
    7.    
    8.     With xlWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
    9.         lNextLine = .CountOfLines + 1
    10.         .InsertLines lNextLine, sCode
    11.     End With
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    11

    Re: Workbook_Open()

    Thanks DKenny,
    I have tried to execute this code. I’m getting a runtime error 1004 – “Programmatic access to Visual Basic Project is not trusted”. Thanks again.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    11

    Re: Workbook_Open()

    Below is the resolution from Microsoft:

    For any Automation client to be able to access the VBA object model programmatically, the user running the code must explicitly grant access. To turn on access, the user must follow these steps:
    1. Open the Office application in question. On the Tools menu, click Macro, and then click Security to open the Macro Security dialog box.
    2. On the Trusted Sources tab, click to select the Trust access to Visual Basic Project check box to turn on access.
    3. Click OK to apply the setting. You may need to restart the application for the code to run properly if you automate from a Component Object Model (COM) add-in or template.


    I’m wondering this change can be done programmatically?

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Workbook_Open()

    you can put most of the code from the macro into your vbcode (with some editing to tell it it is the excel object) then you don't need to call the macro at all,

    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width