LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel vba code disappears when saved workbook is re opened

Hi guys & gals,

Funny one this.

Using an amalgam of 2 codes I found on the ever helpful Chip Pearson's
website
I (finally) figured out how to write to the 'ThisWorkbook' module of a
VBA project.
All good so far...

Until, that is, when I save the workbook the code resides in and
re-open it said code has disappeared! If there is other code present
in the same standard module then only the code goes - if it resides in
a module on it's own, then the module disappears.

As I'm sure you can imagine this is a little annoying and I'm not
sufficiently gifted to hazard a guess as to what is going on as there
seems to be nothing about the code itself that would make this happen.

It has been a good learning experience though as I had to re-write it
twice before I saved it elsewhere and managed to knock it down by a
few lines each time.

Code follows.....

Sub Auto_Write_To_ThisWorkbook()

Dim StartLine As Long
Dim LineNum As Long

''Inserts BeforeClose event in ThisWorkbook module of the active
workbook

'With ActiveWorkbook.VBProject.VBComponents("ThisWorkboo k").CodeModule
' StartLine = .CreateEventProc("BeforeClose", "Workbook") + 1
'End With

'Inserts BeforeClose event in ThisWorkbook module of the active
workbook
'and adds the body of the text of the procedure to the event

With ActiveWorkbook.VBProject.VBComponents("ThisWorkboo k").CodeModule
StartLine = .CreateEventProc("BeforeClose", "Workbook") + 2
LineNum = 3
.InsertLines LineNum, _
"Ans = MsgBox(""Don't forget to run the 'Update Lookup Tables'"" &
vbCr & ""procedure once all comments are updated"" & vbCr & ""Would
you like to open this now?"", vbYesNo, ""Information"")" & Chr(13) & _
"" & Chr(13) & _
"If Ans = vbYes Then" & Chr(13) & _
" Workbooks.Open Filename:= _" & Chr(13) & _
" ""Z:\TRADE FINANCE\Shared Area\Within Trade
Finance\SUSPENCE ACCOUNT\Suspence Queries\UPDATE LOOKUP TABLES.xls"""
& Chr(13) & _
"Else" & Chr(13) & _
" If Ans = vbNo Then" & Chr(13) & _
" Exit Sub" & Chr(13) & _
" End If" & Chr(13) & _
"End If" & Chr(13) & _
""

End With

End Sub

Any help/comments on this would be much appriciated.

Cheers,
JF.
 
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
Excel file opened as read-only, if saved by another user GeEf Excel Discussion (Misc queries) 2 August 28th 05 04:29 PM
Possible to write VBA code to send out an EMAIL everytime a workbook is opened? Marcello do Guzman[_2_] Excel Programming 4 September 2nd 04 07:59 AM
How to find out the residing directory of the opened workbook using VBA code ? Adrian[_7_] Excel Programming 2 August 7th 04 11:34 AM
Code to check if workbook is the last one opened KimberlyC Excel Programming 1 February 13th 04 12:04 AM
Code to tell if workbook is opened in browser BrianB Excel Programming 5 August 19th 03 02:47 PM


All times are GMT +1. The time now is 08:38 AM.

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"