Hi All,
I am trying to create a function that would retrieve all Macros and the short cut keys (or Hot Keys) assigned to the Macros using C# with VSTO 2005. Using VBScript is working fine:
Private Sub Test()
For Each objKey In KeyBindings
With objKey
If .KeyCategory = wdKeyCategoryMacro Then
sCode = objKey.KeyCode
sShortcut = KeyString(KeyCode:=sCode)
sCommand = .Command
MsgBox "sCommand = " & sCommand
End If
End With
Next
End Sub
Not sure if there is a way to convert the above code to C#
I have tried the following:
ApplicationClass objApp = new ApplicationClass();
String iKeyCount = objApp.KeyBindings.Count;
MessageBox.Show(iKeyCount.ToString());
If is there a way to retrieve each key from the KeyBindings
Thanks in advance.

KeyBindings
ArtapuS
Hi,
It does not work. You are right it returns only the normal.dot template. The Templates.get_Item(ref oTemplate) is looking for an index for oTemplate. I changed oTemplate=1 and it worked for the normal.dot.
I created a couple of hot keys on MyTemplate.dot; But, when I use F5 or Ctrl+F5 to run the project, it does not show the hotkeys on the document1.
Thanks!
Abbasi
That's exactly what I tested. Without a @ or "\\", it cannot be compiled; and the error message was from the catch block. Maybe we are using different versions, I use VS / VSTO 2005 and Word 2003. How about you
ThomasJaeger
Hi Norm,
It is great, I created a Word Template project and compiled the code (you suggested) without error, but it did not return any KeyString either.
Maybe I need to provide the path of the template; in VBA, I can do something like:
CustomizationContext = Templates("C:\MyTemplate.dot")
iKeyCount = KeyBindings.Count
If iKeyCount > 0 then
For Each ...
...
End if
How do you use CustomizationContext to assign the template path in C#
Many Thanks,
azeezi
Hello,
How about something like:
private void Test()
{
int sCode = 0;
string sShortcut, sCommand = "";
try
{
foreach (System.Object objKey in Application.KeyBindings)
{
Microsoft.Office.Interop.Word.KeyBinding keyBinding = objKey as Microsoft.Office.Interop.Word.KeyBinding;
if (keyBinding.KeyCategory == Word.WdKeyCategory.wdKeyCategoryMacro)
{
sCode = keyBinding.KeyCode;
sShortcut = keyBinding.KeyString;
sCommand = keyBinding.Command;
MessageBox.Show("sCommand = " + sCommand);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Rafer
"Index" can also be a string containing the full path information. So, for example:
object oTmplPath = "C:\path\mytemplate.dot";
Word.Template tmpl = (Word.Template) ThisApplication.Templates.get_Item(ref oTmplPath);
G. Dean Blakely
Yes, of course I tested it.
But I didn't copy my exact code. Try putting a @ before the path string, or double the back slashes... And be sure To put your code in Try. . .Catch blocks so you get more informative error messages.
DaveC426913
Hello,
Perhaps this will work:
object
oTemplate = "C:\\MyTemplate.dot";Application.CustomizationContext = Application.Templates.get_Item(ref oTemplate);
Also, I think the Templates property only returns global templates and templates attached to open documents, but I am not totally sure on that. Here is a link to the VBA reference doc for the Templates property.
Please let me know if that works. Thanks!
Norm E.
hixxy
Hi David
I'm using the same combination of software...
Hmmm. When are you trying this If it's part of the Startup procedure, and the template you're after is a global template, it's likely it hasn't been loaded yet. I just did a series of tests with a loop like this:
foreach (Word.Template tmplList in ThisApplication.Templates)
{
MessageBox.Show(tmplList.FullName);
if (tmplList.FullName == tmplName)
{
otmpl = tmplList;
break;
}
}
If this it's in ThisDocument_Startup any templates in the Startup folder don't load as Add-ins before this executes. If I place it in the Click event of a commandbar button and run it at a later time, the global templates are there, and the following works
string tmplName = @"C:\Test\GlobalTempltae.dot";
if (!System.IO.File.Exists(tmplName))
{
MessageBox.Show("Can't find template");
return;
}
object otmplName = tmplName;
Word.Template otmpl = null;
otmpl = ThisApplication.Templates.get_Item(ref otmplName);
So, if you need to do this when starting up, you need to explicitly load the global template before you can query it. For example
string tmplName = @"C:\Test\GlobalTempltae.dot";
object otmplName = tmplName;
Word.AddIn adin = ThisApplication.AddIns.Add(tmplName, ref oTrue);
Word.Template tmpl = (Word.Template) ThisApplication.Templates.get_Item(ref otmplName);
ThisApplication.CustomizationContext = tmpl;
MessageBox.Show(String.Format("{0}", ThisApplication.KeyBindings.Count));
Andy Ho
Hi Cindy,
Thanks for the information. I will test and let you know.
David
Rodrigo B. de Oliveira
I got the "COMException was unhandled by user code" error message when using the "full path" for the "index". Not sure if you test it