在Word中嵌入Excel工作表并在Excel共享插件中附加事件 [英] Embeded Excel Sheet in Word and Attaching events in Excel Shared Addins

查看:95
本文介绍了在Word中嵌入Excel工作表并在Excel共享插件中附加事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有另外一个OLE问题:

I have another problem with OLE:

我们有一个用C#编写的共享Excel Addin

We have a shared Excel Addin written in C#

简化它看起来像这样:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Jedox.Palo.Comm;
using Jedox.Palo.XlAddin.Forms;
using Jedox.Palo.XlAddin.Utils;

namespace Jedox.Palo.XlAddin
{
    [ComVisible(true)]
    [GuidAttribute("12B22828-5CDD-4AED-B888-4AE8B84ED7B3"), ProgId("Jedox.Palo.XlAddin.Connect")]
    public class Connect : Extensibility.IDTExtensibility2, IDisposable
    {

        private Excel.Application eapp = null;

        public void OnConnection(object Application, Extensibility.ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
        {
            if (!ConnectHelper.isActive)
            {
                return;
            }

            try
            {
                ConnectHelper.setEnUSCulture();

                this.eapp = Application as Excel.Application;
                ExcelHelper.RegisterExcel(eapp, AddInInst);
		// ...
        }

        private void AttachEventHandlers(Excel.Application eApp)
        {
            eApp.SheetBeforeDoubleClick += new Excel.AppEvents_SheetBeforeDoubleClickEventHandler(eApp_SheetBeforeDoubleClick);
            eApp.SheetBeforeRightClick += new Excel.AppEvents_SheetBeforeRightClickEventHandler(eApp_SheetBeforeRightClick);
	    //...
        }

	public void OnStartupComplete(ref Array custom)
        {
            if (!ConnectHelper.isActive)
            {
                return;
            }

            try
            {
                ConnectHelper.setEnUSCulture();
		// ...
		AttachEventHandlers(eapp);
            }
	// ....
    }
}

现在我有一个奇怪的问题。

Now I have a strange issue.

如果我在Word中加载带有嵌入Excel表格的文档,然后在这个嵌入的表格上双击

If I load in Word a Document with an embeded Excel sheet and then doubleclick on this embedded sheet

onbeginshutdown 方法是呼叫后

the onbeginshutdown method is called after

            eApp.SheetBeforeDoubleClick + =

            eApp.SheetBeforeDoubleClick +=

之前

            ; eApp.SheetBeforeRightClick + =

            eApp.SheetBeforeRightClick +=

为什么会这样,我该怎么办才能避免这种情况?

Why is this so and what can I do to avoid this ?

最好的问候

 亨德里克

推荐答案

您好,

我们有一个用#C 4.0编写的Excel共享插件。

we have an Excel shared Addin written in #C 4.0.

要检测我们是独立的还是在OLE操作中,我们使用以下代码

To detect whether we are standalone or in OLE action we use following code

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Management;
using System.Threading;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Jedox.Palo.Comm;
using Jedox.Controls;

namespace Jedox.Palo.XlAddin.Utils
{
    internal static class ConnectHelper
    {
        private static bool? _standalone = null;
        internal static bool standalone
        {
            get
            {
                return _standalone ?? false;
            }
        }

        internal static void SetStandalone()
        {
            if (!_standalone.HasValue)
            {
                _standalone = GotStartedInsideExcel();
            }
        }


        private static string ParentProcessUseWmi(int pID)
        {
            string parent_process = "";
            try
            {
                using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ProcessID=" + pID))
                {
                    using (ManagementObjectCollection moc = searcher.Get())
                    {
                        dynamic ppid = null;
                        foreach (ManagementObject mo in moc)
                        {
                            ppid = mo["ParentProcessID"];
                            if (ppid != null)
                            {
                                parent_process = Process.GetProcessById(Convert.ToInt32(ppid)).ProcessName;
                                break;
                            }
                        }
                    }
                }
            }
            catch { }
            return parent_process;
        }

        private static bool GotStartedInsideExcel()
        {
            return ParentProcessUseWmi(Process.GetCurrentProcess().Id) != "svchost";
        }

    }
}

这几乎是Tim Lin的代码

This is almost the code from Tim Lin in

http://social.msdn.microsoft.com/Forums/office/en-US/019bacf6-99a9-4361-a274 -f72febbcd04e / how-to-stop-an-addin-loading-if-powerpointexcelword-is-being-automated-embedded?forum = vsto

现在我们简化了连接代码就像

Now our simplified Connect code is like

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Jedox.Palo.Comm;
using Jedox.Palo.XlAddin.Forms;
using Jedox.Palo.XlAddin.Utils;

namespace Jedox.Palo.XlAddin
{
    [ComVisible(true)]
    [GuidAttribute("12B22828-5CDD-4AED-B888-4AE8B84ED7B3"), ProgId("Jedox.Palo.XlAddin.Connect")]
    public class Connect : Extensibility.IDTExtensibility2, IDisposable
    {

        private Excel.Application eapp = null;

        public void OnConnection(object Application, Extensibility.ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
        {
            if (!ConnectHelper.isActive)
            {
                return;
            }

            try
            {
                ConnectHelper.setEnUSCulture();

                this.eapp = Application as Excel.Application;
                ExcelHelper.RegisterExcel(eapp, AddInInst);
		// ...
        }

	public void OnStartupComplete(ref Array custom)
        {

            try
            {
                ConnectHelper.setEnUSCulture();
		// ...
		ConnectHelper.SetStandalone();
            }
	// ....
    }
}

现在我打开一个Word文档用嵌入的Excel工作表然后双击嵌入的Excel工作表,

Now when I open a Word document wit an embedded Excel sheet and then doubleclick on the embedded Excel sheet,

然后调用OnBeginShutdown方法,当

then the OnBeginShutdown method is called, when

ConnectHelper.SetStandalone();

ConnectHelper.SetStandalone();

已执行。

为什么会发生这种情况?如何避免这种情况?

Why does this happens and how can I avoid this ?

致以最诚挚的问候

  Hendrik Schmieder

  Hendrik Schmieder


这篇关于在Word中嵌入Excel工作表并在Excel共享插件中附加事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆