Embed office applications to your application through API or DsoFramer

Costas

Administrator
Staff member
this one will open Office Word and load the given filepath

JavaScript:
public void openWordDocument(string filePath)
{
    object missing = System.Reflection.Missing.Value;

    //create a document in this path  
    object fileName = filePath;

    object wordApp = Activator.CreateInstance(Type.GetTypeFromProgID("Word.Application"));
    //Setup our Word.Document  
    object wordDoc = wordApp.GetType().InvokeMember("Documents", System.Reflection.BindingFlags.GetProperty, null, wordApp, null);
    //Set Word to be not visible  
    wordApp.GetType().InvokeMember("Visible", System.Reflection.BindingFlags.SetProperty, null, wordApp, new object[] { true });

    //Open the word document fileName  
    object activeDoc = wordDoc.GetType().InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, wordDoc, new Object[] { fileName });
}

what about to embed the word to your C# form ?

there are two ways
1) host Office Word window to your application with the API help
2004 - https://www.codeproject.com/Articles/9123/Hosting-EXE-Applications-in-a-WinForm-project
2013 - https://www.codeproject.com/Tips/673701/Hosting-EXE-Applications-in-a-WPF-Window-Applicati

2) use of Microsoft DSOframer interop, discontinued on 2008
JavaScript:
// src - https://github.com/ysjr-2002/DsoFramer
// files used (1.3.0.0) :
// AxInterop.DSOFramer.dll
// Interop.DSOFramer.dll
//
// can be found on Microsoft Developer Support Office Framer Control Sample (KB 311765) - DsoFramer_KB311765_x86.exe
//
//DsoOffice : UserControl

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

namespace WindowsFormsApplication1
{
    public partial class DsoOffice : UserControl
    {
        private AxDSOFramer.AxFramerControl dso = new AxDSOFramer.AxFramerControl();

        public DsoOffice()
        {
            InitializeComponent();

            ((System.ComponentModel.ISupportInitialize)(this.dso)).BeginInit();
            this.Controls.Add(this.dso);
            ((System.ComponentModel.ISupportInitialize)(this.dso)).EndInit();
            dso.Dock = DockStyle.Fill;

            dso.Titlebar = false;
            dso.Menubar = false;
            dso.Toolbars = true;
            dso.set_EnableFileCommand(DSOFramer.dsoFileCommandType.dsoFileSave, false);
            dso.set_EnableFileCommand(DSOFramer.dsoFileCommandType.dsoFileSaveAs, false);
            dso.BackColor = Color.Black;
        }

        private string GetFileType(string fileExtension)
        {
            try
            {
                string sOpenType = "";
                switch (fileExtension.ToLower())
                {
                    case "xls":
                    case "xlsx":
                    case "xlsm":
                    case "xlsb":
                    case "csv":
                        sOpenType = "Excel.Sheet";
                        break;
                    case "doc":
                    case "docx":
                    case "docm":
                    case "rtf":
                        sOpenType = "Word.Document";
                        break;
                    case "ppt":
                    case "pptx":
                    case "pptm":
                        sOpenType = "PowerPoint.Show";
                        break;
                    case "vsd":
                    case "vdx":
                        sOpenType = "Visio.Drawing";
                        break;
                    default:
                        sOpenType = "Word.Document";
                        break;
                }
                return sOpenType;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void OpenDocument(string filepath)
        {
            string sExt = System.IO.Path.GetExtension(filepath).Replace(".", "");
            dso.Open(filepath, false, GetFileType(sExt), "", "");
        }

        public void SaveDocument()
        {
            try
            {
                this.dso.Save(true, true, null, null);
            }
            catch (Exception ex)
            {
            }
        }
    }
}


//form class, add the usercontrol to the form and the load/save code will be as 

        //load
        private void button1_Click(object sender, EventArgs e)
        {
            var ofd = new System.Windows.Forms.OpenFileDialog();
            ofd.Filter = "*.doc|*.doc|*.docx|*.docx|*.xls|*.xls|*.xlsx|*.xlsx";
            ofd.Multiselect = false;
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                var filename = ofd.FileName;
                if (dsoOffice1 == null)
                {
                    MessageBox.Show("Office is not installed");
                    return;
                }
                dsoOffice1.OpenDocument(filename);
            }

        }

        //save
        private void button2_Click(object sender, EventArgs e)
        {
            dsoOffice1.SaveDocument();
        }

//there are some events, as describe on the samples exist into DsoFramer_KB311765_x86.exe
//VB.NET - https://github.com/hueidou/DsoFramer
 
Top