Manipulate Visual Studio from Excel or Word

You can use automation to manipulate various applications, like Word, Excel, Visual Studio, Visual FoxPro
Automation can make many tasks easier, including testing.
You can imagine something like a Word macro that launches Excel to create a concordance of the words in the current Document.

You can explore the Excel “Developer” menu by enabling it:
File->Options->Customize Ribbon->Check “Developer”

Let’s insert a macro:
Start Excel, choose new document, hit Alt-F11, then Menu->Insert->Module
Paste in this code:

Sub t()
Set ox = CreateObject("Word.application")
ox.Visible = 1
ox.Selection = "Text inserted from Excel"
End Sub

Now hit F5 to run the macro

You can turn on macro recording and execute a few steps to see what kind of commands are available.

Similarly, you can start Word to create a new document, hit Alt-F11, etc.

You may ask yourself How does this work?

If you start RegEdit you can see the kinds of things you can create (Called Com Servers. You can easily create your own in FoxPro or )


Using Visual Foxpro (which also has an object model and is likewise automatable),
ox = CREATEOBJECT("VisualStudio.Solution")


Try the same code in Excel or Word. (Excel & Word use VBA, which requires the “Set” command when assigning an object to a variable:
Sub t()
    Set ox = CreateObject("VisualStudio.Solution")
    Set dte = ox.dte
    dte.Application.MainWindow.Visible = 1
    dte.Solution.Open ("C:\Users\CalvinH\Source\Repos\Bounce\Bounce\Bounce.sln")
End Sub

FoxPro intellisense is nice here because it shows the available members/properties and it shows help information in a tooltip
How did I know this? I wrote the code for FoxPro intellisense.

It used to be I could create “VisualStudio.Application”, but thtat doesn’t seem to work any moer

Does this work from other office applications like Outlook and PowerPoint? Try it and see!

See also:
Word Object Model:
Excel Object Model:
Visual Studio Object Model:

Comments (0)

Skip to main content