Jump to content
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×