1. Eduardo G.
  2. PowerBuilder
  3. Friday, 23 October 2020 06:40 AM UTC

Good morning,
How do I know if an MS Word document is open?

You have to take into account that the user can have several MS Word documents open, but I want to know if one is open in order to avoid opening it.

Thank you.

 

SOLUTION:
I solved it using the Handle.exe application from Sysinternals by Microsoft.
This executable returns if a filename is being used and also tells you the full path as the application that uses it.

Example:

handle.exe -nobanner test.docx

Result:

WINWORD.EXE        pid: 3876   type: File          1454: C:\process\test.docx

 

 

Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Friday, 23 October 2020 13:51 PM UTC
  2. PowerBuilder
  3. # 1

Hi Eduardo;

  Yes, you sure can! This is where the ...

  • FUNCTION  uLong FindWindow ( ref String classname, ref string windowname ) Library "USER32.DLL" ALIAS FOR "FindWindowW"
  • FUNCTION  uLong  FindWindowEx ( uLong hwnd, uLong childAfter, ref string classname, ref string windowname ) LIBRARY "USER32.dll"  ALIAS FOR "FindWindowExW"

SDK functions are your friend to basically locate anything open in the MS-Windows O/S.   ;-)

Regards ... Chris

Comment
  1. Eduardo G.
  2. Monday, 26 October 2020 15:18 PM UTC
It is not valid at all. See my answer in this thread to Miguel Leeuwe
  1. Helpful
There are no comments made yet.
Andreas Mykonios Accepted Answer Pending Moderation
  1. Friday, 23 October 2020 13:54 PM UTC
  2. PowerBuilder
  3. # 2

Hi Eduardo.

When a document is open with word, you should have a hidden file (a temporary file) which should have a similar name with the original document, and characters "~$" in at start.

In reality this file is done when the file is open for read & write. When it's open as read only it's not there. So depending on your case this may be a solution: to check if this hidden file exists.

Attention: there are cases when word crashes in which those hidden file remain on the disk. In that case this file may be used to recover work done before crash.

Finally, when the hidden file exists, it may also mean that this file is already in use by someone else.

Andreas.

Comment
  1. Miguel Leeuwe
  2. Friday, 23 October 2020 14:31 PM UTC
Good points!
  1. Helpful
  1. Eduardo G.
  2. Monday, 26 October 2020 15:18 PM UTC
If it is another option, although not 100% secure, sometimes we find users who unexpectedly close the computer and the temporary Word files remain until the Word document is opened again, therefore this option is not completely valid.
  1. Helpful
  1. Andreas Mykonios
  2. Monday, 26 October 2020 19:04 PM UTC
Normally the temporary file has a lock while your word document is edited. So if you are able to open it for write then your word file is probably closed.
  1. Helpful
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Friday, 23 October 2020 14:29 PM UTC
  2. PowerBuilder
  3. # 3

While I was adapting my userobject to filter out all the company stuff, Chris already sent you in the right direction.

See attached my pb 2019 R2 sample application, I hope it's any good to you. There's a button on the main window to open a test document.

If the document is already open, it will "bring it to front".

Note: you can use the n_findWindow.of_viewDocument(as_document) function also to view Excel documents or other file types. If it's not an excel nor word document, the document will be opened using the ShellExecute() API of windows.
If your app is not MDI based, you might have to change some things in the of_getParentWindow() function)

regards.

 Hope it's usefull. 

Attachments (1)
Comment
  1. Miguel Leeuwe
  2. Sunday, 25 October 2020 23:56 PM UTC
Let me know if you / anyone needs an earlier PB version for the sample app.
  1. Helpful
  1. Eduardo G.
  2. Monday, 26 October 2020 15:15 PM UTC
Thanks for your example, but to be completely correct I should know which of the following files is open, same name different path.



Example:

c:\aaa\test.doc

c:\bbb\test.doc
  1. Helpful
  1. Miguel Leeuwe
  2. Monday, 26 October 2020 15:23 PM UTC
Yes, That makes things more complicated and you might have to start getting a reference to the word instance somehow and see if you can read the full path of the opened document. If it's the powerbuilder application opening the files, you could save the path and the windowhandle in a datastore or arrays, so later you can see which windowhandle corresponds to exactly which document. .. (something like that). (but that still doesn't prevent your user from opening the same files without using the powerbuilder application).
  1. Helpful
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Sunday, 1 November 2020 23:16 PM UTC
  2. PowerBuilder
  3. # 4

Thanks for sharing your solution Eduardo, might be very useful:

SOLUTION:
I solved it using the Handle.exe application from Sysinternals by Microsoft.
This executable returns if a filename is being used and also tells you the full path as the application that uses it.

Example:

handle.exe -nobanner test.docx

Result:

WINWORD.EXE        pid: 3876   type: File          1454: C:\process\test.docx

Comment
  1. Miguel Leeuwe
  2. Monday, 2 November 2020 15:59 PM UTC
The only downside is that is has to be run with admin rights.
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.