开发者

Word 2007 + Use commands via macro?

I was wondering if it's possible to fire Word commands via vba macros? What I'm essentually trying to do is to make it easier for users to open the New document or template window that you can find via New --> Document --> My templates

I see that I can add a custom tab, alte开发者_开发知识库ast in Word 2010. But I've already got a custom tab I've added using Custom UI Editor for Microsoft Office. But it doesn't show up when trying to customize the Ribbon. I also tried to record a macro and just open the window where you choose which template you want to open. But it didn't record the action that makes the window actually appear.

Any ideas?


Yes this is possible, although the Backstage view in Word 2010 makes this a little tricky. Basically, Word comes with many built-in macros that control its commands and that are accessible via VBA. These commands can be found by displaying the Developer Tab, clicking the Macros button, and changing the Macros in: menu of the Macros dialog box to display Word commands:

Word 2007 + Use commands via macro?

To access the My templates dialog box via VBA, use this code:

Application.Run MacroName:="FileNewDialog"
SendKeys "%NM"

FileNewDialog accesses the existing Word macro for displaying Backstage view and Sendkeys uses the Alt-key keystrokes to maneuver and open the My templates dialog box (see the Word 2010 VBA help entry for more on SendKeys). Place the code into a Sub and create a QAT or Ribbon button to execute and you should be good to go.

Important note about testing SendKeys: SendKeys cannot be tested by stepping through the code in the VBA IDE as the keystrokes it sends out when running are sent to the active window at the time of execution (which would be the VBA development window). To test SendKeys, save the macro and attach it to a QAT button, which can be depressed, as needed, for testing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜