Save document with custom properties with actxserver Word

The following script shall change custom properties of a word document using the activexserver syntax for Microsoft Word.
%% Initiallize Word Connection
hdlWord = actxserver('Word.Application');
hdlWord.Visible = true;
%% File and Location
fullLocation = 'C:\Users\Name\Documents\MATLAB\PropTest.docx';
hdlDoc = hdlWord.Documents.Open(fullLocation);
%% Property Change
hdlProp = get(hdlDoc.CustomDocumentProperties, 'Item', 'Property1');
get(hdlProp,'Value');
set(hdlProp,'Value','nice'); % saved as "jealous" before running the script
%% Save Doc
hdlWord.ActiveDocument.SaveAs2(fullLocation)
hdlDoc.Close();
hdlWord.Quit();
The property change itself is working. Sadly, the script is not saving the document. There is no error or warning given out.
If I comment the close functions and click save manually it works fine.
If I save the document as a another file the properties are changed and saved. Therefore, a work around by deleting and renaming and so on would work.
Could someone help me out on how to save the opened document and close it by script?

5 Comments

Hi Philipp,
You can add a line of code to save the document after the property change is made. You can use the following syntax:
hdlDoc.Save();
This will save the changes made to the document. You can also add a line of code to close the document after it has been saved:
hdlDoc.Close();
With these modifications, your updated script should look like this:
%% Initiallize Word Connection
hdlWord = actxserver('Word.Application');
hdlWord.Visible = true;
%% File and Location
fullLocation = 'C:\Users\Name\Documents\MATLAB\PropTest.docx';
hdlDoc = hdlWord.Documents.Open(fullLocation);
%% Property Change
hdlProp = get(hdlDoc.CustomDocumentProperties, 'Item', 'Property1');
get(hdlProp,'Value');
set(hdlProp,'Value','nice'); % saved as "jealous" before running the script
%% Save Doc
hdlDoc.Save();
hdlDoc.Close();
hdlWord.Quit();
By adding these lines of code, your script should now be able to save the changes made to the Word document and close it automatically. If you continue to experience issues, you may want to check that you have appropriate permissions to save files in that directory or consider using a different file location. Hope this will help resolve your problem. Please let me know if you have further questions.
Hi Umar,
thanks for the quick response. Sadly, the suggested solution is not working. I do have all rights in this directory.
If I put in a prompt command after changing the property to pause the code from running and then switch to the opened word document, the property is changed.
After using .save .close and .quit, the old property is in the word document. It looks like the save function is not doing anything.
%% Property Change
hdlProp = get(hdlDoc.CustomDocumentProperties, 'Item', 'Property1');
get(hdlProp,'Value');
set(hdlProp,'Value','nice'); % saved as "jealous" before running the script
prompt = "Hit enter to continue";
input(prompt);
% switch to word window and the porperty is changed as expected
%% Save Doc
hdlDoc.Save();
hdlDoc.Close();
hdlWord.Quit();
% open word again and the property is still the old one "jealous"
Hi Phillip,
Try introducing a delay after calling the Save() function. This delay allows time for the changes to be processed and saved before closing the document. By adding a short delay (e.g., pause(1)) after the Save() function, you allow sufficient time for the changes to be saved before closing and quitting the Word document. Please update me about test results.
Hi Umar,
the pause function is not helping. Neither using Doc.Save(); nor using .SaveAs2();
FYI: I used a tempfile as work around. Saving to a new temp file, then deleting the original one. Renaming the temp file to the original name.
Hi Philipp,
Mario just posted a comment about resolving this issue. Please try his suggested solution and update us about test results.

Sign in to comment.

Answers (1)

4 Comments

Yes, that is the answer! Thank you for sharing.
Finally issue has been resolved. Thanks Mario for your contribution. This is what I like about Mathworks platform, team work, help out each other, share knowledge. Philipp, time to celebrate 🎉.
The flag is not ment to be used for these purposes, so it's better to remove it.
If the answer solved your problem, it should be marked as accepted.

Sign in to comment.

Categories

Products

Release

R2020b

Asked:

on 10 Jul 2024

Commented:

on 15 Jul 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!