Checking file type

I/ATech

New Member
Joined
Apr 22, 2009
Messages
29
On a template that is used monthly, on Workbook_Open() a message alerts the user how to name the file and where to place it when saving it, then the macro deletes itself. Problem is, if the template is opened for modifications the macro runs and deletes itself. How would I check to see if the template itself has been opened to preempt executing the code? Or is there a better approach?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I/ATech.

First, disable macros in excel. close excel.

Then open the Template, the macro can run as the macros are disabled.

Don't forget to re-enable macros afterwards.

Regards

ColinKJ
 
Upvote 0
Thank you ColinKJ. By default we all have security set to medium, so we get the alert message that allows us to disable the macro prior to opening the template, or for that matter, the workbook. Problem is I do not control who will open the template as a template in order to make minor modifications, nor do I want to (some of the folks using these tamplates are in other parts of the country). So my thinking is to bypass the code if the template is opened, but not if a workbook is created from the template.
 
Upvote 0
How about using Open with the Application object instead?
 
Upvote 0
TOTALLY UNTESTED
Code:
Private Sub WorkBook_Open
If Right(Me.FullName,4)=".xlt" Then Exit Sub
'rest of code
lenze
 
Upvote 0
Giving the template a name of: "XXXDeleteMe.xlt"

Create a new Class Module named: "Class1"

In the Class Module:
Code:
Option Explicit
 
Public WithEvents XLApp As Application
 
Private Sub XLApp_WorkbookOpen(ByVal Wb As Workbook)
    If Not InStr(1, Wb.Name, ".xlt", vbTextCompare) = 0 _
    And ThisWorkbook.Name = "XXXDeleteMe.xlt" Then
        MsgBox "Editing template"
    ElseIf InStr(1, Wb.Name, ".xlt", vbTextCompare) = 0 _
    And Not InStr(1, Wb.Name, "XXXDeleteMe", vbTextCompare) = 0 Then
        MsgBox "Delete me"
    End If
End Sub

In ThisWorkbook Module:
Code:
Option Explicit
 
Dim MyXL As New Class1
 
Private Sub Workbook_Open()
    Set MyXL.XLApp = Application
End Sub

Only cursorarily tested, but this should look to see if the template itself is being opened, or is another wb being created (based on the template).

Hope this helps,

Mark
 
Upvote 0
GTO, I am not proficient enough to understand what you mean with your first reply, but will be doing some reading this weekend to try and understand the second.

Lenze, tried adding the code to the existing, but it did not prevent the deletion. On the other hand, while looking in VB Help and other places to understand it I picked up a few more pointers.

Great stuff, I am having a blast. Thanks!
 
Upvote 0
Maybe try this as a test
Code:
Sub Check
MsgBox RIGHT(Me.FullName,4)
End Sub
to see what you get when you open as a xlt or xls
 
Upvote 0
Lenze, I created a new Template, put the code in ThisWorkbook, and nothing happened whether I opened it as the template or a workbook from the template.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top