Wrox Programmer Forums
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 18th, 2006, 08:30 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 217
Thanks: 0
Thanked 1 Time in 1 Post
Default detecting if Word is open

I have a sub that opens Word to complete a mail merge. It uses GetObject, and works fine when there are no instances of Word open. But it errors if Word is open.

Helen Fedema writes in her book that GetObject will work if Word is open, and CreateObject will work open Word if it isn't. I don't find other references to this, so....

Does anyone know how to open Word (from Access via VBA)when Word is already open?

Thanks-
Loralee

 
Old October 20th, 2006, 06:34 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

CreateObject should also work if Word is open. But you will want to check if Word is open first.Alternatively you could shut down Word through the WIN32 processes, but you probably don't want to do that.




mmcdonal
 
Old October 20th, 2006, 09:32 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 217
Thanks: 0
Thanked 1 Time in 1 Post
Default

So you are saying CreateObject should work regardless of whether there is an instance of Word running?
Is there a more efficient way to test if WOrd is open other than catching the error thrown when GetObject fails?

I've been through multiple books and other sources and haven't found much on this.

Thanks, Loralee

 
Old October 20th, 2006, 10:01 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

This should get you started:

'----------------------------
Dim sComputer As String

sComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel = impersonate}!\\" & sComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'WINWORD.EXE'")

For Each objProcess In ColProcesses
    MsgBox objProcess.Name
        'or, for example
        'If objProcess.Name <> "" Then
           '...
        'End If
Next
'----------------------------

I am not sure if the IF Then will throw and error if you try to read objProcess.Name and it is Null.
Or if the Set colProcesses will... come to think of it, maybe you should do this:

'--------------------------
Dim sComputer As String
Dim bWord As Boolean

bWord = False
sComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel = impersonate}!\\" & sComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process")

For Each objProcess In ColProcesses
    If objProcess.Name = "WINWORD.EXE" Then
             bWord = True
        End If
Next

If bWord = True Then
  ...
  Else
  ...
End If
'---------------------------

HTH

mmcdonal
 
Old October 20th, 2006, 11:25 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 217
Thanks: 0
Thanked 1 Time in 1 Post
Default

Yikes!
I haven't seen Win32 processes /Windows in that way and that looks like it's beyond where I'm shooting for. I was hoping there would be something simpler.........for this beginner to try.

Thanks-
Loralee

 
Old October 20th, 2006, 11:32 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Well, its pretty simple, conceptually.

Win32_Processes is just a table in memory (swap file usually) that shows what processes are running. To access this data normally, you just open the Task Manager <CTRL><SHIFT><ESC> and look at all the columns you can add. You can call each one of those columns by attaching to the database on your PC.

This really adds a lot of functionality, so something worth considering.

You would add your code here:


If bWord = True Then
  'CreateObject code
  Else
  'GetObject code
End If

HTH




mmcdonal
 
Old October 20th, 2006, 01:26 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 217
Thanks: 0
Thanked 1 Time in 1 Post
Default

Is Win32_Processes supported by XP as well as NT and 2000? This little project is meant to be run by 5 users, and not everyone has the same OS.

I'm going to play with it this weekend. If it's useful and not too deep....and a gateway to learning a little more about Windows' dark side......

 
Old October 20th, 2006, 01:48 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Why yes it is supported by all those versions of windows.

Check out the scripting guys at Microsoft.com and get their scriptomatic tool to develop vbscripts that will work within Access. It really is very simple execpt for the reserved words.

The best book on this subject is Microsoft Press Windows 2000 Scripting Guice, and Advanced VBScript for Windows Administrators.

I use these scripts to populate a SQL database with snapshots of our users' computers every month.

mmcdonal





Similar Threads
Thread Thread Starter Forum Replies Last Post
open Ms Word Application on IE Sheikh Abdul Jaweed Javascript 0 December 11th, 2006 06:39 AM
Detecting Open Database Connections? Ron Howerton SQL Server ASP 1 July 14th, 2005 02:26 PM
script to open db to ms word? weedee Classic ASP Databases 5 June 2nd, 2005 04:40 PM
open, change a excel document from word kareltje Excel VBA 2 February 14th, 2005 11:23 AM
Cannot open Word Document At Client Side leo_ling Classic ASP Basics 6 July 1st, 2004 12:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.