【问题标题】:Retrieving Building Blocks from user generated Word document从用户生成的 Word 文档中检索 Building Block
【发布时间】:2014-04-14 08:54:07
【问题描述】:

我已经进行了广泛的搜索,但我还没有找到合适的解决方案。

首先,这是一个使用 .NET 4.0 和 DevExpress 的 WinForms 应用程序。

我一直在尝试从用户上传到我的应用程序的 Word 文档 (.docx) 或至少是用户生成的模板文件 (.dotx) 中检索构建基块(水印、封面等),然后保存到数据库中。

我必须能够检索该文件中使用的所有构建块。

我尝试了很多不同的方法来检索它们,但我只能从 Built-In Building Blocks.dotx 文件中检索它们,该文件位于:

C:\Users\<username>\AppData\Roaming\Microsoft\Document Building Blocks\1033\14\Built-In Building Blocks.dotx

我还没弄清楚如何从用户生成的文件中提取它们。

这是我一直在使用的正在进行的代码(多次迭代,因此我可以轻松调试它):

private void SaveBuildingBlock(string savedFile)
{
  try
  {
    Microsoft.Office.Interop.Word.ApplicationClass wordApplication = null;
    wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
    Microsoft.Office.Interop.Word.Document wordDocument = wordApplication.Documents.OpenNoRepairDialog(savedFile);

    object missing = System.Type.Missing;

    Database.ToolbarItemsWordInsertFileData.FileData = FileHandler.FileByteLocked(savedFile);
    Database.ToolbarItemsWordInsertFileData.Id = 0;
    Database.ToolbarItemsWordInsertFileData.ToolbarItemId = ToolbarId;
    Database.ToolbarItemsWordInsertFileData.Save();

    ListBuildingBlocks(wordApplication, wordPage);
  }
  catch (Exception err)
  {
    Logger.Log(err);
  }
}

下一个方法:

private void ListBuildingBlocks(Microsoft.Office.Interop.Word.Application wordApplication, WordPages wordPage)
{
  Microsoft.Office.Interop.Word.WdBuildingBlockTypes type = wordPage == WordPages.CoverPage ? type = Microsoft.Office.Interop.Word.WdBuildingBlockTypes.wdTypeCoverPage : type = Microsoft.Office.Interop.Word.WdBuildingBlockTypes.wdTypeWatermarks;
  for (int i = 1; i <= wordApplication.Templates.Count; i++)
  {
    try
    {
      if (wordApplication.Templates[i] != null)
      {
        Microsoft.Office.Interop.Word.Categories categories = wordApplication.Templates[i].BuildingBlockTypes.Item(type).Categories;
        for (int c = 1; c <= categories.Count; c++)
        {
          try
          {
            //Category cat = categories.Application[0];
            Microsoft.Office.Interop.Word.BuildingBlocks buildingBlocks = wordApplication.Templates[i].BuildingBlockTypes.Item(type).Categories.Item(categories.Item(c).Name).BuildingBlocks;
            for (int b = 1; b <= buildingBlocks.Count; b++)
            {
              try
              {
                Microsoft.Office.Interop.Word.BuildingBlock buildingBlock = buildingBlocks.Item(b);
                if (buildingBlock != null)
                {
                  //saving to database occurs here
                }
              }
              catch (Exception err)
              {
                MessageBox.Show(err.Message);
              }
            }
          }
          catch (Exception err)
          {
            MessageBox.Show(err.Message);
          }
        }
      }
    }
    catch (Exception err)
    {
      MessageBox.Show(err.Message);
    }
  }
}

WordPages 参数 [wordPage] 在应用程序中用于指定它是水印还是封面(它是此用户控件中的一个属性)。

【问题讨论】:

  • 我也尝试过使用 OpenXML,它似乎无法处理诸如 Building Blocks 之类的高级项目。
  • 如果你能提供一个word文件和你想得到的东西的清单会有所帮助
  • @Vadim,就像在我原来的帖子中一样,我说我想检索某人可能制作的水印和封面。至此,所有数据。我想将 Building Block 作为对象保存到数据库中。您可以查看至少包含水印或封面的任何正确生成的 dotx 文件。这已经被搁置了,所以我并不急于完成它。
  • 在那个令牌上,当我回到这个项目时,我会制作一个新文件以供参考。

标签: c# ms-word openxml


【解决方案1】:

您需要将其添加到当前加载项列表中,然后您可以将其作为模板打开以使用构建块。除了更改附加的模板并以这种方式加载之外,我看不到任何其他方式:

    Dim app = Globals.ThisAddIn.Application

    'load the file containing the building blocks
    app.AddIns.Add("\\Path to File.dotx")

    'get the object for the loaded template
    Dim template = app.Templates(app.AddIns.Count)

    'go through each building block in the file
    For I = 1 to template.BuildingBlockEntries.Count
        Dim bb = template.BuildingBlockEntries.Item(i)
    Next

如果您需要走 xml 路线,构建块存储在 word\glossary\document.xml 的包中,如果您在此服务器端执行此操作可能会更容易,那么您不需要使用 word 对象完全没有模型,只需使用Open Xml SDK,它对这样的事情非常有用!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多