Results 1 to 11 of 11
  1. #1
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Closing wrord if open (MSACCESS2003)

    I am opening Word from a command button on a form. However if the user minimises Word, and presses the button again,a new instance of Word is generated. I want to check if Word is already open, if it is then close it before opening up a new instance.

    I tried this in the click event, it does not work, as it's asking to create a new instance. I tried just using WD.Application.Quit
    but it complained it was not set. Any ideas? Thanks


    Set WD = CreateObject(Class:="Word.Application")
    WD.Application.Quit
    Set WD = Nothing

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Closing Word if open (MSACCESS2003)

    <P ID="edit" class=small>(Edited by jscher2000 on 08-Feb-09 10:38. )</P>How about re-using the already open instance of Word? You can try GetObject first and, if that fails, then use CreateObject. This requires some special error handling. See the following post for an example: <post#=446,288>post 446,288</post#>. Does that work for you?

  3. #3
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Closing Word if open (MSACCESS2003)

    Many thanks. I was playing around with the code below do not know if I could use it? I only seemed to get two error numbers to deal with in running the db. Is it viable? Thanks

    Private Sub Command276_Click()

    On Error GoTo Command276_Click_Error

    WD.Application.Quit
    MsgBox "Its now closed"
    On Error GoTo 0
    Exit Sub

    Command276_Click_Error:
    If Err.Number = 462 Then
    MsgBox "Its not open at present"
    Else
    If Err.Number = 91 Then
    MsgBox "Its not open at present"
    End If: End If


    End Sub

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Closing Word if open (MSACCESS2003)

    Why not use the code from the post Jefferson referred to?

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Closing Word if open (MSACCESS2003)

    I'm not sure what you are trying to do with your code.

    I think it is safer to try to re-use an open session of Word than to hunt for open instances of Word and close them (if you cannot be 100% certain they were associated with your application and there will be no loss of work). Either way, you could encounter problems, but the latter approach seems to have a higher risk of irate users. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  6. #6
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Closing Word if open (MSACCESS2003)

    Many thanks both, yes I will adopt the suggested code, and thenls for pointing out the value towards making sure its the correct instance of word being looked for. The code I was working with was basically trying to make use of error codes to establish whether word was open or not. Best regards, thanks again.

  7. #7
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Closing Word if open (MSACCESS2003)

    I tried using this, as is. It halts on the line GetObject, and says library not registered? I have Word 11 Objects in my references?

    Private Sub Command283_Click()
    Dim wrdApp As Word.Application
    Dim blnStartWord As Boolean

    ' See if Word is already active
    Set wrdApp = GetObject(, "Word.Application")
    If wrdApp Is Nothing Then
    ' No, so start a new instance
    Set wrdApp = CreateObject("Word.Application")
    If wrdApp Is Nothing Then
    ' Failed
    MsgBox "Cannot activate Word."
    Exit Sub
    End If
    blnStartWord = True
    End If


    End Sub


    Basically I want to test if Word has already been opened, if so close it before I create a new instance.

    My instance of word being opened is:

    LWordDoc = SelectedDoc

    'Create an instance of MS Word
    Set WD = CreateObject(Class:="Word.Application")
    ' WD.Visible = True
    '
    ' 'Open the Document
    WD.Documents.Open Filename:=LWordDoc, ReadOnly:=True

    Last night my PC took 15minutes to close down while it closed numerous word documents left open. They had been set visible=false so they were not obvious in the taskbar. Thanks

  8. #8
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Closing Word if open (MSACCESS2003)

    I'm not sure why you got that message: GetObject is part of the standard VBA library.

    Also, you removed the On Error Resume Next, which is needed to bypass an error when GetObject fails. Please see the original code example for more guidance on the error handling.

  9. #9
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Closing Word if open (MSACCESS2003)

    <hr>Basically I want to test if Word has already been opened, if so close it before I create a new instance.<hr>
    GetObject will get an instance of Word if it exists. My concern is that the user might have her or his own instance of Word running, so you might need to take precautions not to close the "wrong" one. Although you can test whether the Word.Application object is visible, if it is, and therefore you don't want to close that one, GetObject cannot help you find other instances of Word. For that, you would need some Windows API code, and unfortunately I was not able to turn it up in a quick search.

  10. #10
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Closing Word if open (MSACCESS2003)

    Many thanks. I do see the seriousness of making sure I am looking for and closing the document relevant to my program, and not closing another document in use by the user. Its a shame I cannot use my simple procedure which seems to work without fail or errors being highlighted:

    Public Sub CheckForWord()

    On Error GoTo CheckForWord_Error
    WD.Application.Quit

    On Error GoTo 0
    Exit Sub

    CheckForWord_Error:
    If Err.Number = 462 Or Err.Number = 91 Then
    Exit Sub
    End If

    End Sub

    I did a lot of testing and the two error numbers were the only ones it through up. I don't know if catching errors this way is proper. Does WD not tell it what to look for? I will also check out any API's Thanks again

  11. #11
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Closing Word if open (MSACCESS2003)

    I think the question is, "what is WD" when your code reaches the line WD.Application.Quit. If you are just firing up with procedure, WD might be "Nothing." To store a persist object reference, you could try declaring a public or private variable. If you highlight Public and press F1, you can get the VB help on public variables; you also could look at private variables. Note that these need to be declared outside of any procedure (not between Sub and End Sub or Function and End Function), and the convention is to declare them at the top of the module.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •