Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing Word 1

Status
Not open for further replies.

ZOR

Technical User
Jan 30, 2002
2,963
0
0
GB
Sorry to start new thread, as panicking. I have the code below which opens a word document and puts a table in it. How do I close it after using the close buttons of word, such that I can fire it up again. At the moment I get the messages the table is already in the document (meaning the word application is still open) or The remote server machine does not exist or is unavailable. My original posting is way down the pile and I need to make headway. Thanks
 
Nice thought, but tried setting ObjWord to nothing on clicking the button but it did not help. My code to date is the fourth posting down on this thread. The document is a blank document saved as .Dot (ie Template). The only other code is on another button with lines of exhausted closing statements, all of which were standard, ie Application.Quit, etc. I have not gone into filling the data in the document yet, that funs to come. Would be good to find someone had the time to stick it in and try it.
 
Not sure if you last comments were for what I posted - in case it was

DO NOT set ObjWord to nothing in the same button click - set it in the form unload. Also DO NOT initialize it in the button click - do it in the form load. I have tried the code I mentioned above and it does work
 
Hello shabham, sorry I did not come back, did not see your posting the first time round. I pasted your code in, but it halts at the section of putting in a table saying Object variable or with block variable not set. Its now almost midnight again so I will poke around with it again tommorow. Thanks for your help.
 
Haven't had time to read all the code and therefore come up with a code solution. However, the last few posts should be pointing you in the right direction. The reason (almost certainly) that you are left with Word still in memory is because there are still references to the Word automation object in existence.

Basically, every time you create an object what actually happens is that the reference count to that object is incremented by one (only on the first occassion is the object actually created). Now, each time that you think you are destroying an object (eg set fred=nothing) all you are actually doing is decrementing the reference count. Only when the reference count becomes zero does the object actually get destroyed.

So, if Word (as an automation server) is staying in memory it is because you still have a live reference to it.
 
had a play with your code in 4th post (figured your 2nd post went in there somewhere but havent set about that yet)... i seem to be missing something though. in the command1_click bit is this the bit where it sends the info to word? and subsequently errors next time through??

also in the isprogrun what exactly happens (ie is word app running or not) the second time through??

Sub IsProgRun
If ObjWord is nothing then
Set ObjWord = New Word.Application
Else
Set ObjWord = GetObject(, "Word.Application")
End if
End Sub

when running through this block does it add a new instance of word app in task manager(in addition to any other instances in the manager)??
 
Thanks Adoozer, and Strongm. How do I terminate an instance of automation from memory (am I talking sense). Thanks again, be back tonight.
 
dammit this shouldnt be this hard....

this is what i have so far(based on your code)

'assumes that user will close and save/notsave from word itself

Dim objWord As Word.Application
Dim strWorkName As String

Private Sub AddSelectToFile_Click()

'check to see if word is open
On Error Resume Next
Set objWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
'word not open
Err.Clear
Set objWord = New Word.Application
End If

With objWord
.WindowState = wdWindowStateMaximize
.Documents.Add
End With

strWorkName = objWord.ActiveDocument.Name
Set FormToPrint = objWord.Documents(strWorkName)

objWord.Visible = True

'Make table in her
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=6, NumColumns:=1
Selection.Tables(1).Columns.Width = InchesToPoints(4.5)
Selection.MoveDown Unit:=wdLine, Count:=7
Selection.TypeParagraph
Selection.InsertBreak Type:=wdPageBreak
'should these 2 lines below be here?
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=6, NumColumns:=1
Selection.Tables(1).Columns.Width = InchesToPoints(4.5)

End Sub

Private Sub Form_Unload(Cancel As Integer)

If objWord Is Nothing Then
Cancel = 0
Else
MsgBox &quot;word object still exists.&quot;
Set objWord = Nothing
Cancel = 0
End If

End Sub

problems include...

if word isnt closed then a second copy of word is opened(which shouldnt happen[unless im misunderstanding the getobject call and msdn])

second if the user closes word the next run through the code opens the page but doesnt draw the tables??

im stumped big time now!?!?!?
 
Here is an example of some code that may help you. It only opens a new instance of Word if there isn't already one running. Start new project, add command button to form, add reference for Word then paste the following code into form (have set Word.Visible = true so that you can see it):

'#Start of code
Private Word As New Word.Application

Private Sub Command1_Click()

On Error Resume Next
Set Word = GetObject(, &quot;Word.Application&quot;)
If Err.Number = 429 Then
Set Word = CreateObject(&quot;Word.Application&quot;)
Word.Visible = True
End If

Word.Documents.Add

'# Add your code to amend document here

Word.ActiveDocument.Close

Word.Quit

Set Word = Nothing

End Sub
'#End of code

Hope this helps,
pmrankine
 
genious! i never knew about that.

the second run through still doesnt send the table though!!

Zor: you had any luck yet??
 
Hello Adoozer, no luck yet. MiggyD, I tried that code a while ago, resulted in word being there after clicking the close button, and the message box/word dissapearing only if you click the form again. Pmrankine, that gets rid of the message 429 niceley, however the first time Word opens, its underneath my application out of site (put in Word.Visible=true), and gets its table put in. If I press close, using your other code, it closes, but the next time I click Command1, Word opens on top, but the code inserting a table is ignored OR its another document? The code for closing was the
Word.ActiveDocument.Close
Word.Quit
Set Word = Nothing
which was in your same sub, removed so the user can view the document before its closed. Thanks again.
 
IT WORKS!!!!!!!!!!!

not sure on the whys and wherfors but surrounding the insert code with a with statement &quot;seams&quot; to solve the problem.

ENJOY!!!

Dim WithEvents objWord As Word.Application
Dim strWorkName As String

Private Sub Command1_Click()

'check to see if word is open
On Error Resume Next
Set objWord = GetObject(, &quot;word.application&quot;)
If Err.Number <> 0 Then
'word not open
Err.Clear
Set objWord = New Word.Application
Else
MsgBox &quot;word is already running please shut it down.&quot;, vbOKOnly + vbCritical, &quot;error&quot;
objWord.Visible = True
Exit Sub
End If

With objWord
.WindowState = wdWindowStateMaximize
.Documents.Add
End With

strWorkName = objWord.ActiveDocument.Name
Set FormToPrint = objWord.Documents(strWorkName)

objWord.Visible = True

'Make table in her
With objWord
.ActiveDocument.Tables.Add Range:=.Selection.Range, NumRows:=6, NumColumns:=1
.Selection.Tables(1).Columns.Width = InchesToPoints(4.5)
.Selection.MoveDown Unit:=wdLine, Count:=7
.Selection.TypeParagraph
.Selection.InsertBreak Type:=wdPageBreak
'should these 2 lines below be here?
.ActiveDocument.Tables.Add Range:=.Selection.Range, NumRows:=6, NumColumns:=1
.Selection.Tables(1).Columns.Width = InchesToPoints(4.5)
End With

End Sub

Private Sub Form_Unload(Cancel As Integer)

If objWord Is Nothing Then
Cancel = 0
Else
MsgBox &quot;word object still exists.&quot;
Set objWord = Nothing
Cancel = 0
End If

End Sub

Private Sub objWord_Quit()
Set objWord = Nothing
End Sub
 
Hey IT WORKS. Adoozer, your a friend for life. When I ran it it kept telling me that Word was open, I changed the bit If Err.Number <>0 then to If Err.Number=0, as less or greater than 0 meant it was open. Very,Very greatful to you for sorting it out. pmrankine's code could have been working, but for some reason Word opened underneath my application, and on occasion did not create the table. It might have been due to me not always closing, and it being a copy document. Anyway I am jumping around very happy. I will now try and see if I can add the code to close word if it finds it open. But the main thing its fixed, thanks.
 
:) believe me ive answered more of my own questions about excel/word during these past few nights so it was worthwhile all round, and im quite chuffed too.

one question now it seems to be over, what does the following line do?

Set FormToPrint = objWord.Documents(strWorkName)

i searched for formtoprint but cant get any info.

 
I don't truthfully know, however might be able to make a guess, as it was used in an example of someone elses. FormToPrint was Dim FormToPrint as Word.Document. The strWorkName was Dim as String, was set as the name of the active document opened. Thats all I know. Anyway who cares, you did it and thanks again. Don't know if I get a prize for keeping a thread open so long hey?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top