Jump to content
Registration disabled at the moment Read more... ×
misc_bb

Using COM to update Word docx files

Recommended Posts

Anyone playing around with docx files and trying to update a table of contents via win32com? I've got a working one with Python but our production environment runs on 10.4 only and we're having issues compiling it with P4D. So, i'll just ask if anyone has some similar implementation of this Python code in Delphi?

def update_toc(docx_file):
    word = win32com.client.DispatchEx("Word.Application")
    doc = word.Documents.Open(docx_file)
    doc.TablesOfContents(1).Update()
    doc.Close(SaveChanges=True)
    word.Quit()

thanks in advance for your thoughts and comments.

Share this post


Link to post

Try something like this:

procedure update_toc(const docx_file: string);
var
  word, doc: Variant;
begin
  word := CreateOleObject('Word.Application');
  doc := word.Documents.Open(WideString(docx_file));
  doc.TablesOfContents.Item(1).Update;
  doc.Close(-1);
  word.Quit;
end;
Edited by Remy Lebeau
  • Like 2

Share this post


Link to post

Thank you @Remy Lebeau I'll give this a try.

 

wow it does work! I never thought it was possible because in my readings, word needs to open the file in order to update the table of contents. Thanks again! 😉

Edited by misc_bb

Share this post


Link to post

CreateOleObject('com.sun.star.ServiceManager');

 

Read here

Share this post


Link to post

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×