Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 18th, 2006, 08:30 AM
Friend of Wrox
 
Join Date: Mar 2004
Location: Yorba Linda, California, USA.
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

Reply With Quote
  #2 (permalink)  
Old October 20th, 2006, 06:34 AM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
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
Reply With Quote
  #3 (permalink)  
Old October 20th, 2006, 09:32 AM
Friend of Wrox
 
Join Date: Mar 2004
Location: Yorba Linda, California, USA.
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

Reply With Quote
  #4 (permalink)  
Old October 20th, 2006, 10:01 AM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
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
Reply With Quote
  #5 (permalink)  
Old October 20th, 2006, 11:25 AM
Friend of Wrox
 
Join Date: Mar 2004
Location: Yorba Linda, California, USA.
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

Reply With Quote
  #6 (permalink)  
Old October 20th, 2006, 11:32 AM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
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
Reply With Quote
  #7 (permalink)  
Old October 20th, 2006, 01:26 PM
Friend of Wrox
 
Join Date: Mar 2004
Location: Yorba Linda, California, USA.
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......

Reply With Quote
  #8 (permalink)  
Old October 20th, 2006, 01:48 PM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 06:58 PM.


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