Start program from word

A

Ably+

I would like to add a button in word, programatically by Using Visual C#
..NET, and start another program.

Can you point me to specific information? If possible real code samples.

Thanks
 
H

Helmut Obertanner

Create a AddIN for Word and here is your complete code....
It adds a Button to the Standard Toolbar named X4U Button.

When you click, calc is executed.
Hope this helps, here comes the Connect.cs
Take car that you change the GUID and Name of your Addion to that you have
created with Visual Studio Wizard.

------------------------------------------------------------------------------------------
namespace WordExtension
{
using System;
using Office = Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using Word = Microsoft.Office.Interop.Word;


#region Read me for Add-in installation and setup information.
// When run, the Add-in wizard prepared the registry for the Add-in.
// At a later time, if the Add-in becomes unavailable for reasons such as:
// 1) You moved this project to a computer other than which is was
originally created on.
// 2) You chose 'Yes' when presented with a message asking if you wish to
remove the Add-in.
// 3) Registry corruption.
// you will need to re-register the Add-in by building the MyAddin21Setup
project
// by right clicking the project in the Solution Explorer, then choosing
install.
#endregion

/// <summary>
/// The object for implementing an Add-in.
/// </summary>
/// <seealso class='IDTExtensibility2' />
[GuidAttribute("03C1806D-03BC-47A2-A5A5-B42931C77DDB"),
ProgId("WordExtension.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{

Office.CommandBarButton _ExeButton = null;
Word.ApplicationClass _Application = null;
object addInInstance;

/// <summary>
/// Implements the constructor for the Add-in object.
/// Place your initialization code within this method.
/// </summary>
public Connect()
{
}

/// <summary>
/// Implements the OnConnection method of the IDTExtensibility2
interface.
/// Receives notification that the Add-in is being loaded.
/// </summary>
/// <param term='application'>
/// Root object of the host application.
/// </param>
/// <param term='connectMode'>
/// Describes how the Add-in is being loaded.
/// </param>
/// <param term='addInInst'>
/// Object representing this Add-in.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnConnection(object application, Extensibility.ext_ConnectMode
connectMode, object addInInst, ref System.Array custom)
{
_Application = (Word.ApplicationClass ) application;
addInInstance = addInInst;
CreateMenuButton();
}

/// <summary>
/// Implements the OnDisconnection method of the IDTExtensibility2
interface.
/// Receives notification that the Add-in is being unloaded.
/// </summary>
/// <param term='disconnectMode'>
/// Describes how the Add-in is being unloaded.
/// </param>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
DeleteMenuButton();
Marshal.ReleaseComObject (_Application);
}

/// <summary>
/// Implements the OnAddInsUpdate method of the IDTExtensibility2
interface.
/// Receives notification that the collection of Add-ins has changed.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnAddInsUpdate(ref System.Array custom)
{
}

/// <summary>
/// Implements the OnStartupComplete method of the IDTExtensibility2
interface.
/// Receives notification that the host application has completed
loading.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnStartupComplete(ref System.Array custom)
{
}

/// <summary>
/// Implements the OnBeginShutdown method of the IDTExtensibility2
interface.
/// Receives notification that the host application is being unloaded.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnBeginShutdown(ref System.Array custom)
{
}





// Create CommandButtons here
private void CreateMenuButton()
{
if (_ExeButton == null)
{
object missing = System.Reflection.Missing.Value ;
Office.CommandBar commandBar = _Application.CommandBars["Standard"];
_ExeButton = (Office.CommandBarButton) commandBar.FindControl (missing,
missing, "X4U", missing, true);
if (_ExeButton == null)
{
_ExeButton = (Office.CommandBarButton) commandBar.Controls.Add (
Office.MsoControlType.msoControlButton , missing , missing, missing, true );
}
_ExeButton.Tag = "X4U";
_ExeButton.Style = Office.MsoButtonStyle.msoButtonIconAndCaption ;
_ExeButton.FaceId = 22;
_ExeButton.Caption = "X4U Button";
_ExeButton.Visible = true;

_ExeButton.Click +=new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(_ExeButton_Click);

Marshal.ReleaseComObject (commandBar);
}
}

// Remove CommandButtons here
private void DeleteMenuButton()
{
if (_ExeButton != null)
{
_ExeButton.Visible = false;
_ExeButton.Click -= new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(_ExeButton_Click);
_ExeButton.Delete (false);
Marshal.ReleaseComObject (_ExeButton);
}
}

private void _ExeButton_Click(Microsoft.Office.Core.CommandBarButton Ctrl,
ref bool CancelDefault)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "Calc.exe";
process.Start();

}
}
}
----------------------------------------------------------------------------------------

regards
--
Helmut Obertanner
Technical Consultant
Softwaredevelopment
DATALOG Software AG | Zschokkestr. 36 | D-80687 Munich

.... and IT works!
 
P

Paul Qualls

Ably+ said:
I would like to add a button in word, programatically by Using Visual C#
.NET, and start another program.

Can you point me to specific information? If possible real code samples.

Funny that you post this. I have a free tool that does this, and has been
availible for free for several years. It is at
http://paulqualls.com/FormLaunch.html
it is an exchange client addin called "Hyper Tweak'd Custom Form Launcher".
It not only launches Custom forms, but will launch any program too even with
parameters.

I am almost through writing a new version that is written in C# that is a
very similar program, but much more flexible. It will be released next
month as "Launcher Pro".

In the mean time, the FormLauncher program mentioned above might suit you
for the time being without having to write any code.

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top