|
-
Jun 30th, 2006, 12:01 PM
#1
Thread Starter
New Member
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:
Dim xlExcel As Excel.Application
Dim xlWorkbook As Excel.Workbook
Dim xlWorksheet As Excel.Worksheet
Set xlExcel = CreateObject("Excel.Application")
Set xlWorkbook = xlExcel.Workbooks.Open(strFilePath)
'do some formating
'need to change or put some code in Workbook_Open()
xlWorkbook.Save
xlWorkbook.Close
xlExcel.Application.Quit
Set xlWorkbook = Nothing
Set xlExcel = Nothing
Please let me know if that is possible to modify the code in the Workbook_Open(). Thanks for your help.
-
Jun 30th, 2006, 02:15 PM
#2
Re: Workbook_Open()
Welcome to the forums!
This code snipped should give you a start...
VB Code:
Dim sCode As String
Dim lNextLine As Long
sCode = "Private Sub Workbook_Open()" & vbCrLf
sCode = sCode & "MsgBox ""Hi aroV"" " & vbCrLf
sCode = sCode & "End Sub"
With xlWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
lNextLine = .CountOfLines + 1
.InsertLines lNextLine, sCode
End With
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
Jun 30th, 2006, 03:12 PM
#3
Thread Starter
New Member
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.
-
Jun 30th, 2006, 03:28 PM
#4
Thread Starter
New Member
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?
-
Jul 4th, 2006, 06:10 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|