Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default adding code to a workbook programmably

I have an access macro that creates en excel workbook. After the the
wookbook is created I can activate it.
I need to add this code to the workbook programmably.
My thoughts are to add the code to a string?

Dim CodeString As String
CodeString = "my code here"

or do it all as s single statement below

please help what am I missing. I am sure it is a simple syntax problem


ActiveWorkbook.VBProject.VBComponents("ActiveWorkb ook.ActiveSheet.CodeName").CodeModule.AddFromStrin g
_
"Private Function LogInformation(LogMessage$)" & vbCr & _
" On Error GoTo MakeFolder" & vbCr & _
" Entry: " & vbCr & _
" Open "M:\LogFiles\" & vbCr & _
" & Left(MyWorkbook.Name, Len(MyWorkbook.Name) - 4) & " Log.Log" For
Append As #1" & vbCr & _
" Print #1, LogMessage" & vbCr & _
" Close #1" & vbCr & _
" Exit Function" & vbCr & _
" MakeFolder:" & vbCr & _
" MkDir "M:\LogFiles" & vbCr & _
" Resume Entry" & vbCr & _
" End Function" & vbCr & _
" & vbCr & _
" Private Sub Workbook_Open()" & vbCr & _
" LogInformation "Opened by " & Application.UserName &" & vbCr & _
" " " & Format(Now, "dd mmm yyyy hh:mm:ss")" & vbCr & _
" End Sub"



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default adding code to a workbook programmably

Well this works, but you are adding workbook open code to a sheet module,
not much point in that

Sub bob()
Dim sCode As String
Dim myWorkbook

Set myWorkbook = ThisWorkbook
sCode = "Private Function LogInformation(LogMessage$)" & vbCr & _
" On Error GoTo MakeFolder" & vbCr & _
" Entry: " & vbCr & _
" Open ""M:\LogFiles\" & _
Left(myWorkbook.Name, Len(myWorkbook.Name) - 4) & _
" Log.Log"" For Append As #1" & vbCr & _
" Print #1, LogMessage" & vbCr & _
" Close #1" & vbCr & _
" Exit Function" & vbCr & _
" MakeFolder:" & vbCr & _
" MkDir ""M:\LogFiles""" & vbCr & _
" Resume Entry" & vbCr & _
" End Function" & vbCr & vbCr & _
" Private Sub Workbook_Open()" & vbCr & _
" LogInformation ""Opened by " & Application.UserName & _
" " & Format(Now, "dd mmm yyyy hh:mm:ss") & """" & vbCr & _
" End Sub"

ActiveWorkbook.VBProject _
.VBComponents(ActiveWorkbook.ActiveSheet.CodeName) _
.CodeModule.AddFromString sCode

End Sub


--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"Davidg" wrote in message
.. .
I have an access macro that creates en excel workbook. After the the
wookbook is created I can activate it.
I need to add this code to the workbook programmably.
My thoughts are to add the code to a string?

Dim CodeString As String
CodeString = "my code here"

or do it all as s single statement below

please help what am I missing. I am sure it is a simple syntax problem



ActiveWorkbook.VBProject.VBComponents("ActiveWorkb ook.ActiveSheet.CodeName")
..CodeModule.AddFromString
_
"Private Function LogInformation(LogMessage$)" & vbCr & _
" On Error GoTo MakeFolder" & vbCr & _
" Entry: " & vbCr & _
" Open "M:\LogFiles\" & vbCr & _
" & Left(MyWorkbook.Name, Len(MyWorkbook.Name) - 4) & " Log.Log" For
Append As #1" & vbCr & _
" Print #1, LogMessage" & vbCr & _
" Close #1" & vbCr & _
" Exit Function" & vbCr & _
" MakeFolder:" & vbCr & _
" MkDir "M:\LogFiles" & vbCr & _
" Resume Entry" & vbCr & _
" End Function" & vbCr & _
" & vbCr & _
" Private Sub Workbook_Open()" & vbCr & _
" LogInformation "Opened by " & Application.UserName &" & vbCr & _
" " " & Format(Now, "dd mmm yyyy hh:mm:ss")" & vbCr & _
" End Sub"





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding a chart to large workbook brings workbook activity to a hal Dale Excel Discussion (Misc queries) 6 December 29th 08 05:50 PM
run code on opening workbook and apply code to certain sheets Jane Excel Programming 7 August 8th 05 09:15 AM
Code in one workbook to call code in another XL file [email protected] Excel Programming 2 August 1st 05 03:37 PM
Adding Code to the This_workbook module of a created workbook RPIJG[_68_] Excel Programming 1 July 9th 04 06:35 PM
Adding Code Module to Workbook Charles Excel Programming 2 January 6th 04 08:43 AM


All times are GMT +1. The time now is 04:30 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"