【发布时间】:2020-07-10 19:07:20
【问题描述】:
我正在考虑将我们所有的 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() 事件中添加更多代码(显示对话框、选择客户、选择语言和其他信息)。为什么当我使用调试器跳过命令 ...TypeText()... 时文本没有插入到 word 文档中?!?
而是在调试器离开ThisDocument_New()时插入?
当 ThisDocument_New() 不允许正确调试时,我遗漏了什么以及将我的代码放在哪里?
【问题讨论】: