Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 94
Default Stop VB from deleting modules from wrong file

I have an Excel 2003 template that includes 2 macros, the final bit of code
removes both macros, as shown below
--------------------------------------------------------------------------------
On Error Resume Next

Set vbCom = Application.VBE.ActiveVBProject.VBComponents

vbCom.Remove VBComponent:= _
vbCom.Item("Module3")

vbCom.Remove VBComponent:= _
vbCom.Item("Module4")

On Error Goto 0

End Sub
--------------------------------------------------------------------------------

Users often have multiple files created from the same template open at the
same time, unsaved. When the macro runs it sometimes removes the modules from
the wrong file rather than the active one.

What code do I need to add to ensure that the modules are deleted from the
active file only?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Stop VB from deleting modules from wrong file

I'd try:

set vbCom = activeworkbook.VBProject.VBComponents



Code Numpty wrote:

I have an Excel 2003 template that includes 2 macros, the final bit of code
removes both macros, as shown below.
--------------------------------------------------------------------------------
On Error Resume Next

Set vbCom = Application.VBE.ActiveVBProject.VBComponents

vbCom.Remove VBComponent:= _
vbCom.Item("Module3")

vbCom.Remove VBComponent:= _
vbCom.Item("Module4")

On Error Goto 0

End Sub
--------------------------------------------------------------------------------

Users often have multiple files created from the same template open at the
same time, unsaved. When the macro runs it sometimes removes the modules from
the wrong file rather than the active one.

What code do I need to add to ensure that the modules are deleted from the
active file only?


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 94
Default Stop VB from deleting modules from wrong file

So far so good.
Looks like ActiveWorkbook does the trick better than ActiveVBProject.

I just wish I knew why. Thanks Dave.

"Dave Peterson" wrote:

I'd try:

set vbCom = activeworkbook.VBProject.VBComponents


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,582
Default Stop VB from deleting modules from wrong file

Because the active project in the VB editor is not necessarily the active
workbook in Excel. Think about it.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"Code Numpty" wrote in message
...
So far so good.
Looks like ActiveWorkbook does the trick better than ActiveVBProject.

I just wish I knew why. Thanks Dave.

"Dave Peterson" wrote:

I'd try:

set vbCom = activeworkbook.VBProject.VBComponents




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
Can i stop people from deleting my excel file Sheriff Excel Discussion (Misc queries) 3 July 3rd 08 12:25 AM
How to stop file open macro prompt after deleting all macros? twor57 Excel Worksheet Functions 2 November 29th 05 05:00 PM
Deleting blank modules bj Excel Discussion (Misc queries) 0 June 2nd 05 07:52 PM
Deleting modules in XLA ExcelMonkey[_190_] Excel Programming 2 February 27th 05 02:17 PM
No success deleting modules BrianG[_4_] Excel Programming 1 October 18th 03 07:24 PM


All times are GMT +1. The time now is 03:36 PM.

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

About Us

"It's about Microsoft Excel"