Closing a form and closing Word
Author |
Message |
ibby #1 / 8
|
 Closing a form and closing Word
Hi Russell, I don't get the error when I try your code (462 is an automation error). However, I can see two probems with the code - you are closing the document *then* hiding the userform *then* closing Word. When you close the document, the form is unloaded and the rest of the code doesn't run. Secondly, you are not explicitly unloading the userform - you are just hiding it, so it may still be loaded in memory when you are trying to close Word. Try splitting up your code as follows: ---------------------------------------------------------------------------- - Private Sub cmdExit_Click() Dim intResponse As Integer intResponse = MsgBox("Exit without creating cover sheet?", _ vbYesNo + vbQuestion + vbDefaultButton2, "S{*filter*}the cover sheet?") If intResponse = vbYes Then Call ExitWord Else cmdCreate.SetFocus Exit Sub End If End Sub ---------------------------------------------------------------------------- - Then in a normal code module: ----------------------------------------------------------------- Public Sub ExitWord() Application.StatusBar = "" Unload frmSettings ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges Application.Quit End Sub ----------------------------------------------------------------- Hope this helps. ibby Please post replies or follow-ups to the **newsgroup** so that participants may benefit or contribute.
Quote: > I'm running a template that starts a user form. If the user clicks on EXIT, > then I want the form to close and the instance of Word to close. > What I'm getting when the user clicks EXIT is runtime error 462. When I click > on OK of the error box, it goes away and the instance of Word along with the > form is closed, which is what I want. I really don't care about the error, I > just want the error box to go away. I tried to trap the error but couldn't get > it to work. Here is the exit code: > Private Sub cmdExit_Click() > Dim intResponse As Integer > intResponse = MsgBox("Exit without creating cover sheet?", _ > vbYesNo + vbQuestion + vbDefaultButton2, "S{*filter*}the cover sheet?") > If intResponse = vbYes Then > Application.StatusBar = "" > ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges > frmSettings.Hide > Application.Quit > Else > cmdCreate.SetFocus > Exit Sub > End If > End Sub
|
Thu, 10 Apr 2003 22:56:58 GMT |
|
 |
ibby #2 / 8
|
 Closing a form and closing Word
Hi again Russell, As I said, I don't get the error when I try it - Word97 SR2 on Win98. The next thing to try is to remark out all the lines of code in the ExitWord sub, run it and see if you get the error. If not, unremark each line and try again, starting with the first line of code. This should narrow down the problem. You may have to place a DoEvents statement before the problem line of code. -- Hope this helps. ibby Please post replies or follow-ups to the **newsgroup** so that participants may benefit or contribute.
Quote: > Sorry ibby for replying to your email. It was an accident. > Thank you for the response ibby, I split up the code like you suggested but I > still get the 462 error. This is the exact wording of the error: > Run-time error 462, The remote server machine does not exist or is unavailable > I don't get the alarm when I debug line by line. It closes and everything is > cleaned up. If I run it normally, then I get the alarm. Strange. > >Hi Russell, > >I don't get the error when I try your code (462 is an automation error). > >However, I can see two probems with the code - you are closing the document > >*then* hiding the userform *then* closing Word. When you close the document, > >the form is unloaded and the rest of the code doesn't run. Secondly, you are > >not explicitly unloading the userform - you are just hiding it, so it may > >still be loaded in memory when you are trying to close Word. Try splitting > >up your code as follows: >--------------------------------------------------------------------------- - > >- > >Private Sub cmdExit_Click() > > Dim intResponse As Integer > > intResponse = MsgBox("Exit without creating cover sheet?", _ > > vbYesNo + vbQuestion + vbDefaultButton2, "S{*filter*}the cover sheet?") > > If intResponse = vbYes Then > > Call ExitWord > > Else > > cmdCreate.SetFocus > > Exit Sub > > End If > >End Sub >--------------------------------------------------------------------------- - > >- > >Then in a normal code module: > >----------------------------------------------------------------- > >Public Sub ExitWord() > >Application.StatusBar = "" > >Unload frmSettings > >ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges > >Application.Quit > >End Sub > >----------------------------------------------------------------- > >Hope this helps. > >ibby > >Please post replies or follow-ups to the **newsgroup** so that participants > >may benefit or contribute.
> >> I'm running a template that starts a user form. If the user clicks on > >EXIT, > >> then I want the form to close and the instance of Word to close. > >> What I'm getting when the user clicks EXIT is runtime error 462. When I > >click > >> on OK of the error box, it goes away and the instance of Word along with > >the > >> form is closed, which is what I want. I really don't care about the > >error, I > >> just want the error box to go away. I tried to trap the error but > >couldn't get > >> it to work. Here is the exit code: > >> Private Sub cmdExit_Click() > >> Dim intResponse As Integer > >> intResponse = MsgBox("Exit without creating cover sheet?", _ > >> vbYesNo + vbQuestion + vbDefaultButton2, "S{*filter*}the cover sheet?") > >> If intResponse = vbYes Then > >> Application.StatusBar = "" > >> ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges > >> frmSettings.Hide > >> Application.Quit > >> Else > >> cmdCreate.SetFocus > >> Exit Sub > >> End If > >> End Sub
|
Fri, 11 Apr 2003 10:55:08 GMT |
|
 |
Russell Clayto #3 / 8
|
 Closing a form and closing Word
I'm using the template on my PC at work and I'm getting the same error. Maybe it's something earlier in my code that's causing this. This template extracts fields from ACT! I've used this template for a long time. In ACT!, I have a custom command that runs a compiled VB file that runs the template. Here is the code from the VB executable that runs from ACT!. Private Sub Form_Load() On Error GoTo ICanHandleThis Form1.Visible = False Dim objWord As Word.Application Dim objDoc As Word.Document If objWord Is Nothing Then Set objWord = New Word.Application Set objDoc = objWord.Documents.Add("C:\NS Templates\NSFax.dot") Else Set objDoc = objWord.Documents.Add("C:\NS Templates\NSFax.dot") End If objWord.Visible = True Unload Me Exit Sub ICanHandleThis: Set objDoc = Nothing objWord.Quit Set objWord = Nothing MsgBox "Something stinks here..Contact " _ & "System Administrator", vbCritical, "Template failed to load" Unload Me End Sub
|
Fri, 11 Apr 2003 03:00:00 GMT |
|
 |
ibby #4 / 8
|
 Closing a form and closing Word
Russell, I tried it on Word2000 SR1 and still no error here. Try the following (leaving out the ActiveDocument.Close line): Public Sub ExitWord() Application.StatusBar = "" Unload frmSettings Application.Quit SaveChanges:=wdDoNotSaveChanges End Sub -- Hope this helps. ibby Please post replies or follow-ups to the **newsgroup** so that participants may benefit or contribute.
Quote: > Thanks ibby for the response. I tried the DoEvents with the same result. > For anybody else I'm running Word 2000 (9.0.411 SR-1) The error definitely > occurs on Application.Quit. (run-time error 562, The remote server machine does > not exist or is unavailable) > If I rem out Application.Quit and run the code, no error but it leaves Word > open. When I run the code with Application.Quit, Word closes but I get the 562 > error. Click OK on the error box and it goes away. I don't know how to trap it > since Word is already closed. > Private Sub cmdExit_Click() > Dim intResponse As Integer > intResponse = MsgBox("Exit without creating cover sheet?", _ > vbYesNo + vbQuestion + vbDefaultButton2, "S{*filter*}the cover sheet?") > If intResponse = vbYes Then > Call ExitWord > Else > cmdCreate.SetFocus > Exit Sub > End If > End Sub > -------------------------------------------------- > Then in a normal code module: > --------------------------------------------------- > Public Sub ExitWord() > Application.StatusBar = "" > Unload frmSettings > ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges > Application.Quit > End Sub > ----------------------------------------------------- > >Hi again Russell, > >As I said, I don't get the error when I try it - Word97 SR2 on Win98. The > >next thing to try is to remark out all the lines of code in the ExitWord > >sub, run it and see if you get the error. If not, unremark each line and try > >again, starting with the first line of code. This should narrow down the > >problem. You may have to place a DoEvents statement before the problem line > >of code.
|
Fri, 11 Apr 2003 22:07:40 GMT |
|
 |
ibby #5 / 8
|
 Closing a form and closing Word
Russell, Quote: > I'm using the template on my PC at work and I'm getting the same > error. Maybe it's something earlier in my code that's causing this.
Yes, I'd say so ;-) Quote: > This template extracts fields from ACT! I've used this template for a > long time. In ACT!, I have a custom command that runs a compiled VB > file that runs the template. Here is the code from the VB executable > that runs from ACT!.
The problem is the "objWord.Visible = True" line. What is happening is: - You run the VB executable - the way your code is, a New instance of Word is *always* created (is this what you want ?) - This creates a new document based on the template and the userform is shown. The code in the VB executable has stopped executing just before the "objWord.Visible = True" line. - Now, in the UserForm, you press the commandbutton that closes the Word application -> the Word application is closed and control returns to the VB executable. - Now the "objWord.Visible = True" runs, but it cannot find the Word Application because it is already closed -> hence the error. What are you actually trying to do in the VB exe ie: - Do you always want to create a new instance of Word, or do you want to use a running instance if it exists ? - Why are you putting the code in the VB form - you don't seem to be doing anything with the form. You can place the code in VB in a normal code module in a procedure named Main and set the properties of the project to run Main at startup. Post a reply to the newsgroup and I'll give you instructions about how to modify your VB program. -- Hope this helps. ibby Please post replies or follow-ups to the **newsgroup** so that participants may benefit or contribute. Quote: > Private Sub Form_Load() > On Error GoTo ICanHandleThis > Form1.Visible = False > Dim objWord As Word.Application > Dim objDoc As Word.Document > If objWord Is Nothing Then > Set objWord = New Word.Application > Set objDoc = objWord.Documents.Add("C:\NS > Templates\NSFax.dot") > Else > Set objDoc = objWord.Documents.Add("C:\NS > Templates\NSFax.dot") > End If > objWord.Visible = True > Unload Me > Exit Sub > ICanHandleThis: > Set objDoc = Nothing > objWord.Quit > Set objWord = Nothing > MsgBox "Something stinks here..Contact " _ > & "System Administrator", vbCritical, "Template failed to > load" > Unload Me > End Sub
|
Sat, 12 Apr 2003 03:00:00 GMT |
|
 |
Russell Clayto #6 / 8
|
 Closing a form and closing Word
Quote: > > I'm using the template on my PC at work and I'm getting the same > > error. Maybe it's something earlier in my code that's causing this. > Yes, I'd say so ;-) > > This template extracts fields from ACT! I've used this template for a > > long time. In ACT!, I have a custom command that runs a compiled VB > > file that runs the template. Here is the code from the VB executable > > that runs from ACT!. > The problem is the "objWord.Visible = True" line. What is happening is: > - You run the VB executable - the way your code is, a New instance of Word > is *always* created (is this what you want ?) > - This creates a new document based on the template and the userform is > shown. The code in the VB executable has stopped executing just before the > "objWord.Visible = True" line. > - Now, in the UserForm, you press the commandbutton that closes the Word > application -> the Word application is closed and control returns to the VB > executable. > - Now the "objWord.Visible = True" runs, but it cannot find the Word > Application because it is already closed -> hence the error. > What are you actually trying to do in the VB exe ie: > - Do you always want to create a new instance of Word, or do you want to > use a running instance if it exists ?
I want to use a running instance if Word is already running. If it's not running then start it. Quote: > - Why are you putting the code in the VB form - you don't seem to be > doing anything with the form. You can place the code in VB in a normal code > module in a procedure named Main and set the properties of the project to > run Main at startup.
I don't know why I used a form instead of a normal module. Quote: > Post a reply to the newsgroup and I'll give you instructions about how to > modify your VB program. > -- > Hope this helps. > ibby
Thank you ibby!
|
Sat, 12 Apr 2003 03:00:00 GMT |
|
 |
ibby #7 / 8
|
 Closing a form and closing Word
Hi again Russell, Quote: > I want to use a running instance if Word is already running. If it's > not running then start it. > I don't know why I used a form instead of a normal module.
Alright then. - In your VB project, insert a Module - Project | Add Module - In this module, paste the Sub Main I've pasted at the end of this post - Make sure you've set a reference to the Word Ojbect Library - Right-click on the project in Project Explorer and select Project Properties. On the General tab, under Startup Object, select Sub Main - this way, when you compile and create an executable, Sub Main will run first - you can right-click on the Form and delete it (it's not needed). - In your Word template, use the code I posted earlier. - Create the VB executable and test it out. - One thing you may want to consider - do you really want to close Word if you are using a previously open instance - the user may have been working on some document and will lose any unsaved changes. -- Hope this helps. ibby Please post replies or follow-ups to the **newsgroup** so that participants may benefit or contribute. ------------------------------------------------------------ Public Sub Main() On Error Resume Next Dim objWord As Word.Application Dim objDoc As Word.Document ' Get running instance of Word. If there isn't one, ' an error will be generated Set objWord = GetObject(, "Word.Application") ' If error ie: no running instance of Word, ' create new instance If Err.Number > 0 Then Set objWord = New Word.Application objWord.Visible = True Err.Clear End If On Error GoTo ICanHandleThis objWord.Activate Set objDoc = objWord.Documents.Add _ ("C:\NS Templates\NSFax.dot") Exit Sub ICanHandleThis: Set objDoc = Nothing objWord.Quit Set objWord = Nothing MsgBox "Something stinks here..Contact " _ & "System Administrator", vbCritical, _ "Template failed to Load " End Sub ------------------------------------------------------------
|
Sun, 13 Apr 2003 13:27:46 GMT |
|
 |
Russell Clayto #8 / 8
|
 Closing a form and closing Word
Once again, many thanks to you for taking time to help me with this. Quote: > - In your VB project, insert a Module - Project | Add Module > - In this module, paste the Sub Main I've pasted at the end of this post > - Make sure you've set a reference to the Word Ojbect Library > - Right-click on the project in Project Explorer and select Project > Properties. On the General tab, under Startup Object, select Sub Main - this > way, when you compile and create an executable, Sub Main will run first - > you can right-click on the Form and delete it (it's not needed). > - In your Word template, use the code I posted earlier. > - Create the VB executable and test it out. > - One thing you may want to consider - do you really want to close Word if > you are using a previously open instance - the user may have been working on > some document and will lose any unsaved changes. > -- > Hope this helps. > ibby > Please post replies or follow-ups to the **newsgroup** so that participants > may benefit or contribute. > ------------------------------------------------------------ > Public Sub Main() > On Error Resume Next > Dim objWord As Word.Application > Dim objDoc As Word.Document > ' Get running instance of Word. If there isn't one, > ' an error will be generated > Set objWord = GetObject(, "Word.Application") > ' If error ie: no running instance of Word, > ' create new instance > If Err.Number > 0 Then > Set objWord = New Word.Application > objWord.Visible = True > Err.Clear > End If > On Error GoTo ICanHandleThis > objWord.Activate > Set objDoc = objWord.Documents.Add _ > ("C:\NS Templates\NSFax.dot") > Exit Sub > ICanHandleThis: > Set objDoc = Nothing > objWord.Quit > Set objWord = Nothing > MsgBox "Something stinks here..Contact " _ > & "System Administrator", vbCritical, _ > "Template failed to Load " > End Sub
|
Sun, 13 Apr 2003 03:00:00 GMT |
|
|
|