none
Windows("filename.docx").Activate works but Windows(<string variable of filename>).Activate does not RRS feed

  • Question

  • I had a bit of code in Word set up to activate a specific .docx file already open and perform some formatting of it:

    Windows("filename.docx").Activate
    'some formatting done here

    This worked, but then I had to expand the code to cover multiple files, so I changed the code by adding an array filled with the file names:

    Dim NameArray(1 to 2) as String
    Dim i as Integer
    
    NameArray(1) = "filename1.docx"
    NameArray(2) = "filename2.docx"
    
    For i = 1 to 2
    Windows(NameArray(i)).Activate
    ' formatting stuff here
    Next i

    This does NOT work.  When I step through the code, I can mouse over the NameArray(i) and it shows the defined name, but the opened document window is never activated and formatted.

    Is there a reason why this isn't working?

    Friday, December 7, 2018 11:28 PM

All replies

  • What does "it does not work" mean exactly?

    I open MS Word and create 2 new files (one saved as filename1.docx and one saved as filename2.docx).  I open the VBA Editor and I have 2 separate projects, one for each filename.

    When I run your code from the first project, it activates filename1.docx and throws an error "the requested member of the collection does not exist" on filename2.docx.  When I run your code from the second project, it throws the same error on filename1.docx and never gets to filename2.docx. The exact same thing happens if you use the explicit string to refer to a document that's outside the scope of the VBA project. 

    So the problem here, unless your project is configured significantly differently and you'd need to spell it out clearly and completely in addition to what you want to accomplish, is that Word Documents are not Excel Worksheets which you might work with this way.  Excel Worksheets are objects in a containing Workbook, which can be referenced "multitudinously" in a single project for that reason and that reason only.  In Excel, the VBA Project belongs to the workbook as a sibling of the Worksheets.  In Word, the VBA Project belongs to the document and having multiple documents open doesn't infer any relationship.


    It never hurts to try. In the worst-case scenario, you'll learn something.

    Saturday, December 8, 2018 12:10 AM
  • Apologies, by "works" or "does not work" I meant activating the target document file.

    If I use the first example, it activates the single, specified Word document as expected and I can perform the desired formatting.

    If I use the second example with multiple document names specified in an array, neither open document file activates.  I just am not sure why that is.

    Monday, December 10, 2018 6:14 PM
  • OK, so then my post applies in total.  It doesn't matter if you specify "filename2.docx" explicitly or by variable/array index.  Try it all 3 ways you'll get the same error.  MS Word is not MS Excel and Word Documents are not WorkSheets.  You shouldn't be able to do the same thing specifying different WorkBooks either, for the same reason.

    Sub WordIsNotExcel()
        Dim NameArray(1 To 2) As String
        Dim i As Integer
        
        ' Touching Windows without a leading Application specifier touches the Windows Collection for the current
        ' Word Instance
        
        ' Only 1 window is ever open per Word Instance
        MsgBox "There is a count of " & Windows.Count & " Windows in use by JUST THIS PARTICULAR INSTANCE of MS Word"
        
        ' Iterate the instance windows
        Dim instWnd As Window
        For Each instWnd In Windows
            MsgBox "There will be one messagebox for each Window in this Instance of MS Word " & vbCrLf & vbCrLf & instWnd.Document.Name
        Next instWnd
        
        MsgBox "That's all the Windows that exist in this instance"
        
        
        
        ' Touching the Application instead of the Instance Windows gives you the number of Windows associated with Word Instances.
        MsgBox "There is a count of " & Application.Windows.Count & " Windows in use by ALL RUNNING INSTANCES of MS Word"
        
        ' Now the Application, not just this Instance
        Dim wordWnd As Window
        For Each wordWnd In Application.Windows
            MsgBox "There will be one messagebox for each Window in each Instance of MS Word " & vbCrLf & vbCrLf & wordWnd.Document.Name
        Next wordWnd
        
        MsgBox "That's all the Windows in all the Instances of MS Word"
        
        '
        ' So as I said before, MS Word is not MS Excel.  A Word Doc open in Word is equivalent to a Workbook, not a WorkSheet,
        ' in Excel.  WorkSheets are siblings of VBA Scripts, Workbooks are parents of both.  You couldn't do the following
        ' against a Workbook either, but you could against Worksheets.
        '
        NameArray(1) = "filename1.docx"
        NameArray(2) = "filename2.docx"
        
        
        ' Error on next line if filename2.docx is open in this instance of MS Word
        Windows("filename1.docx").Activate
        ' Error on next line if filename1.docx is open in this instance of MS Word
        Windows("filename2.docx").Activate
        
        For i = 1 To 2
            ' Error on next line if NameArray(i) is not open in this instance of MS Word
            Windows(NameArray(i)).Activate
            ' formatting stuff here
        Next i
    End Sub


    It never hurts to try. In the worst-case scenario, you'll learn something.

    Monday, December 10, 2018 6:48 PM
  • Ah, I think there was a misunderstanding.  I'm not trying to run the code from either of the open Word document windows.  I have a separate Word document "tool" that has the VBA code to format the other files.

    However, your explanation was helpful because I found that by using the syntax:

    Application.Windows(NameArray(i,1)).Activate

    with the "Application" specifier, it works.  Thank you!

    Monday, December 10, 2018 7:57 PM