Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2007

Tip: Looking for answers? Try searching our database.

Checking for file type programmatically

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Angie - 31 Oct 2007 16:51 GMT
I have a program that looks for RTF files. Sometimes though it throws and
error because someone just renamed there file from a .doc to a .rtf. I need
to make sure that the file type is Rich Text, not just count on the file
extension. Is there a VBA line for this? I thought ActiveDocument.Type might
work, but it gives me a 0, not sure if this is boolean or not. Thanks all
Jay Freedman - 31 Oct 2007 17:22 GMT
> I have a program that looks for RTF files. Sometimes though it throws
> and error because someone just renamed there file from a .doc to a
> .rtf. I need to make sure that the file type is Rich Text, not just
> count on the file extension. Is there a VBA line for this? I thought
> ActiveDocument.Type might work, but it gives me a 0, not sure if this
> is boolean or not. Thanks all

You can open the file as if it were a pure text file and check the first six
characters. If it's a real RTF file, those characters will be "{\rtf1".

Sub demo()
   Dim dlg As Dialog
   Dim msg As String

   Set dlg = Dialogs(wdDialogFileOpen)
   With dlg
       If .Display = -1 Then
            msg = .Name & " is"
            If Not IsRtf(WordBasic.filenameinfo$(.Name, 1)) Then
               msg = msg & " not"
            End If
            msg = msg & " a real RTF file"
            MsgBox msg
       End If
   End With
End Sub

Private Function IsRtf(filename As String) As Boolean
   Dim firstLine As String
   IsRtf = False

   Open filename For Input As #1
   Line Input #1, firstLine
   Close #1

   If LCase(Left(firstLine, 6)) = "{\rtf1" Then
       IsRtf = True
   End If
End Function

Signature

Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Angie - 31 Oct 2007 19:29 GMT
Blazing fast response!! Thank you so much, totally works for what I need.

> > I have a program that looks for RTF files. Sometimes though it throws
> > and error because someone just renamed there file from a .doc to a
[quoted text clipped - 35 lines]
>     End If
> End Function
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.