View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Nigel[_2_] Nigel[_2_] is offline
external usenet poster
 
Posts: 735
Default Test if Word is running

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

--

Regards,
Nigel




"John" wrote in message
...
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("Green1BackCol or").Value
Range("D" & A.Row).Offset(1, 0) =
wdApp.Documents(nextFile).Variables("Green2BackCol or").Value
Range("D" & A.Row).Offset(2, 0) =
wdApp.Documents(nextFile).Variables("Green3BackCol or").Value
Range("D" & A.Row).Offset(3, 0) =
wdApp.Documents(nextFile).Variables("Green4BackCol or").Value
Range("D" & A.Row).Offset(4, 0) =
wdApp.Documents(nextFile).Variables("Green5BackCol or").Value
Range("D" & A.Row).Offset(5, 0) =
wdApp.Documents(nextFile).Variables("Green6BackCol or").Value
Range("D" & A.Row).Offset(6, 0) =
wdApp.Documents(nextFile).Variables("Green7BackCol or").Value
Range("D" & A.Row).Offset(7, 0) =
wdApp.Documents(nextFile).Variables("Green8BackCol or").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




"Nigel" wrote:

How are you creating an instance of Word?

Post your code.

--

Regards,
Nigel




"John" wrote in message
...
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!