【发布时间】: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 文件。这已经被搁置了,所以我并不急于完成它。
-
在那个令牌上,当我回到这个项目时,我会制作一个新文件以供参考。