testing if a word document is open within Access (several instances of word)

  • Thread starter Jean-Paul HORN via AccessMonster.com
  • Start date
J

Jean-Paul HORN via AccessMonster.com

I want to test if a word document is open within Access. The point is that
I can have several instances of word opened in the same time.

the getObject function works only if the document is open in the first
instance of Word.

I'have tried with WMI scripting. If managed to list all the instances of
word, but I couldn't test if any document was opened.

So my question is : how can I test if a Word Document is open when it is
not in the first instance of word, and how can I activate it ? (if possible
without terminating the other instances of Word)

thanks for help

jean-Paul
 
A

Alex Dybenko

Hi,
I think you can use API to enumerate processes of WinWord.exe, and then try
to get access to them, probably you can find sample code in google. but this
is quite complex approach, perhaps you can use some other solution - for
example each WinWord window has a caption with document name, so you can
find a window with necessary caption and activate it using API. I believe
that necessary functions for this you can find at www.mvps.org/access
 
J

John Nurick

Hi Jean-Paul,

What do you mean by "a Word document is open within Access"? If you're
using Access code to launch a Word document, just point an object
variable to the document as you launch it, e.g.
Set oDoc = GetObject("C:\folder\file.doc")

If you want to find out what Word documents are open, I don't know the
details but I think it involves using the API functions FindWindow() and
or FindWindowEx() to iterate through the windows on the system, looking
for windows whose class name indicates they belong to Word and whose
name (title?) indicates they are document windows. With luck a web
search will find the code you need.
 
J

Jean-Paul HORN via AccessMonster.com

Hi,

I have tried with WMI functions, and I can list all the processes but I
don't know how to activate them.

I have also tried with API functions (findWindowsLike) and then again I
didn't find how to manipulate the appropriate Window, with a Access object.

thanks for the link

JP
 
J

Jean-Paul HORN via AccessMonster.com

the problem is not to find the document, but to manipulate it, when
retrieved.

the document contains several bookmarks and I must fill all the bookmarks
with data stored in the Access DB. the person developping the application
allows the user to open several documents with the Createobject function,
and make changes in the document.
So, the problem is now, that I have to check if the document is open, and
to insert the data in the opened instance.

but I don't know how to use the handle I get through the FindWindow API
Function, in Access.

That is : what is the link between a DIM WdApp as Word.Application and the
hWnd returned by the API function ?

Am I clear enough ?

Thanks for your concern, anyway.

JP
 
J

John Nurick

The best thing to do would be to modify the application to ensure that
(a) all documents are opened in the same instance of Word and (b) an
object variable is set as each document is opened, e.g. (air code)

Set oWord = CreateObject("Word.Application")
....
Set oDoc1 = oWord.Documents.Open(filespec)
Set oDoc2 = oWord.Documents.Open(other filespec)
....

Otherwise I think it's going to take some fairly heavy API programming,
certainly beyond my experience. I'd start with a newsgroup search.
 
J

Jean-Paul HORN via AccessMonster.com

I agree with you, and I suggested the same solution as the one you have
proposed in your last email, but the person in charge of the programming
has decided not to change the code.

So I have to find another solution.

JP
 
J

John Nurick

Does the person in charge realise that their decision will cost maybe
four times as much of your time as doing it the sane way?

If so, treat it as an enjoyable opportunity to learn the Windows API at
someone else's expense<g>.

I've just had a thought: It may be simpler to tackle this crazy task by
using VB rather than VBA, and packaging the function(s) in a custom DLL.
 
N

Norman Goetz

Hello Jean-Paul

Some older Code below

I agree with you, and I suggested the same solution as the one you have
proposed in your last email, but the person in charge of the programming
has decided not to change the code.

So I have to find another solution.

JP

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA"
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Sub demoWord()
Dim objWord As Word.Application
Set objWord = detDoc("<INSERT_DOCUMENTNAME_HERE>")
If TypeName(objWord) <> "Nothing" Then
'Found an Instance of Word and the Document
objWord.Quit SaveChanges:=False
End If
Set objWord = Nothing
End Sub

Public Function detDoc(ByVal strDocument As String) As
Word.Application
Dim lngChan As Long
Dim strTopic As String
Dim objDoc As Word.Document
Dim objWord As Word.Application
Dim varArr
Dim lngCount As Long
Dim lngHwnd As Long
lngHwnd = FindWindow("OpusApp", vbNullString)
If lngHwnd Then
lngChan = DDEInitiate("Winword", "System")
strTopic = DDERequest(lngChan, "Topics")
DDETerminate lngChan
varArr = Split(strTopic, Chr$(9))
For lngCount = 0 To UBound(varArr)
If LCase$(varArr(lngCount)) = LCase$(strDocument) Then
Set objDoc = GetObject(strDocument)
Set objWord = objDoc.Application
Set detDoc = objWord
Exit Function
End If
Next lngCount
End If
Set detDoc = Nothing
End Function


Norman Goetz
 
J

Jean-Paul HORN via AccessMonster.com

thank you to all of you.
With all your advices I think I'll be able to find out a suitable solution

Jean-Paul
 
Top