Word 2010 Automation: "goto bookmark"

I have a program written in Delphi-7 that opens a new Word document based on a template.
When the document is open, the automation goes to the bookmark (predefined in the template) and adds some text there.
The following code works fine in Word 2003, but it raises an invalid variant operation error message in Word 2010 (for clarity, I skipped try/except ).

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

If I replace the third line with

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

The program works fine in Word 2003, but still crashes in Word 2010.

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

+6
source share
3 answers

In Word 2010, there is an error related to loading Normal.dotm (and maybe plugins, who knows?). When you start Word 2010, as usual, you see a splash screen, and Word does some initialization, including loading Normal.dotm. When you start Word through automation - CreateOleObject('Word.Application') - it does not CreateOleObject('Word.Application') for Normal.dotm to load and immediately returns. But executing operations when Normal.dotm is still loading seems to break Word. What I did to solve this problem was to make a loop that just waits for the template to load. You can also choose a delay to give Word time to initialize, but the loop still works.

Something like that:

 wrdapp := CreateOleObject('Word.Application'); //loop that waits for the normal template to load while wrdapp.Templates.Count = 0 do Sleep(200); //continue operations 

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

+5
source

I think you should replace the constants in the "GoTo_" call with variables. For instance:

 ... var vWhat, vBookmark:OleVariant; begin ... vWhat:=wdGoToBookmark; vBookmark:='B1'; wrdApp.Selection.GoTo_(vWhat,emptyParam,emptyParam,vBookmark); ... end; 
0
source

Hi, hope this helps you. I am using D2010 and Office 2010

What I do: if I find the bookmark name, I insert the word Document at this point

Part of my code:


 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; 
0
source

Source: https://habr.com/ru/post/887457/


All Articles