DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: How to determine the version of Word a Word Doc was saved in

  1. #1
    Graham Russell Guest

    How to determine the version of Word a Word Doc was saved in


    I am not sure if this is the best place to ask this question but it is worth
    a try... (I have also posted this in the VBA section)

    I need to write an application that will browse a directory and display which
    version of Microsoft Word that a document was saved in.
    Rather than simply open every document and resave it in the latest format,
    the powers that be have requested a tool that will determine the version.

    I have looked and could not see any way of doing this through Word itself
    using the object model i.e. Automate word to load the .Doc file user that
    to find out what the doc was saved in and then close Word).

    I have though, found a KB article explaining how to do this in C++.
    (Q174140 -
    http://support.microsoft.com/default...174140&GSSNB=1 )

    Does anyone one know how (or if) this can be done in Visual Basic ??

    Any help would be appreciated.

    Kind regards,

    -Graham Russell


  2. #2
    Graham Russell Guest

    Re: How to determine the version of Word a Word Doc was saved in



    Thanks for the quick reply.

    I have downloaded the type library from your site and installed it fine.

    I put the code in a test app, but everytime I attempt to run the command
    > Set pStorage = StgOpenStorage(filename, Nothing, _
    > STGM_READ Or STGM_SHARE_EXCLUSIVE, vbNullString)


    with the filename (in my example C:\Test.Doc) it jumps to the first error
    handler with a error number of 70 and description of Permission denied.

    I am not sure why as I have full access to the file, and nothing is locking
    it.

    I am running Vb6 SP3, Win NT 4 SP 5.

    Any ideas ??

    Where did you get your info to create the type library ??

    Thanks again,

    -Graham Russell


    "Eduardo A. Morcillo" <e _ m o r c i l l o @ y a h o o . c o m> wrote:
    >> I have though, found a KB article explaining how to do this in C++.
    >> (Q174140 -
    >> http://support.microsoft.com/default...174140&GSSNB=1

    )
    >
    >The following code is the VB version of the code in the KB article. It
    >requires a type library with the IStorage and IStream interfaces. I've used
    >OLELIB.TLB from my site.
    >
    >' Word's File-Information-Block (FIB) structure...
    >
    >Private Type FIB
    > magicNumber As Integer
    > ' Word 6.0: 0xA5DC
    > ' Word 7.0 (95): 0xA5DC
    > ' Word 8.0 (97): 0xA5EC
    >
    > version As Integer ' >= 101 for Word 6.0 and higher...
    > ' Word 6.0: 101
    > ' Word 7.0 (95): 104
    > ' Word 8.0 (97): 105+ 103
    >End Type
    >
    >'* WordVersionFromFile() *****************************
    >'
    >'* Returns
    >'* 6 for Word 6.0
    >'* 7 for Word 7.0 (95)
    >'* 8 for Word 8.0 (97)
    >'* Negative if an error occurs...
    >'****************************************************
    >Function WordVersionFromFile(ByVal filename As String) As Long
    >Dim pStorage As IStorage
    >Dim hr As Long
    >Dim tFIB As FIB
    >
    > On Error GoTo Fail1
    >
    > ' Open document as an OLE compound document...
    > Set pStorage = StgOpenStorage(filename, Nothing, _
    > STGM_READ Or STGM_SHARE_EXCLUSIVE, vbNullString)
    >
    > ' Open the data-stream where Word stores the data...
    > Dim pStream As IStream
    >
    > On Error GoTo Fail2
    >
    > Set pStream = pStorage.OpenStream("WordDocument", 0, _
    > STGM_READ Or STGM_SHARE_EXCLUSIVE)
    >
    > ' Read relevant FIB information...
    > Dim dwCount As Long ' bytes read...
    > dwCount = pStream.Read(tFIB, LenB(tFIB))
    >
    > ' Let go of our IStream pointer...
    > Set pStream = Nothing
    > Set pStorage = Nothing
    >
    > ' Determine version to return...
    > If tFIB.version < 101 Then
    > WordVersionFromFile = tFIB.version
    > Else
    > Select Case tFIB.version
    > Case 101
    > WordVersionFromFile = 6
    > Case 103, 104
    > WordVersionFromFile = 7
    > Case 105
    > WordVersionFromFile = 8
    > Case Else
    > WordVersionFromFile = 8
    > End Select
    > End If
    >
    >Exit Function
    >
    >Fail1:
    >
    > WordVersionFromFile = -1
    > Exit Function
    >
    >Fail2:
    > WordVersionFromFile = -2
    > Exit Function
    >
    >End Function
    >
    >
    >--
    >Eduardo A. Morcillo
    >http://www.domaindlx.com/e_morcillo
    >
    >



  3. #3
    Graham Russell Guest

    Re: How to determine the version of Word a Word Doc was saved in



    Thanks for the quick reply.

    I have downloaded the type library from your site and installed it fine.

    I put the code in a test app, but everytime I attempt to run the command
    > Set pStorage = StgOpenStorage(filename, Nothing, _
    > STGM_READ Or STGM_SHARE_EXCLUSIVE, vbNullString)


    with the filename (in my example C:\Test.Doc) it jumps to the first error
    handler with a error number of 70 and description of Permission denied.

    I am not sure why as I have full access to the file, and nothing is locking
    it.

    I am running Vb6 SP3, Win NT 4 SP 5.

    Any ideas ??

    Where did you get your info to create the type library ??

    Thanks again,

    -Graham Russell


    "Eduardo A. Morcillo" <e _ m o r c i l l o @ y a h o o . c o m> wrote:
    >> I have though, found a KB article explaining how to do this in C++.
    >> (Q174140 -
    >> http://support.microsoft.com/default...174140&GSSNB=1

    )
    >
    >The following code is the VB version of the code in the KB article. It
    >requires a type library with the IStorage and IStream interfaces. I've used
    >OLELIB.TLB from my site.
    >
    >' Word's File-Information-Block (FIB) structure...
    >
    >Private Type FIB
    > magicNumber As Integer
    > ' Word 6.0: 0xA5DC
    > ' Word 7.0 (95): 0xA5DC
    > ' Word 8.0 (97): 0xA5EC
    >
    > version As Integer ' >= 101 for Word 6.0 and higher...
    > ' Word 6.0: 101
    > ' Word 7.0 (95): 104
    > ' Word 8.0 (97): 105+ 103
    >End Type
    >
    >'* WordVersionFromFile() *****************************
    >'
    >'* Returns
    >'* 6 for Word 6.0
    >'* 7 for Word 7.0 (95)
    >'* 8 for Word 8.0 (97)
    >'* Negative if an error occurs...
    >'****************************************************
    >Function WordVersionFromFile(ByVal filename As String) As Long
    >Dim pStorage As IStorage
    >Dim hr As Long
    >Dim tFIB As FIB
    >
    > On Error GoTo Fail1
    >
    > ' Open document as an OLE compound document...
    > Set pStorage = StgOpenStorage(filename, Nothing, _
    > STGM_READ Or STGM_SHARE_EXCLUSIVE, vbNullString)
    >
    > ' Open the data-stream where Word stores the data...
    > Dim pStream As IStream
    >
    > On Error GoTo Fail2
    >
    > Set pStream = pStorage.OpenStream("WordDocument", 0, _
    > STGM_READ Or STGM_SHARE_EXCLUSIVE)
    >
    > ' Read relevant FIB information...
    > Dim dwCount As Long ' bytes read...
    > dwCount = pStream.Read(tFIB, LenB(tFIB))
    >
    > ' Let go of our IStream pointer...
    > Set pStream = Nothing
    > Set pStorage = Nothing
    >
    > ' Determine version to return...
    > If tFIB.version < 101 Then
    > WordVersionFromFile = tFIB.version
    > Else
    > Select Case tFIB.version
    > Case 101
    > WordVersionFromFile = 6
    > Case 103, 104
    > WordVersionFromFile = 7
    > Case 105
    > WordVersionFromFile = 8
    > Case Else
    > WordVersionFromFile = 8
    > End Select
    > End If
    >
    >Exit Function
    >
    >Fail1:
    >
    > WordVersionFromFile = -1
    > Exit Function
    >
    >Fail2:
    > WordVersionFromFile = -2
    > Exit Function
    >
    >End Function
    >
    >
    >--
    >Eduardo A. Morcillo
    >http://www.domaindlx.com/e_morcillo
    >
    >



  4. #4
    Eduardo A. Morcillo Guest

    Re: How to determine the version of Word a Word Doc was saved in

    > I have though, found a KB article explaining how to do this in C++.
    > (Q174140 -
    > http://support.microsoft.com/default...174140&GSSNB=1 )


    The following code is the VB version of the code in the KB article. It
    requires a type library with the IStorage and IStream interfaces. I've used
    OLELIB.TLB from my site.

    ' Word's File-Information-Block (FIB) structure...

    Private Type FIB
    magicNumber As Integer
    ' Word 6.0: 0xA5DC
    ' Word 7.0 (95): 0xA5DC
    ' Word 8.0 (97): 0xA5EC

    version As Integer ' >= 101 for Word 6.0 and higher...
    ' Word 6.0: 101
    ' Word 7.0 (95): 104
    ' Word 8.0 (97): 105+ 103
    End Type

    '* WordVersionFromFile() *****************************
    '
    '* Returns
    '* 6 for Word 6.0
    '* 7 for Word 7.0 (95)
    '* 8 for Word 8.0 (97)
    '* Negative if an error occurs...
    '****************************************************
    Function WordVersionFromFile(ByVal filename As String) As Long
    Dim pStorage As IStorage
    Dim hr As Long
    Dim tFIB As FIB

    On Error GoTo Fail1

    ' Open document as an OLE compound document...
    Set pStorage = StgOpenStorage(filename, Nothing, _
    STGM_READ Or STGM_SHARE_EXCLUSIVE, vbNullString)

    ' Open the data-stream where Word stores the data...
    Dim pStream As IStream

    On Error GoTo Fail2

    Set pStream = pStorage.OpenStream("WordDocument", 0, _
    STGM_READ Or STGM_SHARE_EXCLUSIVE)

    ' Read relevant FIB information...
    Dim dwCount As Long ' bytes read...
    dwCount = pStream.Read(tFIB, LenB(tFIB))

    ' Let go of our IStream pointer...
    Set pStream = Nothing
    Set pStorage = Nothing

    ' Determine version to return...
    If tFIB.version < 101 Then
    WordVersionFromFile = tFIB.version
    Else
    Select Case tFIB.version
    Case 101
    WordVersionFromFile = 6
    Case 103, 104
    WordVersionFromFile = 7
    Case 105
    WordVersionFromFile = 8
    Case Else
    WordVersionFromFile = 8
    End Select
    End If

    Exit Function

    Fail1:

    WordVersionFromFile = -1
    Exit Function

    Fail2:
    WordVersionFromFile = -2
    Exit Function

    End Function


    --
    Eduardo A. Morcillo
    http://www.domaindlx.com/e_morcillo



  5. #5
    Eduardo A. Morcillo Guest

    Re: How to determine the version of Word a Word Doc was saved in

    > I have though, found a KB article explaining how to do this in C++.
    > (Q174140 -
    > http://support.microsoft.com/default...174140&GSSNB=1 )


    The following code is the VB version of the code in the KB article. It
    requires a type library with the IStorage and IStream interfaces. I've used
    OLELIB.TLB from my site.

    ' Word's File-Information-Block (FIB) structure...

    Private Type FIB
    magicNumber As Integer
    ' Word 6.0: 0xA5DC
    ' Word 7.0 (95): 0xA5DC
    ' Word 8.0 (97): 0xA5EC

    version As Integer ' >= 101 for Word 6.0 and higher...
    ' Word 6.0: 101
    ' Word 7.0 (95): 104
    ' Word 8.0 (97): 105+ 103
    End Type

    '* WordVersionFromFile() *****************************
    '
    '* Returns
    '* 6 for Word 6.0
    '* 7 for Word 7.0 (95)
    '* 8 for Word 8.0 (97)
    '* Negative if an error occurs...
    '****************************************************
    Function WordVersionFromFile(ByVal filename As String) As Long
    Dim pStorage As IStorage
    Dim hr As Long
    Dim tFIB As FIB

    On Error GoTo Fail1

    ' Open document as an OLE compound document...
    Set pStorage = StgOpenStorage(filename, Nothing, _
    STGM_READ Or STGM_SHARE_EXCLUSIVE, vbNullString)

    ' Open the data-stream where Word stores the data...
    Dim pStream As IStream

    On Error GoTo Fail2

    Set pStream = pStorage.OpenStream("WordDocument", 0, _
    STGM_READ Or STGM_SHARE_EXCLUSIVE)

    ' Read relevant FIB information...
    Dim dwCount As Long ' bytes read...
    dwCount = pStream.Read(tFIB, LenB(tFIB))

    ' Let go of our IStream pointer...
    Set pStream = Nothing
    Set pStorage = Nothing

    ' Determine version to return...
    If tFIB.version < 101 Then
    WordVersionFromFile = tFIB.version
    Else
    Select Case tFIB.version
    Case 101
    WordVersionFromFile = 6
    Case 103, 104
    WordVersionFromFile = 7
    Case 105
    WordVersionFromFile = 8
    Case Else
    WordVersionFromFile = 8
    End Select
    End If

    Exit Function

    Fail1:

    WordVersionFromFile = -1
    Exit Function

    Fail2:
    WordVersionFromFile = -2
    Exit Function

    End Function


    --
    Eduardo A. Morcillo
    http://www.domaindlx.com/e_morcillo



  6. #6
    Michael \(michka\) Kaplan Guest

    Re: How to determine the version of Word a Word Doc was saved in

    "Graham Russell" <GRussell31@Hotmail.Com> wrote...

    > Where did you get your info to create the type library ??


    The definitions is IStorage and IStream are documented in MSDN and in the
    Platform SDK. Eduardo did the work to convert them to something usable in
    VB, thats all.


    --
    MichKa

    Michael Kaplan
    Trigeminal Software, Inc. -- http://www.trigeminal.com/

    International VB? -- http://www.i18nWithVB.com/
    C++? MSLU -- http://msdn.microsoft.com/msdnmag/issues/01/10/





  7. #7
    Michael \(michka\) Kaplan Guest

    Re: How to determine the version of Word a Word Doc was saved in

    "Graham Russell" <GRussell31@Hotmail.Com> wrote...

    > Where did you get your info to create the type library ??


    The definitions is IStorage and IStream are documented in MSDN and in the
    Platform SDK. Eduardo did the work to convert them to something usable in
    VB, thats all.


    --
    MichKa

    Michael Kaplan
    Trigeminal Software, Inc. -- http://www.trigeminal.com/

    International VB? -- http://www.i18nWithVB.com/
    C++? MSLU -- http://msdn.microsoft.com/msdnmag/issues/01/10/





  8. #8
    Eduardo A. Morcillo Guest

    Re: How to determine the version of Word a Word Doc was saved in

    > I put the code in a test app, but everytime I attempt to run the command
    > > Set pStorage = StgOpenStorage(filename, Nothing, _
    > > STGM_READ Or STGM_SHARE_EXCLUSIVE, vbNullString)

    >
    > with the filename (in my example C:\Test.Doc) it jumps to the first error
    > handler with a error number of 70 and description of Permission denied.


    It seems that the document is opened and the function cannot use the share
    exclusive flag. Are you sure that the document is not opened (even in
    read-only mode)?

    --
    Eduardo A. Morcillo
    http://www.domaindlx.com/e_morcillo



  9. #9
    Eduardo A. Morcillo Guest

    Re: How to determine the version of Word a Word Doc was saved in

    > I put the code in a test app, but everytime I attempt to run the command
    > > Set pStorage = StgOpenStorage(filename, Nothing, _
    > > STGM_READ Or STGM_SHARE_EXCLUSIVE, vbNullString)

    >
    > with the filename (in my example C:\Test.Doc) it jumps to the first error
    > handler with a error number of 70 and description of Permission denied.


    It seems that the document is opened and the function cannot use the share
    exclusive flag. Are you sure that the document is not opened (even in
    read-only mode)?

    --
    Eduardo A. Morcillo
    http://www.domaindlx.com/e_morcillo



  10. #10
    Graham Russell Guest

    Re: How to determine the version of Word a Word Doc was saved in


    "Eduardo A. Morcillo" <e _ m o r c i l l o @ y a h o o . c o m> wrote:
    >> I put the code in a test app, but everytime I attempt to run the command
    >> > Set pStorage = StgOpenStorage(filename, Nothing, _
    >> > STGM_READ Or STGM_SHARE_EXCLUSIVE, vbNullString)

    >>
    >> with the filename (in my example C:\Test.Doc) it jumps to the first error
    >> handler with a error number of 70 and description of Permission denied.

    >
    >It seems that the document is opened and the function cannot use the share
    >exclusive flag. Are you sure that the document is not opened (even in
    >read-only mode)?
    >


    I am certain that there is no lock on the document at all.

    I have tried this on two computers and the same error appears on them both.
    One of these is runs Win NT the other Windows 2000.

    I have tried as a test to see if I could open a file with just the STGM_READ
    flag, but that failed with the same result.

    I am not running the lastest Service Pack of VB, could that be the reason
    ?

    Many thanks,

    -Graham Russell

  11. #11
    Graham Russell Guest

    Re: How to determine the version of Word a Word Doc was saved in


    "Eduardo A. Morcillo" <e _ m o r c i l l o @ y a h o o . c o m> wrote:
    >> I put the code in a test app, but everytime I attempt to run the command
    >> > Set pStorage = StgOpenStorage(filename, Nothing, _
    >> > STGM_READ Or STGM_SHARE_EXCLUSIVE, vbNullString)

    >>
    >> with the filename (in my example C:\Test.Doc) it jumps to the first error
    >> handler with a error number of 70 and description of Permission denied.

    >
    >It seems that the document is opened and the function cannot use the share
    >exclusive flag. Are you sure that the document is not opened (even in
    >read-only mode)?
    >


    I am certain that there is no lock on the document at all.

    I have tried this on two computers and the same error appears on them both.
    One of these is runs Win NT the other Windows 2000.

    I have tried as a test to see if I could open a file with just the STGM_READ
    flag, but that failed with the same result.

    I am not running the lastest Service Pack of VB, could that be the reason
    ?

    Many thanks,

    -Graham Russell

  12. #12
    Eduardo A. Morcillo Guest

    Re: How to determine the version of Word a Word Doc was saved in

    > I am certain that there is no lock on the document at all.
    >
    > I have tried this on two computers and the same error appears on them

    both.
    > One of these is runs Win NT the other Windows 2000.


    I tested on XP and it worked without errors.

    > I have tried as a test to see if I could open a file with just the

    STGM_READ
    > flag, but that failed with the same result.


    Try with:

    Set pStorage = StgOpenStorage(filename, Nothing, _
    STGM_READWRITE Or STGM_SHARE_EXCLUSIVE, vbNullString)

    --
    Eduardo A. Morcillo
    http://www.domaindlx.com/e_morcillo



  13. #13
    Eduardo A. Morcillo Guest

    Re: How to determine the version of Word a Word Doc was saved in

    > I am certain that there is no lock on the document at all.
    >
    > I have tried this on two computers and the same error appears on them

    both.
    > One of these is runs Win NT the other Windows 2000.


    I tested on XP and it worked without errors.

    > I have tried as a test to see if I could open a file with just the

    STGM_READ
    > flag, but that failed with the same result.


    Try with:

    Set pStorage = StgOpenStorage(filename, Nothing, _
    STGM_READWRITE Or STGM_SHARE_EXCLUSIVE, vbNullString)

    --
    Eduardo A. Morcillo
    http://www.domaindlx.com/e_morcillo



  14. #14
    Graham Russell Guest

    Re: How to determine the version of Word a Word Doc was saved in


    >
    >I tested on XP and it worked without errors.
    >
    >> I have tried as a test to see if I could open a file with just the

    >STGM_READ
    >> flag, but that failed with the same result.

    >
    >Try with:
    >
    >Set pStorage = StgOpenStorage(filename, Nothing, _
    > STGM_READWRITE Or STGM_SHARE_EXCLUSIVE, vbNullString)
    >


    That solved it, it now allows me to open the file.

    I have to say that I am a little confused why, I would have thought Read
    would have been fine.

    Many thanks for your help.

    -Graham Russell





  15. #15
    Graham Russell Guest

    Re: How to determine the version of Word a Word Doc was saved in


    >
    >I tested on XP and it worked without errors.
    >
    >> I have tried as a test to see if I could open a file with just the

    >STGM_READ
    >> flag, but that failed with the same result.

    >
    >Try with:
    >
    >Set pStorage = StgOpenStorage(filename, Nothing, _
    > STGM_READWRITE Or STGM_SHARE_EXCLUSIVE, vbNullString)
    >


    That solved it, it now allows me to open the file.

    I have to say that I am a little confused why, I would have thought Read
    would have been fine.

    Many thanks for your help.

    -Graham Russell





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center