none
Activate Word Document using VBA RRS feed

  • Question

  • I'm using a 1) document which contains a form which allows the select different to create 2) another document based on a template.  After the document is created, the documents that contains the form, is closed and I would like the new document (2) to be the active document.  The code below works only when there are no other documents open when the code runs, but if there is at least 1 document open, the focus is set to that document and not the one I would like to get the focus... Any help?

    Thanks

    Sub OpenDoc()
    Dim doc As Document
    Dim Answer As Long
    Dim strString As String
    Dim strPath As String
    Dim strFile As String
    Dim WshShell As Object
        Set doc = ActiveDocument
        Set WshShell = CreateObject("WScript.Shell")
        strPath = WshShell.SpecialFolders("MyDocuments") & Application.PathSeparator
        strFile = "MasterDocument.docm"
        strString = "Would you like to open the file '" & strFile & "'?"
        Answer = MsgBox(strString, vbQuestion + vbOKCancel)
       
        If Answer = vbOK Then
            Documents.Add (strPath & strFile)
            Documents(Documents.Count).Activate
            doc.Close wdDoNotSaveChanges
        End If
        If Answer = vbCancel Then
        End If
    End Sub

    Saturday, June 18, 2016 12:03 AM

Answers

  • With the code in my previous post, the added document is switched to and activated before the first document is closed. The problem you're most likely seeing is that, when you close the document running the macro, Word switches the active document. To leave the last-opened document active, you could try:

    Sub OpenDoc()
    Dim doc As Document
    Dim Answer As Long, WshShell As Object
    Dim strString As String, strPath As String, strFile As String
     
        Set doc = ActiveDocument
        Set WshShell = CreateObject("WScript.Shell")
        strPath = WshShell.SpecialFolders("MyDocuments") & Application.PathSeparator
        strFile = "MasterDocument.docm"
        strString = "Would you like to open the file '" & strFile & "'?"
        Answer = MsgBox(strString, vbQuestion + vbOKCancel)
       
        If Answer = vbOK Then
            Documents.Add (strPath & strFile)
            Windows(Windows.Count).Activate
            doc.Close wdDoNotSaveChanges
        End If
    Set doc = Nothing
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by B121WYHWH Sunday, June 19, 2016 7:55 PM
    Saturday, June 18, 2016 5:20 AM

All replies

  • Try:

    Sub OpenDoc()
    Dim doc1 As Document, doc2 As Document
    Dim Answer As Long, WshShell As Object
    Dim strString As String, strPath As String, strFile As String
     
        Set doc1 = ActiveDocument
        Set WshShell = CreateObject("WScript.Shell")
        strPath = WshShell.SpecialFolders("MyDocuments") & Application.PathSeparator
        strFile = "MasterDocument.docm"
        strString = "Would you like to open the file '" & strFile & "'?"
        Answer = MsgBox(strString, vbQuestion + vbOKCancel)
       
        If Answer = vbOK Then
            Set doc2 = Documents.Add(strPath & strFile)
            doc2.Activate
            doc1.Close wdDoNotSaveChanges
        End If
    Set doc1 = Nothing: Set doc2 = Nothing
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, June 18, 2016 2:06 AM
  • Thanks Paul, that works only if there are no other documents open before running the code.  However, if there is one (or any number of) document(s) open, doc2 doesn't activate... the doc n-1 activates...

    Rgds,

    FdR

    Saturday, June 18, 2016 3:59 AM
  • With the code in my previous post, the added document is switched to and activated before the first document is closed. The problem you're most likely seeing is that, when you close the document running the macro, Word switches the active document. To leave the last-opened document active, you could try:

    Sub OpenDoc()
    Dim doc As Document
    Dim Answer As Long, WshShell As Object
    Dim strString As String, strPath As String, strFile As String
     
        Set doc = ActiveDocument
        Set WshShell = CreateObject("WScript.Shell")
        strPath = WshShell.SpecialFolders("MyDocuments") & Application.PathSeparator
        strFile = "MasterDocument.docm"
        strString = "Would you like to open the file '" & strFile & "'?"
        Answer = MsgBox(strString, vbQuestion + vbOKCancel)
       
        If Answer = vbOK Then
            Documents.Add (strPath & strFile)
            Windows(Windows.Count).Activate
            doc.Close wdDoNotSaveChanges
        End If
    Set doc = Nothing
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by B121WYHWH Sunday, June 19, 2016 7:55 PM
    Saturday, June 18, 2016 5:20 AM
  • Paul thanks for your support, same result: I can't get the new document to becomes the active, active document is the last-opened document active......

    Best regards,

    FdR

    Saturday, June 18, 2016 4:36 PM
  • In my testing, the last set of code I posted leaves the document opened by the macro as the active document, regardless of whether the active document the macro is run from is the first, last, or anywhere in between, of multiple documents that might be open when it's run.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, June 18, 2016 5:06 PM
  • Thanks Paul
    Sunday, June 19, 2016 7:55 PM