“如何使Windows窗体与Microsoft Office通信?”

时间:2019-07-07 03:18:04

标签: c# windows ms-office

我的程序与在适当位置使用热键来更改Microsoft Word,Powerpoint,Excel和Outlook中的文本完美配合。

我唯一的问题是,除非我用鼠标单击它,或者使用Alt和Tab在程序和Microsoft Office程序之间切换,否则该程序将不会执行。

我希望不使用鼠标或使用Alt和Tab。我有一个用于复制热键的键盘挂钩脚本,但是仅当Windows在所有内容中都看到我的程序并且该程序处于活动状态时,该脚本才起作用。

我的程序位于所有程序之上,但是例如转到Microsoft Word时,我的程序不再处于活动状态,并且Microsoft Word现在处于活动状态。我将不得不进入程序以使其活动,方法是用鼠标单击鼠标或使用Alt和Tab键,然后执行热键,然后使用鼠标单击它或使用Alt和Tab键使它返回Microsoft Word。可以使用按下的热键。

有了这个,我将如何使我的程序成为Microsoft Office的共享插件,并在运行时Microsoft Office能够识别我的程序?

2 个答案:

答案 0 :(得分:0)

首先,您可以从其他应用程序自动化Outlook。由于Outlook是单例,因此只能在系统上同时运行一个应用程序实例。因此,您可能会从ROT获得正在运行的实例:

//Get reference to Outlook.Application from the ROT.
oApp =  (Outlook.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application");

如果系统上没有运行Outlook,则可以简单地创建一个新的Outlook。有关更多信息,请参见C# app automates Outlook (CSAutomateOutlook)

答案 1 :(得分:0)

这是我的代码供您查看,以便您了解我在做什么。 抱歉,我尝试将其作为评论,但过长。

//MICROSOFT WORD INTEGRATION
        try
        {
            app = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
            app.ActiveWindow.Selection.Font.Color = (Microsoft.Office.Interop.Word.WdColor)(changeColor.R + 0x100 * changeColor.G + 0x10000 * changeColor.B);
        }
        catch (System.Exception excp)
        {

        }
        //MICROSOFT EXCEL INTEGRATION
        try
        {
            xlApp = (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
            xlBook = xlApp.ActiveWorkbook;
            xlSheet = xlBook.ActiveSheet;
            Microsoft.Office.Interop.Excel.Range rngSelection = xlApp.Selection as Microsoft.Office.Interop.Excel.Range;
            for (var r = 1; r <= rngSelection.Rows.Count; r++)
            {
                for (var c = 1; c <= rngSelection.Columns.Count; c++)
                {
                    rngSelection[r, c].Font.Color = copyProgramText.SelectionColor;
                }
            }

        }
        catch (System.Exception excp)
        {

        }
        //MICROSOFT POWERPOINT INTEGRATION
        try
        {
            pwptApp = (Microsoft.Office.Interop.PowerPoint.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("PowerPoint.Application");
            pwptApp.ActiveWindow.Selection.TextRange.Font.Color.RGB = System.Drawing.ColorTranslator.ToOle(changeColor);
            if (pwptApp.ActiveWindow.Selection.TextRange.Text.Trim() == "") pwptApp.ActiveWindow.Selection.TextRange.Text = " ";
        }
        catch (System.Exception excp)
        {

        }
        //MICROSOFT OUTLOOK INTEGRATION
        try
        {
            Microsoft.Office.Interop.Outlook.Application otlkApp = (Microsoft.Office.Interop.Outlook.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application");
            Microsoft.Office.Interop.Outlook.Explorer otlkExp = otlkApp.ActiveExplorer();
            Microsoft.Office.Interop.Outlook.Selection otlkSel = otlkExp.Selection;
            Microsoft.Office.Interop.Outlook.MailItem otlkMsg = otlkApp.ActiveInspector().CurrentItem as Microsoft.Office.Interop.Outlook.MailItem;
            Microsoft.Office.Interop.Outlook.Inspector insp = otlkMsg.GetInspector;

            Microsoft.Office.Interop.Word.Document otlkDoc = (Microsoft.Office.Interop.Word.Document)insp.WordEditor;

            string sepSel = otlkDoc.Application.Selection.Text;
            byte rColor = changeColor.R;
            byte gColor = changeColor.G;
            byte bColor = changeColor.B;
            if (sepSel.Trim() == "\r") return;
            char cReturn = (char)13;
            if (sepSel == cReturn.ToString()) return;
            if (sepSel != "")
            {
                otlkMsg.HTMLBody = otlkMsg.HTMLBody.Replace(sepSel, "<font style = 'color: rgb(" + rColor.ToString() + ", " + gColor.ToString() + ", " + bColor.ToString() + ")'>" + sepSel + " </font>");
            }
            else
            {
                return;
            }

            prevOutlookContents = otlkMsg.HTMLBody;

        }
        catch (System.Exception excp)
        {

        }