Test if Word is running

J

John

I have a macro that cycles through a group of word docs and imports data into
an excel spreadsheet. Unfortunetly, if a copy of Word is running before the
macro runs I get all kinds of errors.

Is there any way to test from Excel if any copies of Word are running, and
if so stop the macro?

Using Excel 2003, thanks in advance!
 
J

John

Not sure how the code create the instance of Word matters. I just need
something that identifies if Word is running, and if so, stops the macro. But
here is my code anyway:

Sub CommandCheck()

Dim oWord As Word.Application
Dim Counter As Boolean
Dim wdApp As Word.Application, wdDoc As Word.Document
Dim Dest As Workbook
Dim Source As Word.Document
Dim nextFile As Variant

Sheets("Sheet1").Select
Set RngColA = Range("A2", Range("A" & Rows.Count).End(xlUp))
Dim MyDir As String
MyDir = Workbooks("CommandCenter.xls").Path & "\"
Application.ScreenUpdating = False
Application.EnableEvents = False
NewFile = True
nextFile = Dir(MyDir & "*.doc")
Set wdApp = CreateObject("Word.Application")
'wdApp.Visible = True
For Each A In RngColA
If A <> "" Then
Do While nextFile <> ""
If (A & ".doc") = nextFile Then
Set wdDoc = wdApp.Documents.Open(MyDir & nextFile)
Range("C" & A.Row) =
wdApp.Documents(nextFile).Variables("TextBox1Text").Value
Range("C" & A.Row).Offset(1, 0) =
wdApp.Documents(nextFile).Variables("TextBox2Text").Value
Range("C" & A.Row).Offset(2, 0) =
wdApp.Documents(nextFile).Variables("TextBox3Text").Value
Range("C" & A.Row).Offset(3, 0) =
wdApp.Documents(nextFile).Variables("TextBox4Text").Value
Range("C" & A.Row).Offset(4, 0) =
wdApp.Documents(nextFile).Variables("TextBox5Text").Value
Range("C" & A.Row).Offset(5, 0) =
wdApp.Documents(nextFile).Variables("TextBox6Text").Value
Range("C" & A.Row).Offset(6, 0) =
wdApp.Documents(nextFile).Variables("TextBox7Text").Value
Range("C" & A.Row).Offset(7, 0) =
wdApp.Documents(nextFile).Variables("TextBox8Text").Value
Range("D" & A.Row) =
wdApp.Documents(nextFile).Variables("Green1BackColor").Value
Range("D" & A.Row).Offset(1, 0) =
wdApp.Documents(nextFile).Variables("Green2BackColor").Value
Range("D" & A.Row).Offset(2, 0) =
wdApp.Documents(nextFile).Variables("Green3BackColor").Value
Range("D" & A.Row).Offset(3, 0) =
wdApp.Documents(nextFile).Variables("Green4BackColor").Value
Range("D" & A.Row).Offset(4, 0) =
wdApp.Documents(nextFile).Variables("Green5BackColor").Value
Range("D" & A.Row).Offset(5, 0) =
wdApp.Documents(nextFile).Variables("Green6BackColor").Value
Range("D" & A.Row).Offset(6, 0) =
wdApp.Documents(nextFile).Variables("Green7BackColor").Value
Range("D" & A.Row).Offset(7, 0) =
wdApp.Documents(nextFile).Variables("Green8BackColor").Value
Range("E" & A.Row) =
wdApp.Documents(nextFile).Variables("Red1BackColor").Value
Range("E" & A.Row).Offset(1, 0) =
wdApp.Documents(nextFile).Variables("Red2BackColor").Value
Range("E" & A.Row).Offset(2, 0) =
wdApp.Documents(nextFile).Variables("Red3BackColor").Value
Range("E" & A.Row).Offset(3, 0) =
wdApp.Documents(nextFile).Variables("Red4BackColor").Value
Range("E" & A.Row).Offset(4, 0) =
wdApp.Documents(nextFile).Variables("Red5BackColor").Value
Range("E" & A.Row).Offset(5, 0) =
wdApp.Documents(nextFile).Variables("Red6BackColor").Value
Range("E" & A.Row).Offset(6, 0) =
wdApp.Documents(nextFile).Variables("Red7BackColor").Value
Range("E" & A.Row).Offset(7, 0) =
wdApp.Documents(nextFile).Variables("Red8BackColor").Value
Counter = True
End If
nextFile = Dir
Loop
If Counter = False Then
Range("C" & A.Row) = "File Missing"
Range("C" & A.Row).Offset(1, 0) = "File Missing"
Range("C" & A.Row).Offset(2, 0) = "File Missing"
Range("C" & A.Row).Offset(3, 0) = "File Missing"
Range("C" & A.Row).Offset(4, 0) = "File Missing"
Range("C" & A.Row).Offset(5, 0) = "File Missing"
Range("C" & A.Row).Offset(6, 0) = "File Missing"
Range("C" & A.Row).Offset(7, 0) = "File Missing"
Range("D" & A.Row) = "32768"
Range("D" & A.Row).Offset(1, 0) = "32768"
Range("D" & A.Row).Offset(2, 0) = "32768"
Range("D" & A.Row).Offset(3, 0) = "32768"
Range("D" & A.Row).Offset(4, 0) = "32768"
Range("D" & A.Row).Offset(5, 0) = "32768"
Range("D" & A.Row).Offset(6, 0) = "32768"
Range("E" & A.Row) = "128"
Range("E" & A.Row).Offset(1, 0) = "128"
Range("E" & A.Row).Offset(2, 0) = "128"
Range("E" & A.Row).Offset(3, 0) = "128"
Range("E" & A.Row).Offset(4, 0) = "128"
Range("E" & A.Row).Offset(5, 0) = "128"
Range("E" & A.Row).Offset(6, 0) = "128"
Range("E" & A.Row).Offset(7, 0) = "128"
End If
nextFile = Dir(MyDir & "*.doc")
Counter = False
End If
Next A
wdDoc.Close
wdApp.NormalTemplate.Save
wdApp.Quit

Sheets("Sheet1").Select
Columns("C:C").Select
Selection.TextToColumns Destination:=Range("C1"),
DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), Array(64, 1)),
TrailingMinusNumbers:=True
Application.ScreenUpdating = True
MsgBox "Done"
End Sub
 
J

Jacob Skaria

Set the visible property of Word Application object to True .

If this post helps click Yes
 
J

John

Can you elaborate? How does setting the visible property allow me to test if
Word is running?
 
N

Nigel

The solution is wholly dependent on how you create the instance of word, as
your later post and solution reveals.

--

Regards,
Nigel
(e-mail address removed)
 
J

Jim Cone

Couple of suggestions...
Use Option Explicit as a single line at the top of the module.
Qualify all objects with the appropriate parent object.
(both Word and Excel have a range object)...

Dim RngColA As Excel.Range
Set MySht = ThisWorkbook.Sheets("Sheet1")
Set RngColA = MySht.Range("A2", MySht.Range("A" & MySht.Rows.Count).End(xlUp))
...all objects
--
Jim Cone
Portland, Oregon USA




"John" <[email protected]>
wrote in message
Nevermind...found a solution.

On Error Resume Next
Set oWord = GetObject(, "Word.Application")
If Err Then
Else
MsgBox ("Stop")
End
End If

Credit goes to http://www.word.mvps.org/FAQs/InterDev/ControlXLFromWord.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top