Mohan Kumar Karunakaran

Hi,

Is there anyway to assign a shortcut key for the procedures I have created in excel addin using VSTO 2005 SE. I have designed ribbon for these procedures and I would like to access it with shortcut keys like Ctrl + Alt + B or Ctrl + Shift + R

I can assign keytip in ribbon.xml file but it uses Alt combinations and not easily accessible also for custom created procedures. I would like to keep the shortcut as simple as possible. In VBA we used to assign shortcut key using Application.onkey function. But in VSTO 2005 SE is there any way to do this.

I have read few keybindings posts but I am not clear and any help would be appreciated.

Thanks,

Mohan




Re: Visual Studio Tools for Office Shortcut Key for excel addin

Ji Zhou – MSFT

Hi Mohan,

Unfortunately, the short cut is only supported to be assigned by us in VBA. This question is discussed in the forum before. You can refer the following link:

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=405023&SiteID=1

Thanks

Ji






Re: Visual Studio Tools for Office Shortcut Key for excel addin

Mohan Kumar Karunakaran

Hi Ji,

Thanks for your reply and I have gone through the post already and I could not understand it fully as I am very new to .net environment. I have created an excel addin using VSTO SE and below given is a small bit of coding which I have done.

ribbon1.xml

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">

<ribbon>

<tabs>

<tab id="CustomTab" label="My Menu" visible ="true">

<group id="MyGroup" label="My Group">

<button id="BlueUL" label="Blue UL" size="normal" onAction="MyModule" />

</group>

</tab>

</tabs>

</ribbon>

</customUI>

ribbon1.vb - ribbon callback

Public Sub MyModule(ByVal control As Office.IRibbonControl)
Dim ThisWkSht As New Excel.Worksheet
Dim rng As Excel.Range
ThisWkSht = Globals.ThisAddIn.Application.ActiveSheet
rng = Globals.ThisAddIn.Application.Selection

Select Case control.Id
Case "BlueUL"
With rng.Borders(Excel.XlBordersIndex.xlEdgeBottom)
.LineStyle = Excel.XlLineStyle.xlContinuous
.Weight = Excel.XlBorderWeight.xlThin
.ColorIndex = 7
End With
End Select

End Sub

Now my requirement is to assign "Ctrl + Alt + B" to the button. If you could show a sample vba procedure or some clear walk through on how to assign the shortcut key that would be great. Since I got struck with this thing for so long and the search also doesn't yield good results.

Thanks in advance,

Mohan






Re: Visual Studio Tools for Office Shortcut Key for excel addin

Ji Zhou – MSFT

Hi Mohan,

Sorry about not realizing you are working on Office 2007 and using Ribbon.xml to customize your Office UI. The thread I posted above is target at the scenario that we create the button using CommandBar and CommandBarControl. So we can create a VBA Module get the CommandControl’s handle and call it Execute() method. And we can assign a shortcut key for the VBA Module. And then save the module as a Excel Add In.

If we create the button via customize ribbon.xml, we cannot get the button’s handle in VBA. So the above way is stumped this time!

My thought is that, we should still use VBA to assign the shortcut key, but alternatively we should expose an ExcelAddin Automation Service to let VBA call our function. I will show the process step by step (I am writing C# example. I think you can convert it to VB version without much effort. And if you have any difficulty about that, please feel free to let me know). And if you want to understand this post better, you can refer an article on Andrew Whitechapel’s blog first, here: http://blogs.msdn.com/andreww/archive/2007/01/15/vsto-add-ins-comaddins-and-requestcomaddinautomationservice.aspx

1. I create an Excel Add In named ExcelAddIn using VSTO SE and Excel 2007

2. I add a Ribbon support Item to my project. Add the same Ribbon description to the ribbon.xml file as yours

3. Using the following call back: (We will implement addinUtilities.CallBlueUl later)

Code Block

public void MyModule(Office.IRibbonControl control)

{

switch (control.Id)

{

case "BlueUL":

Globals.ThisAddIn.addinUtilities.CallBlueUl();

break;

default:

break;

}

}

4. Add an interface named IAddinUtilities in the project. Make it like:

Code Block

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.InteropServices;

using Office = Microsoft.Office.Core;

using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelAddIn

{

[ComVisible(true)]

[Guid("B523844E-1A41-4118-A0F0-FDFA7BCD77C9")]

[InterfaceType(ComInterfaceType.InterfaceIsDual)]

public interface IAddinUtilities

{

void CallBlueUl();

}

}

5. Add a Class named AddinUtilities, and make it implement the interface IAddinUtilities, codes like:

Code Block

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.InteropServices;

using Excel = Microsoft.Office.Interop.Excel;

using Office = Microsoft.Office.Core;

namespace ExcelAddIn

{

[ComVisible(true)]

[ClassInterface(ClassInterfaceType.None)]

public class AddinUtilities : IAddinUtilities

{

public void CallBlueUl()

{

Excel.Range range = Globals.ThisAddIn.Application.Selection as Excel.Range;

range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous;

range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThin;

range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].ColorIndex = 7;

}

}

}

6. In ExcelAddIn Class, we override the RequestComAddInAutomationService() to return a new instance of the Class we created above. And make the field as public, which makes the call in step 3 can be compiled.

Code Block

public partial class ThisAddIn

{

public AddinUtilities addinUtilities;

protected override object RequestComAddInAutomationService()

{

if (addinUtilities == null)

{

addinUtilities = new AddinUtilities();

}

return addinUtilities;

}

}

7. Compile and run the project. The Excel application shows up. Click Developer Tab->Code Group->Macros Button. In pop up window, you type the Macro Name you want. Typically, we use CallBlueUl here. And then click Button Create.

8. The Microsoft Visual Basic IDE pops up, and copy the following codes into the module, and make it look like:

Code Block

Sub CallBuleUl()

Dim addin As Office.COMAddIn

Dim automationObject As Object

Set addin = Application.COMAddIns("ExcelAddin")

Set automationObject = addin.Object

automationObject.CallBlueUl

End Sub

9. Close the Microsoft Visual Basic IDE. Again, we click Developer Tab->Code Group->Macros Button. In the pop up window, we click Button Options….Press B with the hold of Shirt. You will see the Shortcut key is assigned as Ctrl+Shift+B.(We can only assign Ctrl+Shift shortcut key, but not Ctrl+Alt). Click OK.

10. Save the workbook as an Excel Add-In. To do this, you can choose the file type to be Excel Add-In(*.xlam) in Save As dialog. Typically, the name is Book1.xlam

11. Click Office Button->Excel Options->Add-Ins Tab, choose Excel Add-ins, click Go. In the pop up Add-Ins Window, check the box Book1, and then click OK.

12. Select a few cells and press Ctrl+Shift+B. The bottom of the range will be changed to Color pink. (It seems the color index 7 is pink, however, that does not related and be the matter)

That’s all! If you have any doubt about the process, please feel free to let me know, I will try my best to make clarification.

Thanks

Ji






Re: Visual Studio Tools for Office Shortcut Key for excel addin

Mohan Kumar Karunakaran

Hi Ji,

That's a wonderful piece of example and I converted it to vb.net code and it works fine. I have also got another post which helped me in converting to vb code.

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1934426&SiteID=1

Also, I have few things to clarify.

1) I am involved in migrating 2000 templates to 2007 templates using VSTO. So there are already "Alt" combination shortcut keys in existing templates. Is there any way to assign "Alt" combination shortcut keys

2) Here we have created a macro enabled addin to assign shortcut keys. So while deployment this macro enabled workbook should also accompany this solution right

3) Is there anyway other than this method to assign shortcut key for excel addin created using VSTO SE and Office 2007.

Sorry for putting forward too many questions.

Thanks a lot for your support till now.

Regards,

Mohan






Re: Visual Studio Tools for Office Shortcut Key for excel addin

Ji Zhou – MSFT

Hi Mohan,

Yesterday, I thought that Ctrl+Alt cannot be assigned to the Macro, because Excel UI only let us to set Ctrl or Ctrl+Shirt combination key. But after some testing, it seems we can assign other kinds of combination key via VBA codes as your first post described: Application.OnKey

All we need to do is modify the step 9:

Step 9:

In Microsoft Visual Basic IDE, double click ThisWorkbook in the project window. Above the editor, there are two dropdown list controls. Choose Workbook in first one, and Open in Second one. Then a Sub named Workbook_Open will be generated by default for us. In that procedure, we can use Application.OnKey to register our combination keys. Codes:

Code Block

Private Sub Workbook_Open()

Application.OnKey "^%b", " CallBuleUl "

End Sub

Then, perform Step 10 and go ahead as usual.

Every time you Open Excel, Book1.xlam will be loaded and register “Ctrl+Alt+b” to be the shortcut key of procedure CallBlueUl.

As to your second question, yes, you should deploy the Book1.xlam Add-In along with the solution.

And the last one, based on my knowledge and research, this is the only way I can think out to assign the shortcut key to buttons created via Ribbon customization.

Thanks

Ji






Re: Visual Studio Tools for Office Shortcut Key for excel addin

Mohan Kumar Karunakaran

Thanks a lot Ji.

Finally one more clarification, Do I need to write procedures separately for all the ribbon functionality as mentioned in Step 8. If so the length of the code will be much more than expected.

Regards,

Mohan






Re: Visual Studio Tools for Office Shortcut Key for excel addin

Ji Zhou – MSFT

Yes. We have to write procedures separately. It cannot be denied that this way makes the code a bit long, which is a disadvantage.

Thanks

Ji






Re: Visual Studio Tools for Office Shortcut Key for excel addin

Mohan Kumar Karunakaran

Hi Ji,

I have created a macro workbook to assign shortcut key. I have included this file also in the setup project. Is there anyway to load this macro workbook whenever the addin is loaded or we need to copy this file to XLStart folder under C:\Program Files\Microsoft Office\Office Ver\XLSTART\ . Can this be done automatically when the addin installation is done.

Thanks,

Mohan






Re: Visual Studio Tools for Office Shortcut Key for excel addin

Ji Zhou – MSFT

Hi Mohan,

Yes, we should deploy the Book1.xlam with our project. But the right location is not what you mentioned. When we performed the step 10, we will notice the default location of the Excel Add In is at: C:\Documents and Settings\username \Application Data\Microsoft\AddIns. So we can use custom action to copy the file to that place. Excel will find the Add In from that place, but by default it is not loaded. So we can use the following codes in our VSTO Add In to install it.

Code Block

foreach (Excel.AddIn addin in app.AddIns)

{

if (addin.Name == "Book1.xlam")

{

MessageBox.Show("found");

addin.Installed = true;

}

}

Thanks

Ji






Re: Visual Studio Tools for Office Shortcut Key for excel addin

Mohan Kumar Karunakaran

Hi Ji,

Thanks for your response and that is what I exactly needed. But I am new to .net and I would like to know how to add a custom action to copy a file. I tried adding a custom folder on file system on target machine. Is this correct If so, how to give the target folder specification on the custom folder properties Also, I would like to know in which part of the VSTO coding we add the above mentioned procedure to activate the addin.

Sorry for troubling you again.

Thanks






Re: Visual Studio Tools for Office Shortcut Key for excel addin

Ji Zhou – MSFT

Hi Mohan,

You are more than welcomed.

To know more detailed information about custom actions, you can refer this link first:

http://msdn2.microsoft.com/en-us/library/d9k65z2d(VS.80).aspx

From the article above, you can see, we can perform .NET codes in custom actions. So I think the System.IO.File.Copy(string source, string dest) will be competent for your objective. You could deploy the Book1.xlam with your project. Then copy it from the installation location to the Add Ins directory. The installation location should be specific by [targetDir] (Same with SetSecurity custom action we already used to grant full trust). The Add Ins directory can be specific by “%userprofile%/Application Data/Microsoft/AddIns”

Thanks

Ji






Re: Visual Studio Tools for Office Shortcut Key for excel addin

Mohan Kumar Karunakaran

Hi Ji,

Thanks again. I have gone through several articles for creating custom actions. But still not clear. I tried executing the below line of code in the ThisAddin_Startup. But no results.

System.IO.File.Copy("[TARGETDIR]\MyExcelShortcutAddin.xlam", "%userprofile%\Application Data\Microsoft\AddIns\MyExcelShortcutAddin.xlam")

I planned to deploy the first version of release today but held up with this shortcut issue.

Also, It would be great if you could tell me where to put the code for file copy and enable the addin.

Thanks,

Mohan






Re: Visual Studio Tools for Office Shortcut Key for excel addin

Ji Zhou – MSFT

Hi Mohan,

Not put these codes in ThisAddin, but in Installer Class in the Custom Action project. So the part of your Installer Class should look like:

http://msdn2.microsoft.com/en-us/library/9cdb5eda(VS.80).aspx

Code Block

Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)

Dim targetDir As String = Me.Context.Parameters.Item("targetDir")

System.IO.File.Copy("targetDir\MyExcelShortcutAddin.xlam", "%userprofile%\Application Data\Microsoft\AddIns\MyExcelShortcutAddin.xlam")

End Sub

And then add the Custom Action project as Custom Action to your setup project. So the codes will be executed after the installing of your Add in. Note that, after adding it as Custom Action, you should set the targetDir parameter. In the Properties window, select the CustomActionData property and type /targetDir="[TARGETDIR]".

If you have read through the second article about VSTO deployment,

http://msdn2.microsoft.com/en-us/library/bb332052.aspx

You should be familiar with Custom Action because we use Custom Action to grant full trust to the assembly. You can refer to the SetSecurity project in the Sample Codes provided with the articles.

Thanks

Ji