Force Macro - question

ryan_law2000

Well-known Member
Joined
Oct 2, 2007
Messages
738
Ok so i know you cant force macro enable but this is kinda what i would like to do:

I have 10 Sheets
Going to make all sheets Hidden except for one
this sheet will just have something saying "MUST ENABLE MACROS"

when my workbook is opened the application is hidden and only a userform is shown
at this point i would like the rest of my sheets to be visible
couple names are: "Main Page" and "Enter"

now all the data will be gatherd through multiple userforms and eventually closing the userform with this code:

Code:
Private Sub CommandButton10_Click()
If MsgBox("Are you sure you want close Report", vbYesNo + vbQuestion) = vbYes Then
    ActiveWorkbook.Close savechanges:=False
Else
End If
End Sub

I would like to add to this code
Hide all sheets other then sheet named "WARNING"

So the next time the sheet is opened (if macro's are not enabled) it will just show the one "Warning" sheet. Once macro's are enabled again it should show the userform and unlock all the sheets.

This was just an Idea i thought might work but I wouldnt know what the code would be. and i have tried everything
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Wouldn't it be trivial to disable macros and then unhide the sheets? Do any of these turnkey solutions actually work?
 
Upvote 0
Ok so i know you cant force macro enable but this is kinda what i would like to do:

I have 10 Sheets
Going to make all sheets Hidden except for one
this sheet will just have something saying "MUST ENABLE MACROS"

One thing you can do is, in a standard module, paste in this:


Public Function MacrosEnabled() As Boolean
MacrosEnabled = True
End Function



In a worksheet cell, enter this native formula:
=IF(ISERROR(MacrosEnabled()),"Macros not enabled","Macros enabled")


Depending on when you disable or enable macros, you may need to select the cell and press F2 and Enter to refresh the formula, but once refreshed it will return the enabled status of macros.

Keep in mind that the formula and UDF report on the status at the time the workbook opens, which means if you change your security settings when the workbook is open, the formula will not automatically update itself. That is the nature of Excel's security (such as that is) platform. When you save and close the workbook, and then re-open it, the formula will return the correct result, and as I said, the formula may need to be refreshed depending on the sequence of when you opened, closed, and changed security settings.

It does not force anyone to do anything, but it is a non-VBA way to advise the user about macros being enabled or not enabled.

You may also find this link from Ivan Moala useful:

http://www.xcelfiles.com/SplashScrn.html
 
Upvote 0
The sheets are usually hidden with xlVeryHidden intending to make it very difficult to unhide them without VBA, so macros OFF surely complicates things for them.

It is trivial to circumvent techniques like this for people like us who live and breath so much VBA. Fortunately most users do not fall into that category and this would be fine for normal day-to-day operational security in an office.

Once you pass you Excel docs outside the office into the world...well, no guarantees at that point. That's why we only pass out PDFs of our Excel docs.
 
Upvote 0
Hi all.

here is a novel approach to this very old problem : We are going to open the workbook from an EXE file to ensure that the workbook is (hopefully) ALWAYS open with the Macros enabled !.

Concept : How it works :

The very first time the workbook is ran , the code creates a Standard VB EXE file on the fly (I developped & compiled the EXE in VB6 and stored the EXE bytes in the cells of a hidden sheet inside the workbook). Once the EXE is created, the hidden sheet is no longer needed so the code permanently deletes the hidden sheet.

For convinience, the EXE is automatically saved on the user's desktop as the workbook will be launched from the EXE. (This location can be changed as needed)

Next, the code embeeds the excel workbook as an ADS file inside the "C:\Windows" folder. and gives it the name of ":ADS_file.dat". There are two reasons for this 1-: so that the EXE file can subsequently easily locate the workbook and open it. 2-: The initial workbook is supposed to be deleted.


Example : (Follow these steps)

1- Download this workbook and save it somwhere on your disk preferably on your Desktop. (Note the workbook is big in size becuase of the EXE bytes stored in the hidden sheet. This size problem will be resolved once the EXE is created and the hidden sheet is deleted.)

2- With the Macros enabled, Open the workbook and Wait until you are prompted that the EXE file has been created.

3- Now close the workbook and you will see that the EXE file has just been created and saved on your desktop.

4- Now, you no longer need the initial workbook so you can now safely delete it.

5- Finally, disable Excel Macros and open the workbook via the newly created EXE . You will see that the Macros associated with the workbook are working despite Macros being disabled !

You can carry on working with the workbook as normal as well as save any changes.


Limitations:

- Only tested in Excel 2003 Win XP. So I don't know if this will work with other versions.

- The workbook cannot be open via the File>Open Menu. It behaves like a standalone workbook. You just double click it's icon and it is launched in a new excel instance.

-Maybe other issues that I haven't noticed.


Code in the Workbook Module :

Code:
Option Explicit
 
Private Sub Workbook_Open()
 
    Call Create_exe(True)
 
End Sub
 
Private Sub Workbook_BeforeSave _
(ByVal SaveAsUI As Boolean, Cancel As Boolean)
 
    Call UpdateFile(True)
 
End Sub

Code in a Standard Module :

Code:
Option Explicit
 
Private Declare Function SHGetFolderPath Lib "shfolder" _
   Alias "SHGetFolderPathA" _
   (ByVal hwndOwner As Long, ByVal nFolder As Long, _
   ByVal hToken As Long, ByVal dwFlags As Long, _
   ByVal pszPath As String) As Long
 
Private Const SHGFP_TYPE_CURRENT As Long = 0
Private Const CSIDL_DESKTOPDIRECTORY As Long = &H10
Private Const FILE_PATH_NAME = "C:\Windows" & ":ADS_file.dat"
 
Public Sub Create_exe(ByVal Dummy As Boolean)
 
    Dim oExeBytesWsh As Worksheet
    Dim Var1 As Variant
    Dim Var2 As Variant
    Dim i As Long
    Dim FileNum As Integer
    Dim Bytes() As Byte
 
 
    On Error Resume Next
    Set oExeBytesWsh = ThisWorkbook.Worksheets("exeBytes")
 
    If Err.Number = 0 Then
 
        On Error GoTo 0
 
        With ThisWorkbook.Worksheets("exeBytes")
            Var1 = .Range(.Cells(1, 1), .Cells(1, 1).End(xlDown))
            Var2 = .Range(.Cells(1, 2), .Cells(1, 2).End(xlDown))
        End With
 
        ReDim Bytes(LBound(Var1) To UBound(Var1) + UBound(Var2))
 
        For i = LBound(Var1) To UBound(Var1)
            Bytes(i) = CByte(Var1(i, 1))
        Next
 
         For i = UBound(Var1) To UBound(Var2)
            Bytes(i) = CByte(Var2(i, 1))
        Next
 
    '
        FileNum = FreeFile
         Open GetDeskTopFolderPath & "\ForceMacros.exe" _
         For Binary As #FileNum
            Put #FileNum, 1, Bytes
        Close FileNum
 
 
        Application.DisplayAlerts = False
        ThisWorkbook.Worksheets("exeBytes").Visible = xlSheetHidden
        ThisWorkbook.Worksheets("exeBytes").Delete
        Application.DisplayAlerts = True
 
        Call UpdateFile(True)
 
        MsgBox "EXE created successfully." & _
        vbNewLine & "Disable the Macros and run the EXE file"
 
    End If
 
End Sub
 
Public Sub UpdateFile(ByVal Dummy As Boolean)
 
    Application.EnableEvents = False
 
    ThisWorkbook.Save
 
    Call EmbeedFile(ThisWorkbook.FullName)
 
    Application.EnableEvents = True
 
End Sub
 
Private Sub EmbeedFile(ByVal PathName As String)
 
    Dim Bytes() As Byte
    Dim lFileNum As Integer
 
    ReDim Bytes(1 To FileLen(PathName))
 
    lFileNum = FreeFile
    Open PathName For Binary Access Read As #lFileNum
    Get #lFileNum, , Bytes
    Close #lFileNum
    lFileNum = FreeFile
    Open FILE_PATH_NAME For Binary As #lFileNum
    Put #lFileNum, 1, Bytes
    Close lFileNum
 
End Sub
 
Private Function GetDeskTopFolderPath() As String
 
    Dim sBuffer As String * 260
    Dim lngReturn As Long
 
    lngReturn = SHGetFolderPath _
    (0, CSIDL_DESKTOPDIRECTORY, 0, SHGFP_TYPE_CURRENT, sBuffer)
    GetDeskTopFolderPath = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
 
End Function

I hope this approach proves stable enough and works accross different versions.
 
Upvote 0
Hi Jaafar,

I have been saving your code for use for a while now and just tried to run it. I am using Excel 2010 on Windows 7 and am getting an error at line of code:

Code:
Open FILE_PATH_NAME For Binary As #lFileNum

in this sub:

Code:
Private Sub EmbeedFile(ByVal PathName As String)

I wonder if you have a newer version of this code that you can share with us.

Thanks.

AMAS
 
Upvote 0
Hi Jaafar,

I tried this code on my PC and it used to work well. Recently Symantec EndPoint Protection got updated to version 12.1. Now when I run this code, it detects the exe file as a virus (bloodhound sonar 9) to be specific. Is this just a false positive or something to worry about.

Thanks for your help
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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