使用Word.Interop创建嵌套字段 [英] Creating a nested field with Word.Interop

查看:100
本文介绍了使用Word.Interop创建嵌套字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我正在使用VSTO,更确切地说是使用C#和"Microsoft Word"应用程序加载项.我确实想以编程方式创建嵌套字段.我提供了以下源代码(出于测试目的):

I'm playing around with VSTO, more precisely with C# and a "Microsoft Word" application add-in, at the moment. I do want to programmatically create nested fields. I've come up with the following source code (for testing purposes):

public partial class ThisAddIn
{
    private void ThisAddIn_Startup(object sender, EventArgs e)
    {
        // TODO This is just a test.
        this.AddDocPropertyFieldWithinInsertTextField("Author", ".\\\\FileName.docx");
    }

    private void AddDocPropertyFieldWithinInsertTextField(string propertyName, string filePath)
    {
        // TODO Horrible, since we rely on the UI state.
        this.Application.ActiveWindow.View.ShowFieldCodes = true;

        Word.Selection currentSelection = this.Application.ActiveWindow.Selection;

        // Add a new DocProperty field at the current selection.
        currentSelection.Fields.Add(
            Range: currentSelection.Range,
            Type: Word.WdFieldType.wdFieldDocProperty,
            Text: propertyName,
            PreserveFormatting: false
        );

        // TODO The following fails if a DocProperty with the specified name does not exist.

        // Select the previously inserted field.
        // TODO This is horrible!
        currentSelection.MoveLeft(
            Unit: Word.WdUnits.wdWord,
            Count: 1,
            Extend: Word.WdMovementType.wdExtend
        );

        // Create a new (empty) field AROUND the DocProperty field.
        // After that, the DocProperty field is nested INSIDE the empty field.
        // TODO Horrible again!
        currentSelection.Fields.Add(
            currentSelection.Range,
            Word.WdFieldType.wdFieldEmpty,
            PreserveFormatting: false
        );

        // Insert text BEFORE the inner field.
        // TODO Horror continues.
        currentSelection.InsertAfter("INCLUDETEXT \"");

        // Move the selection AFTER the inner field.
        // TODO See above.
        currentSelection.MoveRight(
            Unit: Word.WdUnits.wdWord,
            Count: 1,
            Extend: Word.WdMovementType.wdExtend
        );

        // Insert text AFTER the nested field.
        // TODO See above.
        currentSelection.InsertAfter("\\\\" + filePath + "\"");

        // TODO See above.
        this.Application.ActiveWindow.View.ShowFieldCodes = false;

        // Update the fields.
        currentSelection.Fields.Update();
    }

尽管提供的代码满足了要求,但它有一些主要问题(如您所看到的是否阅读了一些代码注释),例如:

Although the provided code covers the requirements, it has some major problems (as you can see if reading some of the code comments), e.g.:

  1. 它不可靠,因为它依赖于应用程序的UI状态.
  2. 是冗长的.任务不是那么复杂,imo.
  3. 这不容易理解(重构可能会有所帮助,但是通过解决问题1.和2.,这种缺陷应该消失了.)

我在WWW上做了一些研究,但是还没有找到一个干净的解决方案.

I did some research on the WWW, but haven't been able to find a clean solution, yet.

所以,我的问题是:

  • 有人可以提供(替代的)C#源代码,比我的健壮更强大,并且允许我将嵌套字段添加到"Microsoft Word"文档中吗?
  • Can someone provide (alternative) C# source code, that is more robust than mine and that allows me to add nested fields to a "Microsoft Word" document?

推荐答案

我创建了一个通用实现,以使用VSTO for Microsoft Word创建非嵌套字段和嵌套字段.您可以在要点中查看相关的源代码.我已经从文章 VBA中的嵌套字段移植了VBA源代码.到C#并应用了一些改进.

I've created a generic implementation to create both non-nested and nested fields with VSTO for Microsoft Word. You can see the relevant source code in this Gist. I've ported the VBA source code from the article Nested Fields in VBA to C# and applied some improvements.

仍然不够完美(需要一些其他错误处理),但比依赖于用户界面状态的带有Selection对象的解决方案要好得多!

Still not perfect (needs some additional error handling), but far better than a solution with a Selection object, which relies on the state of the user interface!

这篇关于使用Word.Interop创建嵌套字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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