Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

For both Excel 2007 and 2010. I want to right click on an cell and have one of the choices be "Run Slump". And when the user chooses that, it will launch the executable:

     \\servername\path\slump.exe 12345

Where 12345 is the contents of the cell that was right-clicked. Can someone give me an overview of how to add that to the context menu and pass the contents of the cell to the VBA that launches the project, and what the best way is for a user to add the functionality to a copy of excel where on a machine where s/he is the adminstrator.

EDIT: In case it makes a difference to deciding how to deploy it - I should mention there will be maybe 7 or 8 people using it. We all work for the same company. I don't think we own a certificate to sign macros. We'll probably want to install and uninstall a bunch of similar things for different projects.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

If the functionality is only needed in one workbook; you can just add the macros to that workbook and save it as a xslm. That would be easiest.

But if the funtionality is needed for several workbooks, then you'll want to want to either make it as an addin (code that can be loaded to anyone's copy of excel) or perhaps a shared macro, depending on your needs.

The code is very simple. When the macro enabled workbook (or addin) is loaded, add an entery to the context menu (right click menu):

Private Sub Workbook_Open()
With Application.CommandBars("Cell")
    With .Controls.Add(Type:=msoControlButton)
        .BeginGroup = True
        .Caption = "Run Slump"
        .OnAction = "doTheSlump"
    End With
End With
End Sub

Whenever the workbooks is closed (or deactived), remove the context menu:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
  With Application.CommandBars("Cell")
    .Controls("Run Slump").Delete
  End With
End Sub

The code do run, should be in a vba module. In this example, it will open notebook using the cell's content as a filepath:

Sub doTheSlump()
retval = Shell("C:\WINDOWS\notepad.exe " + Selection.Value, 1)
End Sub
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.