我有一个用Delphi-7编写的程序,它打开一个基于模板的新Word文档。
打开文档后,自动跳转到书签(在模板中预定义)并在其中添加一些文本。
以下代码在Word 2003中运行良好,但会在Word 2010中导致invalid variant operation错误消息(为了清楚起见,我省略了try/except块)。
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中仍然崩溃。
单词2010“转到”书签的正确代码是什么?
发布于 2011-05-07 15:46:17
Word 2010有一个与加载Normal.dotm相关的错误(也许插件也是,谁知道呢?)当您像平常一样启动Word 2010时,您会看到一个启动屏幕,Word执行一些初始化,包括加载Normal.dotm。当您通过自动化( CreateOleObject('Word.Application') )启动Word时,它不会等到加载Normal.dotm之后立即返回。但是,当Normal.dotm仍在加载时执行操作似乎会导致崩溃。为了解决这个问题,我所做的就是做一个循环,等待模板加载。您也可以选择延迟来给Word初始化的时间,但是到目前为止,循环可以工作。
就像这样:
wrdapp := CreateOleObject('Word.Application');
//loop that waits for the normal template to load
while wrdapp.Templates.Count = 0 do
Sleep(200);
//continue operationsPS:我这里没有Delphi,所以代码可能包含错误,但是你知道
发布于 2011-05-07 11:48:45
我认为您应该用变量替换"GoTo_“调用中的常量。就像这样:
...
var
vWhat, vBookmark:OleVariant;
begin
...
vWhat:=wdGoToBookmark;
vBookmark:='B1';
wrdApp.Selection.GoTo_(vWhat,emptyParam,emptyParam,vBookmark);
...
end;发布于 2011-05-19 17:38:49
你好,希望这个能帮到你。我正在使用D2010和Office 2010
我要做的是:如果我找到一个书签名,我就在这里插入一个word文档
我的代码之一是:
try
Template := EmptyParam;
NewTemplate := true;
ItemIndex := 1;
try
Wdapplication.Connect;
except
Screen.Cursor := crDefault;
MessageDlg('No se detecta Word Puede no estar instalado(1) o versi?n incorrecta de Word', mtError, [mbOK], 0);
Abort;
result := False;
end;
Wdapplication.Visible := true; // False;
WdApplication.Caption := 'Kalemat automation';
{Turn Spell checking of because it takes a long time if enabled and slows down Winword}
WdApplication.Options.CheckSpellingAsYouType := false;
WdApplication.Options.CheckGrammarAsYouType := false;
lbInfo.Lines.Add('Word connected');
except
on E: Exception do begin
ShowMessage(E.Message);
WdApplication.Disconnect;
result := False;
Exit;
end;
end;
//-
if wdapplication.Documents.Count > 0 then begin
Screen.Cursor := crDefault;
MessageDlg(
'Por Favor cierre todos sus Word-documentos antes de proseguir...', mtWarning,
[mbRetry], 0);
wdApplication.Visible := true;
WdApplication.Disconnect;
result := False;
exit;
end
else begin
with WdApplication do begin
// OnQuit := WordAppQuit;
// OnChangeDocument := WordDocChange;
// OnOpenDocument := WordDocOpen;
// OnPreCloseDocument := WordPreClose;
// OnCloseDocument := WordDocClose;
// DisableSystemCloseBox;
end
end;
{Create new document}
Template := EmptyParam;
NewTemplate := false;
oNewDocument := ModEsc;
// abre documento
lbInfo.Lines.Add('Abriendo escritura '+ModEsc);
WdApplication.Documents.AddOld(oNewDocument, NewTemplate);
// Conecta con al instancia de Word
WdDocument.ConnectTo(WdApplication.Documents.Item(ItemIndex));
sBookMarkName := 'FPROEMIO';
lbInfo.Lines.Add('Busca marcador Proemio');
if WdDocument.Bookmarks.Exists(sBookMarkName) then begin
// ShowMessage(' -Existe: '+sBookMarkName);
owhat := wdGotoBookMark;
owhich := unAssigned;
ocount := unAssigned;
//-->>> // ShowMessage(' -Ve a..: '+sBookMarkName);
//-->>> // Ve a ese marcados addendum
wdDocument.GoTo_(oWhat, oWhich, OCount, sBookMarkName);
// ShowMessage(' GoTo_.. ya estoy en: '+sBookMarkName);
// Lo encontre
oRange := '';
oConformConv := false;
oLink := false;
oattachment := false;
fl_Name := proemi;
lbInfo.Lines.Add('Insertando Proemio '+Proemi);
if not FileExists(fl_name) then begin
Screen.Cursor := crDefault;
lbInfo.Lines.Add('No Existe Documento PROEMIO ');
MessageDlg('Documento FPROEMIO NO EXISTE, Revise el modelo de escritura', mtError, [mbRetry], 0);
end
else
wdDocument.Bookmarks.Item(sBookMarkName).Range.InsertFile(Fl_Name, oRange, oConformConv, oLink, oattachment);
// ShowMessage(' -.. inserte el addendum');
end
else begin
lbInfo.Lines.Add('No Existe Marcador PROEMIO ');
end;https://stackoverflow.com/questions/5913665
复制相似问题