PDA

View Full Version : [SOLVED:] Problem with Document Close



naigy
02-24-2006, 04:08 AM
Hi. I am relatively new to vba in word but have a reasonable understanding of its use in Access. I have a problem with the following code where this vba is in the activedocument (oDoc) extracts data from a table in oLookupDoc. Once this data lookup is down I want to close the Word window which has oLookupDoc but can't seem to do it. My code will close the file but not that instance of Word. Please see code below. Alternatively if I can't close this instance of word is it possible to close all instances of word which do not have an open document. Thanks.


Private Sub Document_Open()
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oLookupDoc As Word.Document
Dim oTable As Table
Dim Start As Word.Document
Set Start = ActiveDocument
Set oWord = CreateObject("Word.Application")
Set oDoc = Start
Set oLookupDoc = oWord.Documents.Open(ActiveDocument.Path & "Export.doc")
Set oTable = oLookupDoc.Tables(1)
oWord.Visible = True
oDoc.Bookmarks("Name1").Range.Text = oTable.Cell(2, 2).Range.Text
oLookupDoc.Close
End Sub

TonyJollans
02-24-2006, 05:14 AM
Hi naigy,

Welcome to VBAX!

First off - to naswer your actual question ...

oWord.Quit
will close the Word instance, but ...

If you are running in WOrd to start with you don't (normally) want to start another instance. All you should need is[vba] Private Sub Document_Open()

Dim oDoc As Word.Document
Dim oLookupDoc As Word.Document
Dim oTable As Table
Set oDoc = ActiveDocument
Set oLookupDoc = Documents.Open(ActiveDocument.Path & "Export.doc")
Set oTable = oLookupDoc.Tables(1)
oDoc.Bookmarks("Name1").Range.Text = oTable.Cell(2, 2).Range.Text
oLookupDoc.Close
End Sub

fumei
02-24-2006, 09:05 AM
There are odd things in the code, and one of them Tony mentions.


Set Start = ActiveDocument
Set oWord = CreateObject("Word.Application")
Set oDoc = Start

You are setting Start to be ActiveDocument - this implies that there IS an instance of Word. Then Set oWord creates another instance. Also I have to ask if Start is a Word.Document, and oDoc is a Word.Document why are you making two object the same document?


Start = ActiveDocument
oDoc = Start

TWO document objects for the same file. Is there some reason for this?

mdmackillop
02-24-2006, 11:30 AM
Hi Naigy,
If you select your code and click on the VBA tab, it will format it as shown, making it more readable.
Regards
MD

naigy
03-03-2006, 10:04 PM
Problem now solved. FYI if you are interested I ended up using the following code. I am not sure how to change the subject line of orginal message to Solved. If someone wishes to this on my behalf I would appreciate it.


Private Sub Document_Open()
Dim oDoc As Word.Document
Dim oLookupDoc As Word.Document
Dim oTable As Table
Set oDoc = ActiveDocument
Set oLookupDoc = Documents.Open(ActiveDocument.Path & "\Export.doc", , ReadOnly:=True)
Set oTable = oLookupDoc.Tables(1)
Set oTable2 = oLookupDoc.Tables(2)
Set oTable3 = oLookupDoc.Tables(3)
'oDoc.Bookmarks("JobType").Range.Text = oTable.Cell(2,2).Range.Text
oDoc.Bookmarks("BuildingType").Range.Text = oTable.Cell(1, 2).Range.Text
oDoc.Bookmarks("Level").Range.Text = oTable.Cell(2, 2).Range.Text
oDoc.Bookmarks("Address").Range.Text = oTable.Cell(3, 2).Range.Text
oDoc.Bookmarks("Suburb").Range.Text = oTable.Cell(4, 2).Range.Text
oDoc.Bookmarks("CustNumber").Range.Text = oTable.Cell(3, 4).Range.Text
oDoc.Bookmarks("OrderNumber").Range.Text = oTable.Cell(8, 4).Range.Text
oDoc.Bookmarks("Timeslot").Range.Text = oTable.Cell(1, 4).Range.Text
oDoc.Bookmarks("Comments").Range.Text = oTable2.Cell(4, 2).Range.Text
oDoc.Bookmarks("OrderLinesa").Range.Text = oTable3.Cell(4, 1).Range.Text
oDoc.Bookmarks("OrderLinesb").Range.Text = oTable3.Cell(4, 3).Range.Text
oDoc.Bookmarks("OrderLinesc").Range.Text = oTable3.Cell(4, 5).Range.Text
oDoc.Bookmarks("OrderLinesd").Range.Text = oTable3.Cell(4, 6).Range.Text

mdmackillop
03-04-2006, 02:46 AM
Hi Naigy,
You'll find the Solved button in the Thread Tools drop down at the head of the page.
Regards
MD

naigy
03-04-2006, 06:58 PM
It isn't there. The only options I have are Show Printable Version, Email this thread & Unsubscribe to this thread. I am currently logged in correctly so I have no idea why I can't do this.

mdmackillop
03-04-2006, 07:21 PM
Hi,
Still problems with this due to the upgrade I think. I can see the button but it's non functioning. I've marked this Solved manually.
Regards
MD