Problem using a URL as index for the Documents collection

A poster in the Word for Developers forum has brought a new problem with the Documents collection to light this week. Unlike the one discussed in an earlier blog post, this behavior applies to all current versions of Word, including 2013.

Back when the Internet was comparatively new, Word didn’t know how to handle URLs as paths to files at all. The Open and Save methods of the Document(s) object couldn’t deal with anything that wasn’t a standard local or network path. At some point, this was rectified and Word can quite happily open and save documents to Internet, SharePoint and Skydrive sites.

What Word still can’t handle, however, is a URL as the index value to identify a Document object in the Documents collection. Code such as the following triggers the error 4160 “Bad file name”.


The only known workaround is that presented by the original poster in the MSDN forum: loop through the Documents collection, comparing the string to the FullName property of each document until you find the one you’re looking for:

Dim doc as Word.Document
Dim sDocPath as String
Dim i as Long, docIndex as Long
sDocPath = "[idnumber]/Documents/MyDoc.docx"
    For i = 1 To Documents.Count
        If Documents(i).FullName = sDocPath Then
            docIndex = i
            Exit For
        End If
    Next i
Set doc = Documents(docIndex)

Leave a Reply