如何在ThisDocument_New()中查看Word文档文本更改



我正在考虑将我们所有的Word模板从VBA迁移到VSTO,并有以下问题:如何调试VSTO项目中的代码?

与VBA中的调试不同,在逐步执行过程时,我看不到代码逐行执行的结果。

例如,我在VS 2019中构建了一个原型Word文档:

using ...;
namespace MyCompany.OfficeTemplates.MyTemplate
{
public partial class ThisDocument
{
private void ThisDocument_Startup(object sender, System.EventArgs e)
{
}
private void ThisDocument_Shutdown(object sender, System.EventArgs e)
{
}
private void ThisDocument_New()
{
var currentSelection = Application.Selection;
currentSelection.TypeText("This text was added by using code.");
}
#region VSTO Designer 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(ThisDocument_Startup);
this.Shutdown += new System.EventHandler(ThisDocument_Shutdown);
this.New += new Microsoft.Office.Interop.Word.DocumentEvents2_NewEventHandler(this.ThisDocument_New);
}
#endregion
}
}

我计划在ThisDocument_New()事件中添加更多的代码(显示对话框、选择客户、选择语言和其他信息(。为什么当我跨过命令时文本没有插入word文档。。。TypeText((。。。使用调试器?!?

相反,它是在调试器离开ThisDocument_New()时插入的?

当ThisDocument_New((不允许propper调试时,我缺少什么以及将代码放在哪里?

在代码执行过程中看不到发生了什么是VSTO的典型情况。调试与VBA不同-只有在代码完成后,结果才可见。没有什么可以改变这一点——这是事情的本质(.NET<->COM交互(。

当我开始使用VSTO时,我自己也很难解决这个问题,必须学会如何解决它

一般来说,如果在故障排除过程中需要查看发生了什么,我首先在VBA中进行测试,然后将代码转移到VSTO。

如果有必要在代码运行时检查代码生成的值,我会在其中粘贴一些Debug.Print行,以便在遍历代码时在Visual Studio输出窗口中遵循这些行。

在遍历代码时,还可以跟踪分配给对象的内容。

最新更新