简体   繁体   中英

Using Excel Macros on a XSLX document

This is going to be a long question I'm afraid.

We ask our customers to fill-in in an excel-document with some client-data. This data is separated in columns (Id, Name, Phone, etc). I have a hidden worksheet where these column-headers are mapped to fields which we use in our database. For security reasons I don't want/cannot distribute this file as a macro-enabled document. But when they send the file back I want to execute a macro on this file which saves the content into an CSV so this file can be imported in the DB.

I was thinking of creating the macro in the PERSONAL.XSLB file so I can use it on the returned XSLX file.

It this the correct way to handle it like this? Or should I use a different approach?

Thanks for any help

[Edit] By the way if there is already an answer for this, I couldn't find it. Maybe I didn't use the correct search terms.

This sounds like a great opportunity for an Add-in -- I wrote a post about it here: How to make a reusable button from a macro?

Here is a short summary of the VBA:

(1) Save an xlsm or xlsb file with a name that's easy to increment for versions of your add-in.

(2) Add the following scripts into ThisWorkbook to ensure that you create a menu bar when the workbook is opened and when the workbook is activated:

Private Sub Workbook_Open()
    Call CreateMenuBar
End Sub

Private Sub Workbook_Activate()
    Call CreateMenuBar
End Sub

(3) Create a new module and add the following code to create, delete and update your menu bar:

Option Explicit
Sub CreateMenuBar()

    Dim MenuObject As CommandBarPopup
    Dim MenuItem As Object
    Dim SubMenuItem As Object

    'clear the old menu bar
    Call DeleteMenuBar("&MyMenuBar")

    'create the menu bar and drop down options
    Set MenuObject = Application.CommandBars(1).Controls.Add(Type:=msoControlPopup, _
        before:=10, Temporary:=True)
    MenuObject.Caption = "&MyMenuBar"
    MenuObject.OnAction = "UpdateMenuBar"

    'first level menu option
    Set MenuItem = MenuObject.Controls.Add(Type:=msoControlPopup)
    MenuItem.Caption = "&First Menu Stuff"

        'link to first script
        Set SubMenuItem = MenuItem.Controls.Add(Type:=msoControlButton)
        SubMenuItem.Caption = "&First Script"
        SubMenuItem.OnAction = "Script1"

        'link to second script
        Set SubMenuItem = MenuItem.Controls.Add(Type:=msoControlButton)
        SubMenuItem.Caption = "&Second Script"
        SubMenuItem.OnAction = "Script2"

    'first level menu option
    Set MenuItem = MenuObject.Controls.Add(Type:=msoControlPopup)
    MenuItem.Caption = "&Second Menu Stuff"

        'link to third script
        Set SubMenuItem = MenuItem.Controls.Add(Type:=msoControlButton)
        SubMenuItem.Caption = "&Third Script"
        SubMenuItem.OnAction = "Script3"

End Sub

Sub DeleteMenuBar(MenuName As String)
    On Error Resume Next
    Application.CommandBars(1).Controls(MenuName).Delete
    On Error GoTo 0
End Sub

Sub UpdateMenuBar()
    'do special checks, like verifying sheets, in this routine
End Sub

(4) Verify your scripts work and save the file.

(5) Save the file again as an xlam or xla file and you've now got your scripts handy!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM