经过一些实验后,我最终得到了以下代码,以便在MSWord中执行搜索和替换.此代码在页眉和页脚中也能很好地工作,包括第一页或奇数/偶数页面的页眉和/或页脚不同的情况.
问题是我需要调用MSWordSearchAndReplaceInAllDocumentParts我替换的每个字符串,并且我得到一个不可接受的性能(在4页doc字中大约50个字符串2分钟).理想情况下,它当然应该是"瞬时的".
在处理页眉和页脚之前,我只是在主文档中进行搜索和替换(使用wdSeekMainDocument).在这种情况下,perofmrance是可以接受的(即使很慢).我只是想知道它为什么这么慢:切换视图需要时间吗?通常页眉或页脚包含的单词很少,所以我预计页眉和页脚中的所有搜索和替换都不会使整体性能变得更糟.但这不是我观察到的.
这是代码,在底部我放置了探查器结果:
// global variable (just for convenience of posting to Stack Overflow)
var
aWordApp: OLEVariant; // global
// This is the function that is executed once per every string I replace
function MSWordSearchAndReplaceInAllDocumentParts;
begin
try
iseekValue := aWordApp.ActiveWindow.ActivePane.View.SeekView;
iViewType := aWordApp.ActiveWindow.ActivePane.View.Type;
if iViewType <> wdPrintView then
aWordApp.ActiveWindow.ActivePane.View.Type := wdPrintView;
if aWordApp.ActiveDocument.PageSetup.OddAndEvenPagesHeaderFooter then
begin
Try
aWordApp.ActiveWindow.ActivePane.View.SeekView := wdSeekEvenPagesFooter;
SearchAndReplaceInADocumentPart;
Except
// do nothing ..it was not able to set above view
end;
Try
aWordApp.ActiveWindow.ActivePane.View.SeekView := wdSeekEvenPagesHeader; …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过Interop.Word(11.0)使用Word Automation为Word文档编写查找/替换代码.我的文档都有各种字段(没有显示在Document.Fields中),用括号括起来,例如,<DATE>需要替换DateTime.Now.Format("MM/dd/yyyy").查找/替换工作正常.但是,一些被替换的文本是右对齐的,并且在替换时,文本将换行到下一行.当我执行替换时,有什么方法可以保持理由吗?代码如下:
using Word = Microsoft.Office.Interop.Word;
Word.Application wordApp = null;
try
{
wordApp = new Word.Application {Visible = false};
//.... open the document ....
object unitsStory = Word.WdUnits.wdStory;
object moveType = Word.WdMovementType.wdMove;
wordApp.Selection.HomeKey(ref unitsStory, ref moveType);
wordApp.Selection.Find.ClearFormatting();
wordApp.Selection.Find.Replacement.ClearFormatting(); //tried removing this, no luck
object replaceTextWith = DateTime.Now.ToString("MM/dd/yyyy");
object textToReplace = "<DATE>";
object replaceAll = Word.WdReplace.wdReplaceAll;
object typeMissing = System.Reflection.Missing.Value;
wordApp.Selection.Find.Execute(ref textToReplace, ref typeMissing,
ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing,
ref typeMissing, ref typeMissing, ref …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种方法,使用openxml在word doc中的书签后插入一些文本.到目前为止,我已经能够使用以下内容找到书签:
var bookmarks = mainPart.Document.Descendants<BookmarkStart>().ToList();
var bookMarkToWriteAfter = bookmarks.FirstOrDefault(bm => bm.Name == insertAfterBoomark.Name);
Run Code Online (Sandbox Code Playgroud)
单词doc中的这个书签是doc中两行的选择.我必须在两行选择后立即插入一些文本.我试图使用以下内容插入文本:
var run = new Run();
run.Append(new Text("Hello World"));
bookMarkToWriteAfter .Parent.InsertAfterSelf(run);
mainPart.Document.Save();
Run Code Online (Sandbox Code Playgroud)
然而,这不会产生期望的结果.有没有人知道使用openxml在word文档中的书签后插入文本的正确方法?
我正在为Microsoft Word开发一个VB6 COM加载项,我在功能区中添加了一个按钮,将文档保存到数据库中.但是在保存文档之前,我想将用户带到文档属性窗口,以便他们可以填写文档的属性(如标题,主题和作者).我使用以下语句来调出窗口:
Application.Dialogs(750).Display
Run Code Online (Sandbox Code Playgroud)
这样可以正常工作,但默认情况下会显示常规选项卡." 标题"," 主题"和" 作者 "的字段位于"摘要"选项卡上.有没有办法打开这个对话框并强制它到摘要选项卡?我考虑过发送击键,但是标签没有与之关联的热键.
我需要它在Word 2007和Word 2010中工作.上面的行在Word 2003中已经正常工作,因为2003没有多标签属性窗口.
我有一个WinForms应用程序,我使用Word Automation通过模板构建文档,然后将它们保存到数据库.创建文档后,我从数据库中检索文档,将其写入临时目录中的文件系统,然后使用Word Interop服务打开文档.
加载了一个文档列表,用户只能打开每个文档的一个实例,但可以同时打开多个不同的文档.打开1个文档时打开,保存和关闭没有任何问题,但是,当他们同时打开多个文档时,关闭我的任何Word实例时出现以下错误:
The file is in use by another application or user. (C:\...\Templates\Normal.dotm)
This error is commonly encountered when a read lock is set on the file that you are attempting to open.
Run Code Online (Sandbox Code Playgroud)
我使用以下代码打开文档并处理BeforeDocumentClosed事件:
public void OpenDocument(string filePath, Protocol protocol, string docTitle, byte[] document)
{
_protocol = protocol;
documentTitle = docTitle;
_path = filePath;
if (!_wordDocuments.ContainsKey(_path))
{
FileUtility.WriteToFileSystem(filePath, document);
Word.Application wordApplication = new Word.Application();
wordApplication.DocumentBeforeClose += WordApplicationDocumentBeforeClose;
wordApplication.Documents.Open(_path);
_wordDocuments.Add(_path, wordApplication);
}
_wordApplication = _wordDocuments[_path];
_currentWordDocument = _wordApplication.ActiveDocument;
ShowWordApplication(); …Run Code Online (Sandbox Code Playgroud) 我有一个word文档,可能有n个表.该表由表名标识,该表名在第一个单元格中写为标题.现在我必须找到带有表名的表,并在该表的一个单元格中写入.我尝试使用apache-poi,但无法弄清楚如何将它用于我的目的.如果我无法解释文档的外观,请参阅随附的屏幕截图.
谢谢 
String fileName = "E:\\a1.doc";
if (args.length > 0) {
fileName = args[0];
}
InputStream fis = new FileInputStream(fileName);
POIFSFileSystem fs = new POIFSFileSystem(fis);
HWPFDocument doc = new HWPFDocument(fs);
Range range = doc.getRange();
for (int i=0; i<range.numParagraphs(); i++){
Paragraph tablePar = range.getParagraph(i);
if (tablePar.isInTable()) {
Table table = range.getTable(tablePar);
for (int rowIdx=0; rowIdx<table.numRows(); rowIdx++) {
for (int colIdx=0; colIdx<row.numCells(); colIdx++) {
TableCell cell = row.getCell(colIdx);
System.out.println("column="+cell.getParagraph(0).text());
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我试过的,但这只能读取第一张表.
任何人都可以指导我如何使用开放式XML文字处理在段落上添加预定义样式吗?我在论坛上尝试了各种解决方案,但对我来说没什么用.这是我想要完成的:
// Create a document by supplying the filepath.
WordprocessingDocument wordDocument = WordprocessingDocument.Create("E:/Test/Executive.Docx", WordprocessingDocumentType.Document);
// Add a main document part.
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
// Create the document structure and add some text.
mainPart.Document = new Document();
Body body = mainPart.Document.AppendChild(new Body());
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text("Executive Summary"));
if (para.Elements<ParagraphProperties>().Count() == 0)
para.PrependChild<ParagraphProperties>(new ParagraphProperties());
// Get the ParagraphProperties element of the paragraph.
ParagraphProperties pPr = para.Elements<ParagraphProperties>().First();
// Set the value of ParagraphStyleId to …Run Code Online (Sandbox Code Playgroud) 我有一个C#程序,它做了一些Word和Excel自动化,它使用了Office 2003主互操作程序集.我部署它的方式是将Interops包含在bin中,我让我的程序从那里引用它,而不是从GAC引用它(不是非常专业,但它工作了fpr年).最近在3台机器上安装了Office 2007兼容包,现在当我的软件试图调用Word或者我发现错误时,我得到一个错误:
无法将Microsoft.Office.Interop.Word.ApplicationClass类型的COM对象转换为接口类型Microsoft.Office.Interop.Word.Application,此操作失败,因为QueryInterface在具有IID的接口的COM组件上调用{00020970-0000由于以下错误,-0000-C000-000000000046}失败:库未注册.(HRESULT的例外情况:0x8002801D(TYPE_E_LIBNOTREGISTERED))
经过一番搜索后,我注意到安装兼容包Word11和Word12都安装在GAC中,并且与Excel相同.
我试图让我的程序在GAC中引用Word11互操作,但仍然没有帮助.我没有安装Word12互操作,仍然没有任何帮助.似乎安装这似乎已经改变了一些注册表设置或我的软件混淆的东西找不到正确的库.
有人有想法吗?
我们需要为学校项目启动一个单词实例并跟踪文档是否已关闭.来自单词的COM api没有事件,有没有其他方法可以做到这一点?
目前我们正在使用COM api,但其他一切都没问题.我们正在用C#编程.
题
是否可以从C#调用Word 2003的工具>比较和合并文档..."函数,并获得是否发现任何差异的反馈?
到目前为止我发现了什么
可以像这样调用这样的功能.但我不知道如何得到反馈是否有任何差异.
private void CompareAndMergeWithFileB(string fullFilePath)
{
string FileName = fullFilePath;
object MergeTarget = WdMergeTarget.wdMergeTargetSelected;
object DetectFormatChanges = false;
object UseFormattingFrom = WdUseFormattingFrom.wdFormattingFromPrompt;
object AddToRecentFiles = true;
word.ActiveDocument.Merge(FileName, MergeTarget, DetectFormatChanges, UseFormattingFrom, AddToRecentFiles);
}
Run Code Online (Sandbox Code Playgroud)