We help IT Professionals succeed at work.

Check to see if a Word Doc is open

al4629740
al4629740 asked
on
Medium Priority
214 Views
Last Modified: 2010-04-30
I run a statistic program that calculates data.  Then after it executes, it places all the data into a Word doc.  I have the following code to open a Word Doc in VB.

Dim myWOrd As New Word.Application
Dim myDOc As Word.Document
Dim myRange As Range
Dim Para1 As Word.Paragraph

Set myDOc = myWOrd.Documents.Add()
 'Then I add my stuff to the doc here


However when I want to add again to the same doc after I execute the procedure, how do I check to see if the Word Doc is open rather then open a new doc?
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2010

Commented:
Hi al4629740,

On Error Resume Next
Set myDoc = myWord.Documents("Doc name.doc")
If Err <> 0 Then
    Err.Clear
    Set myDoc = myWord.Documents.Open("c:\folder\subfolder\Doc name.doc")
End If
On Error GoTo 0

Regards,

Patrick
theGhost_k8Database Consultant
CERTIFIED EXPERT

Commented:
here wdapp is Word.application

use for loop till wdapp.windows.count (shows total no of word doc. opened),
compare your doc. path and file name with following:-

if "c:\a.doc" = wdapp.Windows.item(2).Document.Path & "\" & wdapp.Windows.item(2).Caption then
    .....do whatever....

Author

Commented:
Thank You.  In my program, a new word document is always created when you click Execute for the first time, and it is usually called "Document1."  Until my program is closed, I want them to work on the same Document1 until they are finished.  

Basically I want them to append data everytime they click Execute into this Document1.  But unfortunately, the code I have above keeps creating a new document, instead of appending it to the currently opened Document1.  So the second time they press Execute a second document is created.  That's not we want.  We want to keep using the Document1.  How do I do to accomplish this?

Thanks Much!
theGhost_k8Database Consultant
CERTIFIED EXPERT

Commented:
but why are you writing code for creating Document in Execute button.
or let doc be created only for the first time, thereafter just use that doc object to insert data.
     inshort, place u'r code that creates a word doc. out of execute button's "area".
bye the way eazy way is place ur execute btn code here to get so called, perfact answer.

Author

Commented:
Ok here we go.  Scroll down to around the middle of this code to find where a Word doc is created and opened.  Thanks again for help.


Private Sub Command1_Click()

On Error GoTo Error1

If Option3.Value And Option4.Value And Option5.Value = False Then
MsgBox "Please select a Date by choosing DOR, DOE or Start Date.", vbOKOnly, "Error Handler"
Exit Sub
End If

If IsDate(Combo1) = False Then
MsgBox "Please enter a valid Date in the From: section.", vbOKOnly, "Error Handler"
Exit Sub
End If

If IsDate(Combo2) = False Then
MsgBox "Please enter a valid Date in the To: section.", vbOKOnly, "Error Handler"
Exit Sub
End If

Set conn = New ADODB.Connection
Set rec = New ADODB.Recordset
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\2000server\JJDP\Referral Database\db3.mdb;Persist Security Info=False"
'conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db3.mdb;Persist Security Info=False"

Dim Com(1 To 10) As String
Dim Report(1 To 10) As String

'Check for blank values in the Combo Boxes

If Combo1 = "" Then
MsgBox "You must enter a value first!", vbOKOnly, "Value Needed"
Exit Sub
End If

If Combo2 = "" Then
MsgBox "You must enter a value first!", vbOKOnly, "Value Needed"
Exit Sub
End If

'------------------------------

Dim a As String
Dim b As String
Dim c As String
Dim d As String
Dim e As Integer
Dim f As String
Dim g As String
Dim h As String
Dim i As String
Dim j As Integer
Dim k As Integer
Dim l As Integer
Dim m As Integer
Dim n As String


If Option3.Value = True Then
n = "DOR"
End If

If Option4.Value = True Then
n = "DOE"
End If

If Option5.Value = True Then
n = "StartDate"
End If

If Not Combo1 = "" Then
a = Combo1.Text
a = Format(a, "mm/dd/yyyy")
End If

If Not Combo2 = "" Then
b = Combo2.Text
b = Format(b, "mm/dd/yyyy")
End If



c = frmChooseProgram.Combo3.Text
d = Combo4.Text
e = Combo5.Text
f = Combo6.Text
g = Combo7.Text
h = Combo8.Text
j = Combo10.Text
k = Combo11.Text

l = Option1.Value
m = Option2.Value


pound = Chr(35)
hash = Chr(39)

        esql = "Select * from " & c & " Where " & n & " >= " & pound & a & pound
        esql = esql & " And " & n & " <= " & pound & b & pound
       
If Check1.Value = 1 Then
esql = esql & " And Sex =" & hash & d & hash
End If
   
If Check2.Value = 1 Then
esql = esql & " And Race =" & e
End If

If Check3.Value = 1 Then
esql = esql & " And Offense =" & hash & f & hash
End If

If Check4.Value = 1 Then
esql = esql & " And Status =" & hash & g & hash
End If

If Check5.Value = 1 Then
esql = esql & " And Outcome =" & hash & h & hash
End If


If l = True Then
    esql = esql & " And Age " & "= " & j
    Else
        If m = True Then
        esql = esql & " And Age " & ">= " & j & " And Age <= " & k
        End If
End If
   
        rec.Open (esql), conn, adOpenStatic, adLockOptimistic

Com(1) = rec.RecordCount
If Not rec.EOF Then rec.MoveNext
rec.Close
       
Label10.Caption = Com(1)

''Here is where the Word document is created each time.  After it is initially created, the next time this button is clicked, I want the code to just append to the current document.

Set myDOc = myWOrd.Documents.Add()




myDOc.Paragraphs.Last.Alignment = wdAlignParagraphCenter
myDOc.PageSetup.TopMargin = 25
myDOc.PageSetup.BottomMargin = 60
myDOc.PageSetup.LeftMargin = 25
myDOc.Sentences.Last.Font.Name = "Times New Roman"
myDOc.Sentences.Last.Font.Size = 16
myDOc.Sentences.Last.Bold = True

myDOc.Sentences.Last.Text = myDOc.Sentences.Last.Text & "JJDP Special Statistics"
myDOc.Sentences.Last.Text = myDOc.Sentences.Last.Text & c
myDOc.Sentences.Last.Font.Size = 14
myDOc.Sentences.Last.Text = myDOc.Sentences.Last.Text & " "
myDOc.Sentences.Last.Bold = False
myDOc.Paragraphs.Last.Alignment = wdAlignParagraphLeft

Set Para1 = myDOc.Content.Paragraphs.Add(myDOc.Bookmarks("\endofdoc").Range)
Para1.Range.InsertParagraphBefore

'Para1.Range.Text = ""
myDOc.Sentences.Last.Bold = True
myDOc.Sentences.Last.Font.Size = 12

If Option1.Value = True Then
myDOc.Sentences.Last.Text = myDOc.Sentences.Last.Text & "Age is Equal to " & j
End If

If Option2.Value = True Then
myDOc.Sentences.Last.Text = myDOc.Sentences.Last.Text & "Age is Between " & j & " and " & k
End If

If Check1.Value = 1 Then
myDOc.Sentences.Last.Text = myDOc.Sentences.Last.Text & "Sex =" & d
End If

If Check2.Value = 1 Then
myDOc.Sentences.Last.Text = myDOc.Sentences.Last.Text & "Race =" & e
End If

If Check3.Value = 1 Then
myDOc.Sentences.Last.Text = myDOc.Sentences.Last.Text & "Offense =" & f
End If

If Check4.Value = 1 Then
myDOc.Sentences.Last.Text = myDOc.Sentences.Last.Text & "Status =" & g
End If

If Check5.Value = 1 Then
myDOc.Sentences.Last.Text = myDOc.Sentences.Last.Text & "Outcome =" & h
End If

If Option3.Value = True Then
myDOc.Sentences.Last.Text = myDOc.Sentences.Last.Text & "Based on Date of Registration"
End If

If Option4.Value = True Then
myDOc.Sentences.Last.Text = myDOc.Sentences.Last.Text & "Based on Date of Exit"
End If

If Option5.Value = True Then
myDOc.Sentences.Last.Text = myDOc.Sentences.Last.Text & "Based on Start Date"
End If

myDOc.Sentences.Last.Text = myDOc.Sentences.Last.Text & "Date: " & a & " - " & b

myDOc.Sentences.Last.Text = myDOc.Sentences.Last.Text & ""
myDOc.Sentences.Last.Text = myDOc.Sentences.Last.Text & "The Calculation for the above Query is: " & Com(1)




myWOrd.Visible = True



Exit Sub

Error1:
MsgBox "Please Quit the Program and try again. An Error Has Occurred.  Please notify the administrator.", vbOKOnly, "Error Handler"
Retired
CERTIFIED EXPERT
Top Expert 2012
Commented:
I can't see a declarion for your Document object, myDOc, so it is (or should be) declared at a module or global level (outside of the Command1_Click procedure), e.g.

Public myDoc as Word.Document



In that case just test to see if the object has been instantiated:

    If myDOc Is Nothing Then
        Set myDOc = myWOrd.Documents.Add()
    End If

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
GrahamSkanRetired
CERTIFIED EXPERT
Top Expert 2012

Commented:
Sorry, I've just seen that how you declared it in your first comment, so you should be OK.

Author

Commented:
Exactly what I was looking for.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.