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
 
Sorry, in a hurry forgot code.



Set WP = New Word.Application
WP.Documents.Add
WP.Visible = True


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
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=6, NumColumns:=1
Selection.Tables(1).Columns.Width = InchesToPoints(4.5)
 
Thanks John, I'm still not htere. I keep getting the error 462 regarding the remote server does not exist. I'm on a standalone pc. Tried the following code below but still no luck

Private Sub Command1_Click()
Dim strWorkName As String

IsProgRun

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

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

ObjWord.Visible = True

'Make table in here

end sub


Sub WordClose_Click
ObjWord.ActiveDocument.Close
ObjWord.Quit
Set ObjWord = Nothing
end sub

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




All I am trying to achieve is having a button that when pressed opens a word document and fill it. The user can then do what he likes, ie save,print etc from the normal word application. When he closes it, it closes. If he hits the button again, the process repeats itself. So basic, but a real headache especially when time is against one.
Many thanks
 
johnyingling:

>ActiveDocument.Close ' Might need True to Save Changes without Prompt

how exactly do you do this(the true bit)... is it as simple as: -

ActiveDocument.Close=true???

does it work in excel???

my related thread...
thread222-471548
 
At my wits end now. Sometimes more than often, the word document appears beneath my application form so it cannot be seen. What kind of ---- is automation. I have to see someone tommorow to say I can or cannot do a simple task!!
I'm not trying to get to the moon. Just want to open an empty word doc, put some text on it from the vb application. The user then does what he wants, ie save, print, abort. When he closes it, he can repeat the operation, ie open/close/open/close. Really in a mess, any real direction appreciated. Thanks. 12.00 midnight
 
zor: at which point are you getting the error? (or did you fix the error....

if i can pass on any fumbling error/corrections i will...

we can beat this :)
 
damn the tab key.....

from what i understand: your opening word, copying something then offering focus to word at which point the user does as he/she pleases.... this is where you loose me
>When he closes it, he can repeat the operation, ie open/close/open/close
 
Zor ... you say"I have to see someone tommorow to say I can or cannot do a simple task!!" Tell them absolutly YES!YOU CAN!
It has been a long time since I have written to the Word.Application object, but anyone who tells you that is easy is either a much better programmer than I am, or they do it daily! It is NOT a simple, straight forward task, as you will learn when you try to format certain portions of text to be 10 point "times New Roman" and another portion to be "Arial 12 Point bold" ... especially if you need to do it on the same line!!! :-( Believe me, it is possible, but it takes alot of trila nad error... plus a few pints of beer and sleepless nights!
As for the problem you are now having.. you have a reference to the some portioin ofg the Word object still active and/or alive. Remember that when you open Word all on its' own .. it automatically adds a new word document... In your code your are using ".Documents.Add".. i suspect that you actually end up with 2 word.documents.. the one you are programming to.. and the one that Word automatically creates... SO Word is sitting there wondering what to do with that one document.. that contains nothing!
Look into the VB Object Browser ... and in the VB Debug windows ... somewhere there is something staying open.

What I typically do is only close the Word.Document(s) object(s).. leaving the Word.App open. I only close it when i close the VB program...(explain to the client you do that for faster function). Also, write an error handler to deal with that automation error...
If err.Number = ***(whatever your code is) then
... work through each Word object you have referenced..
Close them.. then set them = Nothing .. then recall your "Is program running" routine... check the error code again .. if it is 0 then show your word.document..
Play with those thoughts a little bit and the answer will become very appearant to you.
Good luck .... you can do this ..
Michael

 
Thanks Micheal, I think you could be correct, I also have suspicion of two word documents being there. I will try and go through it tonight when I get back. When leaving off last night, had a problem in that word refused to open on top of the application. Had to turn the pc off before it came back again. Thanks for your advice. Re Adoozer, my application allows a user to pick names from a database, and put into a word document. They then see the list put into word upon which they can either print/save the word document, or close it to go back to the application to add, remove names they selected in the first place, and then reopen word showing the amended list. Many thanks
 
Still getting nowhere!! I will try and describe what I am trying to do.

VB application is a database
A user selects a list of names to go in a listbox.
He presses a button, word opens, and the names put into wordtable.

Word when opened is maximized. The user closes it, getting a prompt to save the document. At this stage he answers no.
Word then appears to go away/closed. However the next time he hits the button, Word had not closed, because a message comes up a table is already in the destination document. Or a runtime error 462 appears.

If I do the same operation without making any changes (ie not putting in a word table), the same document Document1 appears each time without any problems.

Somebody must know how to terminate a dirty document/application so it can be called again? New code below avoids the .add statement.

Private Sub Command1_Click()
Dim ObjWord As Word.Application
Dim objWordDoc As Word.Document

Set ObjWord = CreateObject("Word.Application")

Set objWordDoc = ObjWord.Documents.Open("G:\NEWEASTENDERS\EASTEND2.DOT")

ObjWord.Visible = True
ObjWord.WindowState = wdWindowStateNormal

' Start by putting in a table
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=6, NumColumns:=1
Selection.Tables(1).Columns.Width = InchesToPoints(4.5)


How do I KILL THE DOCUMENT AFTERWARDS AND QUIT WORD READY FOR NEXT TIME - Simple to someone
 
Try this to close without saving dirty doc:

WP.Documents(1).Close (wdDoNotSaveChanges)
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
No it did not work.
ObjWord(1).Close(wdDoNotSave) produced an error 5941 saying The requested member of collection does not exist. I tried getting the ObjWord.Count and put that in, but same result.
If I leave out (1) I get error 4605 This method or property is not available because a document window is not active.

But thanks for the suggestion
 
im really confused but im gonna give it a try.

>appears to go away/closed

>Word had not closed, because a message comes up a table is already in the destination document.

is word open? (ie is it in task manager) [i ask coz not everyone checks]...
 
Yes it stays open, because if I look underneath my application it's there. If I don't do anything with the document, ie no table insertion, text, it does seem to close. Having so little code I'm surprised it's such a headache and nobody else has hit the nail. If you use the close box on word itself, it asks if you want to save the document and seems to do little towards ending the session. I moved away from the .add statement thinking like Michael suggested 2 documents were present, one closing and the other hanging around. Soon I'm going to chuck it in, I'm wasting so much time. I downloaded examples from planet, but that still produced the error 462 of the server not being available. I don't like wasting spsce on the forum so unless I get a direct hit on how to solve/do what I want then its time for a space. Thanks again
 
I checked with task manager, and Winword appears in the list even when the application looks like it's gone.
Project1
Explorer
Winword
etc
Sometimes there are muliple lines of Winword, so its not going away. PC's going through the window soon.
 
if its not too much to ask... could you post the code in its entirity or mail it to me at pingu_2_k_2@hotmail.com [or both] (plus assosiated files). [i know its not tek-tips etiquete(ooo my spellling bad) but im kinda hooked now]
 
when i had multiple instances of excel in task manager it was because i wasnt setting "myexcelobject" to nothing. could you not set the wordobject to nothing when your app regains focus?

"clutching at straws!"
 
Try this -
the declarations need to go into the form's general declarations part. You can either remove the code to move to the end of the document and add a line (Selection.EndKey Unit:=wdStory
Selection.TypeParagraph
) and close the document without saving

OR - you can keep those 2 lines and close the document. This will keep adding tables to the end of the file -
Good luck



Private ObjWord As Word.Application
Private objWordDoc As Word.Document

Private Sub Command1_Click()

Set objWordDoc = ObjWord.Documents.Open("d:\EASTEND1.DOc")

ObjWord.Visible = True
ObjWord.WindowState = wdWindowStateNormal

' Start by putting in a table
Selection.EndKey Unit:=wdStory
Selection.TypeParagraph
objWordDoc.Tables.Add Range:=Selection.Range, NumRows:=6, NumColumns:=1
Selection.Tables(1).Columns.Width = InchesToPoints(4.5)

ObjWord.ActiveDocument.Close '(wdDoNotSaveChanges)

ObjWord.Visible = False

End Sub

Private Sub Form_Load()
Set ObjWord = New Word.Application
End Sub

Private Sub Form_Unload(Cancel As Integer)
ObjWord.Quit
Set objWordDoc = Nothing
Set ObjWord = Nothing

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top