Saturday, October 10, 2009

Office 2007 Server Document

The simplest way to change an associated Visual Studio Tools for Office (VSTO) manifest to a Microsoft Office 2007 document (Word, PowerPoint, Excel).

*************************

using System;
using Microsoft.VisualStudio.Tools.Applications;
//Microsoft.VisualStudio.Tools.Applications.ServerDocument.v9.0.dll

namespace ManifestCustomActions
{
    public static class ChangeManifestActions
    {
        public static void AddDocumentCustomization(string document, Uri manifest)
        {
            RemoveDocumentCustomization(document);
            ServerDocument.AddCustomization(document, manifest);
        }

        public static void RemoveDocumentCustomization(string document)
        {
            if (DocumentIsCustomized(document))
            {
                ServerDocument.RemoveCustomization(document);
            }
        }

        public static bool DocumentIsCustomized(string document)
        {
            if (ServerDocument.IsCustomized(document))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

ClickOnce Install Actions

The following code class is designed to provide the ability to execute installing a Visual Studio Tools for Office (VSTO) into the ClickOnce cache.

This code would be desired for use by an MSI based installer or a utility tool.

*************************************************

using System;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
using System.Security;
using System.Security.Permissions;
using System.Text;

namespace ClickOnceCustomActions
{
    public static class ClickOnceActions
    {
        public static bool CheckForPermission()
        {
            bool bRtn = false;

            try
            {
                SecurityPermission permission =
                    new SecurityPermission(PermissionState.Unrestricted);
                permission.Demand();

                bRtn = true;
            }
            catch (SecurityException)
            {
                throw new InstallException(
                    "You have insufficient privileges to " +
                    "install the add-in into the ClickOnce cache. " +
                    "Please contact your system administrator.");
            }

            return bRtn;
        }

        public static void InstallVSTO(string fileAbsolutePath)
        {
            string arguments = String.Format(
                        "/S /I \"{0}\"", fileAbsolutePath);

            int exitCode = ExecuteVSTOInstaller(arguments);
            if (exitCode != 0)
            {
                string message = null;
                switch (exitCode)
                {
                    case -300:
                        message = String.Format(
                            "The Visual Studio Tools for Office solution " +
                            "was signed by an untrusted publisher and as " +
                            "such cannot be installed automatically. " +
                            "Please use your browser to navigate to {0} in " +
                            "order to install the solution manually. " +
                            "You will be prompted if the solution is trusted for execution.",
                            fileAbsolutePath);
                        break;

                    default:
                        message = String.Format(
                            "The installation of the ClickOnce solution failed with exit code {0}",
                            exitCode);
                        break;
                }
                throw new InstallException(message);
            }
        }

        public static void UninstallVSTO(string fileAbsolutePath)
        {
            string arguments = String.Format(
                "/S /U \"{0}\"", fileAbsolutePath);

            ExecuteVSTOInstaller(arguments);
        }

        public static int ExecuteVSTOInstaller(string arguments)
        {
            string basePath = Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles);
            string subPath = @"Microsoft Shared\VSTO\9.0\VSTOInstaller.exe";
            string vstoInstallerPath = Path.Combine(basePath, subPath);

            if (File.Exists(vstoInstallerPath) == false)
            {
                throw new InstallException(
                    "The Visual Studio Tools for Office installer was not found.");
            }

            ProcessStartInfo startInfo = new ProcessStartInfo(vstoInstallerPath);
            startInfo.Arguments = arguments;

            Process process = Process.Start(startInfo);
            process.WaitForExit();

            int exitCode = process.ExitCode;
            return exitCode;
        }
    }
}

Friday, October 9, 2009

InfoPath On a Windows form

In order to load InfoPath form code onto a windows form control, a special control is needed.

The Windows Form control must be displayed prior to loading the code.

NewFromFormTemplate – Use this function to create InfoPath XML.
Open – Use this function to load existing InfoPath XML.

*******************************************************

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

//%Program Files%\Microsoft Office\Office12\Microsoft.Office.InfoPath.FormControl.dll
using Microsoft.Office.InfoPath;

namespace MyAppNS
{
    public class InfoPathCustomActionPane
    {
        FormControl frmCtrl = new FormControl();
        Form frmDisp = new Form();

        public InfoPathCustomActionPane()
        {
            frmDisp.Size = new Size(600, 400);
            frmDisp.Controls.Add(frmCtrl);

            frmCtrl.Dock = DockStyle.Fill;
        }

        public void DisplayNewInfoPathForm()
        {
            frmDisp.Show();
            frmCtrl.NewFromFormTemplate(@"C:\temp\MyInfoPathForm.xsn");
        }

        public void DisplayExistingInfoPathForm(string xml)
        {
            MemoryStream msXml = new MemoryStream();
            StreamWriter sw = new StreamWriter(msXml);
            sw.Write(xml);
            sw.Flush();
            msXml.Position = 0;

            frmDisp.Show();
            frmCtrl.Open(msXml);
        }
    }
}