Word 2010中的自动化:“转到书签” [英] Word 2010 automation: 'goto bookmark'

查看:238
本文介绍了Word 2010中的自动化:“转到书签”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Delphi编写-7的程序将打开它基于模板的新的Word文档。结果
一旦文档被打开,自动跳到一个书签(在模板pdefined $ P $),并增加一些文本那里。结果
下面code正常工作在Word 2003,但在Word 2010中引起无效的变异操作错误信息的(我省略了试/除了块清楚起见)

I have a program written in Delphi-7 which opens a new Word document which is based on a template.
Once the document is open, the automation jumps to a bookmark (predefined in the template) and adds some text there.
The following code works fine in Word 2003 but causes a invalid variant operation error message in Word 2010 (I have omitted try/except blocks for the sake of clarity).

wrdapp:= CreateOleObject ('Word.Application');
wrdDoc:= wrdapp.documents.add (wrdApp.Options.DefaultFilePath[wdUserTemplatesPath] + '1.dot'
wrdApp.selection.goto (wdGotoBookmark, unassigned, unassigned, 'B1')

如果我更换了与第三行

wrdDoc.bookmarks.item ('B1').select

该程序在Word 2003中工作正常,但在Word 2010中仍然崩溃。

the program works fine in Word 2003 but still crashes in Word 2010.

什么是正确的code为Word 2010中'去'书签?

What is the correct code for Word 2010 to 'go to' the bookmark?

推荐答案

Word 2010中有一个关系到装载的Normal.dotm一个bug(也许太插件,谁知道?)。当您启动Word 2010中,你通常会做的,你看到一个启动画面和Word进行一些初始化,包括加载的Normal.dotm。当您通过自动化启动Word - CreateOleObject('Word.Application') - 它不会等到的Normal.dotm被加载并立刻返回。但仍然被加载的Normal.dotm时执行操作似乎崩溃的话。我所做解决这个问题是使一个循环,只是等待模板加载。您也可以选择延迟给Word来初始化时间,但到目前为止,循环工作。

Word 2010 has a bug that is related to loading Normal.dotm (and maybe plugins too, who knows?). When you start Word 2010 as you would normally do, you see a splashscreen and Word performs some initialization, including loading Normal.dotm. When you start Word via automation - CreateOleObject('Word.Application') - it doesn't wait till Normal.dotm is loaded and returns immediatly. But performing operations when Normal.dotm is still being loaded seems to crash Word. What I did to solve this problem is to make a loop that just waits for the template to load. You can also choose for a delay to give Word the time to initialize, but so far, the loop works.

事情是这样的:

wrdapp := CreateOleObject('Word.Application');

//loop that waits for the normal template to load
while wrdapp.Templates.Count = 0 do
  Sleep(200);

//continue operations

PS:我没有德尔福可以在这里找到,所以code可能有错,但你的想法

PS: I don't have Delphi available here, so the code may contain errors, but you get the idea

这篇关于Word 2010中的自动化:“转到书签”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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