Solved

Check to see if a Word Doc is open

Posted on 2006-10-26
8
154 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?
0
Comment
Question by:al4629740
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 17813619
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
0
 
LVL 21

Expert Comment

by:theGhost_k8
ID: 17817336
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....
0
 

Author Comment

by:al4629740
ID: 17822013
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!
0
 
LVL 21

Expert Comment

by:theGhost_k8
ID: 17824579
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:al4629740
ID: 17834795
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"
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 17840896
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
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 17840903
Sorry, I've just seen that how you declared it in your first comment, so you should be OK.
0
 

Author Comment

by:al4629740
ID: 17860037
Exactly what I was looking for.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

709 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now