Skip to content

Instantly share code, notes, and snippets.

@thoughtcroft
Created August 2, 2016 00:41
Show Gist options
  • Save thoughtcroft/722d4f97b0622726a98f8e0d3f341dd1 to your computer and use it in GitHub Desktop.
Save thoughtcroft/722d4f97b0622726a98f8e0d3f341dd1 to your computer and use it in GitHub Desktop.
2010-04-01-how-to-tell-if-a-file-is-a-word-document.md
Public Function IsWordDocument(ByVal strExtension As String) As Boolean
' Developed by Warren Bain on 01/04/2010
' Copyright (c) Thought Croft Pty Ltd
' All rights reserved.
' Verifies if the supplied file extension e.g. "doc"
' is recognised as one of the documents that this version
' of Word can natively handle. List is constructed from the
' types of documents that can be filtered in the Open File dialog
' therefore this only works from Microsoft Word 2002 onwards.
Const strcNoiseChars As String = "*?."
Const strcSeparators As String = ";,"
Const strcDelimiter As String = "|"
Static colExtensions As Collection
Dim fdf As FileDialogFilter
Dim astrExts As Variant
Dim strExts As String
Dim i As Integer
' Check if we have loaded the collection yet - only done once
If colExtensions Is Nothing Then
Set colExtensions = New Collection
For Each fdf In Application.FileDialog(msoFileDialogOpen).Filters
strExts = fdf.Extensions
' Remove any 'noise' characters from the string
For i = 1 To Len(strcNoiseChars)
strExts = Replace(strExts, Mid$(strcNoiseChars, i, 1), vbNullString)
Next i
' Ensure we standardise on separators used
For i = 1 To Len(strcSeparators)
strExts = Replace(strExts, Mid$(strcSeparators, i, 1), strcDelimiter)
Next i
' Turn the current set of extensions into an array
astrExts = Split(strExts, strcDelimiter)
' Add all the ones we haven't already got
For i = LBound(astrExts) To UBound(astrExts)
' If already there, this will fail so ignore
On Error Resume Next
colExtensions.Add Trim(astrExts(i)), Trim(astrExts(i))
On Error GoTo 0
Next i
Next fdf
End If
' We just try and look up the file type and if it fails
' then it is not an intrinsically supported document
On Error Resume Next
strExts = colExtensions.Item(strExtension)
IsWordDocument = (Err = 0)
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment