Excel VSTO中的复制粘贴正在删除更改事件而事件没有执行

问题描述 投票:0回答:1

如果我将粘贴内容从A2单元复制到B2单元,则更改事件会很好,但是当我执行剪切和粘贴操作时,它不执行更改事件,因此我认为在剪切和粘贴过程中事件已被删除。以下是VSTO代码。

[我尝试过其他事件,例如取消选择或选择更改,但它们也不适用于从A2单元到B2单元的数据剪切和粘贴。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using System.Windows.Forms;
using ExcelVSTO.Service;
namespace ExcelVSTO.UI
{
    public partial class ThisAddIn
    {
        #region "Private Variables"
        Worksheet controlWorksheet;       
        Microsoft.Office.Tools.Excel.NamedRange cellFrom;
        Microsoft.Office.Tools.Excel.NamedRange cellTo;
        string cellFromValue;
        #endregion  

        #region "Events"
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {

            controlWorksheet = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet);

            if (controlWorksheet != null)
            {
                cellFrom = controlWorksheet.Controls.AddNamedRange(controlWorksheet.Range["A2"], "cellFrom");

                cellTo = controlWorksheet.Controls.AddNamedRange(controlWorksheet.Range["B2"], "cellTo");

                cellFrom.Value2 = "FromCell";

                cellTo.Value2 = "ToCell";

                cellFrom.Change += new Microsoft.Office.Interop.Excel.DocEvents_ChangeEventHandler(changesRange_ChangeFrom);

                cellTo.Change += new Microsoft.Office.Interop.Excel.DocEvents_ChangeEventHandler(changesRange_ChangeTo);


            }

        }

        void changesRange_ChangeFrom(Excel.Range Target)
        {
            string cellAddress = Target.get_Address(Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1);
            cellFromValue = Target.Value2 == null ? string.Empty : Target.Value2.ToString();
        }
        void changesRange_ChangeTo(Excel.Range Target)
        {
            string cellAddress = Target.get_Address(Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1);
            string cellFromNewVal = cellFrom.Value2 == null ? string.Empty : cellFrom.Value2.ToString();
            string cellToNewVal = Target.Value2 == null ? string.Empty : Target.Value2.ToString();
            //if (string.IsNullOrEmpty(cellFromValue) && cellToNewVal == cellFromValue)
            if (cellToNewVal == cellFromNewVal)
            {
                MessageBox.Show(cellToNewVal + " data is moved to cell " + cellAddress);
                //Send data to API//
                RestAPIService api = new RestAPIService();
                var response = api.SendAlertToApi(cellAddress);
                MessageBox.Show("Api Response - " + response.Status.ToString());
            }            
        }      
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {

        }
        #endregion

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}
c# excel vsto
1个回答
0
投票

Blockquote我认为在剪切和粘贴期间事件已被删除。以下是VSTO代码。

您是否已通过设置断点并在复制粘贴后手动编辑单元格来确认这一点?

想到的一个选项是尝试捕获某个范围或整个工作表上的“粘贴”事件,但由于该事件似乎不存在,我发现以下线程可能会指导您找到解决方案Excel VBA How to detect if something was pasted in a Worksheet

© www.soinside.com 2019 - 2024. All rights reserved.