user1049518
user1049518

Reputation: 291

how to force user to enable macros in the sheet?

I have a template with macros to validate the data on clicking the button. The template having only one sheet and no sheets added in that file (It should have one sheet only). Now, I have to do is, when the file is open without macros enabled, i need to display a image, please enable macros other wise no need to display.

Thanks a lot in advance for any help.

Upvotes: 2

Views: 9033

Answers (1)

Siddharth Rout
Siddharth Rout

Reputation: 149325

how to force user to enable macros in the sheet?

The answer to your question is "You Can't"

However here is an alternative by Ken Puls which will suit your purpose just fine.

Topic: Force users to enable macros in a workbook

Link: http://www.vbaexpress.com/kb/getarticle.php?kb_id=379

QUOTE FROM THAT LINK

Since there is no way to use a macro to turn on macros, a technique to ensure the user has enabled macros is desirable. This particular method hides all sheets except a "welcome" sheet which tells the user to enable macros, and is enforced every time the workbook is saved. If the user opens the workbook with macros enabled, the sheets will all be unhidden by the macro. The hiding of sheets is also done using Excel VeryHidden property, which means that the sheets cannot be unhidden using Excel's menus. Keep in mind, however, that this only affects this workbook, so a user could use a macro from another workbook to unhide all of your sheets. Chances are, however, if your user is that skilled, they can always get into your file anyway. NOTE: To prevent some event looping issues, this code requires overruling Excel's built in Save events, and also requires replicating Excel's "Workbook has changed, do you want to save" prompts and actions. This code takes care of all of it. It does, however, create a very minor issue when closing the file. If the user trys to quit the application, it will close the workbook, but not Excel. Quitting again will close Excel completely.

FOLLOWUP

Before you close the Workbook do the following

  1. Insert a new Row
  2. Increase the height of the row
  3. Hide the rest of the rows
  4. Resize and Move picture at a relevant location

And when you open do the reverse of above

  1. Unhide the rest of the rows
  2. Delete the 1st Row
  3. Decrease the size and Move the picture to a far left location in the sheet

Important.

When you place the picture on the sheet, right click on the picture and click on 'Format Picture'. In the Format Picture Dialog, select Don't move or size with cells and uncheck the option which says Print Object

enter image description here

CODE WHEN CLOSING THE WORKBOOK - Apply the same concept as shown in the above link

Dim shp As Shape

With Sheets("Sheet1")
    .Rows(1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    .Rows(1).RowHeight = 300

    .Rows("2:" & .Rows.Count).EntireRow.Hidden = True
    Set shp = .Shapes("Picture 1")
    With shp
        .Top = Sheets("Sheet1").Range("A1").Top
        .Left = Sheets("Sheet1").Range("A1").Left
        .LockAspectRatio = msoFalse
        .Height = 100
        .Width = 100
    End With
End With

CODE WHEN OPENING THE WORKBOOK - Apply the same concept as shown in the above link

Dim shp As Shape

With Sheets("Sheet1")
    .Rows("1:" & .Rows.Count).EntireRow.Hidden = False
    .Rows(1).Delete

    Set shp = .Shapes("Picture 1")
    With shp
        .LockAspectRatio = msoFalse
        .Height = 0
        .Width = 0
        .Top = Sheets("Sheet1").Range("A1").Top
        .Left = Sheets("Sheet1").Range("IV1").Left
    End With
End With

NOTE: In the above code, replace "Sheet1" with the relevant sheet name and replace "Picture 1" with the relevant picture name. I am showing the image in Cell A1. You can amend that to display the picture where ever you want by adjusting .Top and .Left of the Shape.

SNAPSHOT IF MACRO IS DISABLED

enter image description here

Upvotes: 6

Related Questions