Word document identifying

Add-in Express™ Support Service
That's what is more important than anything else

Word document identifying
 
Marco




Posts: 6
Joined: 2018-03-02
Hello,

I need a way how I can identify a Word document uniquely during the runtime of my Add-in.

First, I thought I can use the “DocID” of the document but this ID is not uniquely during the runtime. Then I thought maybe the “FullName” of the document but with this is the problem if the user saves the document then the full name can be changing and because Word does not provide an event to get a notification about this I cannot use it.

Maybe over “GetHashCode()” method of the document I can solve it. But I have read in the internet that a document not always generates the same hash code because the document it self in C# it’s just a COM object wrapper and this wrapper can change for the document.

My question is now, what is the best way to identify a Word document during the runtime of the Add-in without having a reference to it so I can release it?
Posted 31 May, 2018 01:54:43 Top
Marcus from London




Posts: 33
Joined: 2015-04-12
Hi Marco,

Are you allowed to make a (hidden) changes to the documents?
If so, you could try a custom document property.

This is invisible to the user (unless they go looking for it) and persists between sessions.

Cheers - Marcus
Posted 31 May, 2018 02:16:45 Top
Marco




Posts: 6
Joined: 2018-03-02
Hello Marcus,

If it’s somehow possible I really want to avoid making hidden changes to the document. Because this also leads to a change in the document and if the user has nothing changed in the document and close it, a save question appears. I know I can call after setting the document property “document.Saved = true” and then no save question will appear. But I’m not sure if this could lead the some other unexpected behavior or a bug with setting the “Saved” property to true.

But thank you for your answer.
Posted 31 May, 2018 03:19:45 Top
Andrei Smolin


Add-in Express team


Posts: 15738
Joined: 2006-05-11
Hello Marco,

To identify a document with no possibility of changing it requires using the Document.FullFileName property and intercepting all ways for the user to invoke SaveAs. This includes:
- using ADXRibbonCommand components to intercept clicking all Ribbon controls that invoke SaveAs.
- using ADXKeyboardShortcut components to intercept all keyboard combinations that allow the user to invoke SaveAs
- hiding the SaveAs tab in Backstageview and [optionally] providing a custom tab as a replacement

When an ADXRibbonCommand (or ADXKeyboardShortcut component) triggers its event, you cancel the action, and show a the FileSave dialog after a delay. We give an example of this approach at https://www.add-in-express.com/creating-addins-blog/2011/12/05/excel-calculation-mode-event/. As to showing the FileSave dialog, see https://www.add-in-express.com/creating-addins-blog/2012/02/16/word-dialogues-programmatically/.

You may also be required to intercept the DocumentBeforeSave and DocumentBeforeSave events.

If it is possible to modify the document, you can add a variable to the Document.Variables collection; see https://msdn.microsoft.com/en-us/VBA/word-vba/articles/document-variables-property-word.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 31 May, 2018 04:31:40 Top
Marco




Posts: 6
Joined: 2018-03-02
Hello Andrei,

Thank you for your answer.

The document “FullName” looks to much work to implement, I think it would be better to use Word variables instead. But maybe I have another solution. What if I would use the document window “Hwnd” property to identify the document? I need only to identify documents which are shown in a document window. I could access the document window over the document itself like that: “document.ActiveWindow.Hwnd”

What do you think would be better, using a Word variable or using the document window “Hwnd” property?
Posted 31 May, 2018 05:20:43 Top
Andrei Smolin


Add-in Express team


Posts: 15738
Joined: 2006-05-11
Hello Marco,

This property was introduced in Word 2013. You cannot use it if you support Word 2000-2010. Our guys confirm this is a good way to identify Word windows.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 31 May, 2018 05:40:00 Top
Marco




Posts: 6
Joined: 2018-03-02
Hello Andrei,

The minimum supported version of my Add-in is Word 2013. I will then use the Word document window to identify documents.

Thank you Andrei for your support.
Posted 31 May, 2018 06:20:05 Top
Andrei Smolin


Add-in Express team


Posts: 15738
Joined: 2006-05-11
You are welcome!

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 31 May, 2018 06:29:12 Top