-1

Before opening the word document i want to check whether same document is opened already or not. If opened then i want to close app.documents.open(as_doc_name)

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
user1788373
  • 39
  • 2
  • 6
  • word document already opened on my desktop so on running application the code get hanged and prompt for three option : 1)read only file 2)create local copy and merge changes later 3) no wait for file so to avoid this condition i want to check my file word doc is opened already or not. If opened then close word document and open as new copy – user1788373 Jan 25 '17 at 10:28
  • ole_myobject.connecttonewobject("word.application") ole_myobject.visible=false IF ole_myobject.Documents.Count >= 1 THEN Ls_doc_name = ole_myobject.ActiveDocument.Name END IF if Ls_doc_name =as_doc_name THEN ole_myobject.ActiveDocument.close(0) end if ole_myobject.documents.open(as_doc_name) – user1788373 Jan 25 '17 at 10:28

2 Answers2

1

You can try this

IF ole_myobject.Documents.Count >= 1 THEN
   ls_doc_name = ole_myobject.ActiveDocument.Name
END IF

Here you have help of its operation: http://www.java2s.com/Code/VBA-Excel-Access-Word/Word/Checkthecurrentdocumentcount.htm

Eduardo G.
  • 341
  • 1
  • 7
  • Locate the document with: "For ... Next". This verifies all open documents. – Eduardo G. Jan 26 '17 at 12:19
  • Checked active document count is comming zero but still i saw in taskbar winword.exe is runnung. please provide the code to check all active word document in powerbuilder – user1788373 Feb 02 '17 at 11:41
0

The code works. The following example will help you understand how to develop it:

Declare Instance Variables:

OLEObject ole_myobject

Event Open():

ole_myobject = CREATE OLEObject 
ole_myobject.connecttonewobject("word.application") 

Code in buttom:

String ls_doc_name
String as_doc_name
String as_path_name

Long ll_ActiveDocument

as_path_name = "C:\"
as_doc_name = "Prueba.doc"

ole_myobject.visible=1

ll_ActiveDocument = ole_myobject.Documents.Count

IF ll_ActiveDocument >= 1 THEN 
    Ls_doc_name = ole_myobject.ActiveDocument.Name 
END IF 
if Ls_doc_name = as_doc_name THEN 
    ole_myobject.ActiveDocument.close(0) 
end if 
ole_myobject.documents.open(as_path_name + as_doc_name)

Remember "ole_myobject.ActiveDocument.close(0)" does not close Word, it only closes the document but the Word application continues to run.

Eduardo G.
  • 341
  • 1
  • 7
  • ole_myobject.Documents.Count is showing Zero but still i am getting this error xxx.docx is locked for editing by user Do you want to: 1) Open a read Only Copy 2) Create a local copy and merge your changes later 3)Receive Notification when the Original Copy is available – user1788373 Feb 05 '17 at 05:37
  • Try doing the example I gave you, beware of the exact location of the connecttonewobject () function, an incorrect place always returns zero when using "ole_myobject.Documents.Count" – Eduardo G. Feb 06 '17 at 07:14
  • my code is similar to what you added above but problem is happening when word document get hanged and session got closed opnning new same and accessing the same document is cuasing the problem. – user1788373 Feb 06 '17 at 11:16