none
Work with files in two different folders RRS feed

  • Question

  • Hi,

    I'm running into an "Error: (5941) The requested member of the collection does not exist."  Error when I am running a program.  In this program, I am working with two different files, which I selected (so they both exist), and are called "filePath" and "filePath2".  When I try to activate the second file (using Windows(filePath2).Activate), I get the error.  The program works perfectly when the files are located in the same folder, but get the error when the files are located in different folders.  Do you have any idea why this would be happening, and if there is a workaround?

    Thanks!

    Josiah

    Wednesday, August 14, 2013 6:08 PM

Answers

  • Hi Josiah

    Usually, I'd assign a Document object to each file when I open it. That way, I can work with the object, directly, rather than having to worry about files names or windows...

    Dim doc1 As Word.Document
    Dim doc2 As Word.Document

    Set doc1 = Documents.Open("pathandfilename")
    Set doc2 = Documents.Open("otherpathandfilename")

    doc1.Activate
    doc2.Activate


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, August 21, 2013 11:05 AM
    Moderator
  • Oh, I see.

    I haven't recognise the code in you post at the first sight, since I don't usually use the Window Object.

    In your case, Windows(filePath) could not work because the index is actually the caption of that window.

    When you open C:\Document1.docx, the caption could be "Document1.docx - Microsoft Word" or perhaps "Document1.docx [Compatibility Mode] - Microsoft Word". It's not so reliable to use a string which was generated based on the document name.

    I'd suggest you use the code below.

    Application.Documents(fileName).Activate

    'or

    Document.Activate

    'If you have declared Document variables for each document before.

    Hope it helps.

    Quist
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, August 16, 2013 3:24 AM
    Moderator

All replies

  • Hi Josiah,

    I cannot see how your issue is related to Word Development. Could you please provide more detailed infomation?

    Regards,


    Quist
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, August 15, 2013 9:18 AM
    Moderator
  • Sorry! It's related to Word Development because I am coding for Word.
    Thursday, August 15, 2013 1:33 PM
  • Oh, I see.

    I haven't recognise the code in you post at the first sight, since I don't usually use the Window Object.

    In your case, Windows(filePath) could not work because the index is actually the caption of that window.

    When you open C:\Document1.docx, the caption could be "Document1.docx - Microsoft Word" or perhaps "Document1.docx [Compatibility Mode] - Microsoft Word". It's not so reliable to use a string which was generated based on the document name.

    I'd suggest you use the code below.

    Application.Documents(fileName).Activate

    'or

    Document.Activate

    'If you have declared Document variables for each document before.

    Hope it helps.

    Quist
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, August 16, 2013 3:24 AM
    Moderator
  • Hi Josiah

    Usually, I'd assign a Document object to each file when I open it. That way, I can work with the object, directly, rather than having to worry about files names or windows...

    Dim doc1 As Word.Document
    Dim doc2 As Word.Document

    Set doc1 = Documents.Open("pathandfilename")
    Set doc2 = Documents.Open("otherpathandfilename")

    doc1.Activate
    doc2.Activate


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, August 21, 2013 11:05 AM
    Moderator