Using Word with SQL Server

  • I need to store and retrieve a Word Document in and from SQL Server. Do you have any examples of how do do this using VB.NET? The application will use a command button to automatically open Microsoft Word with the appropriate document? Any ideas?


    Arthur Lorenzini

  • Have you tried writing the word document to a BLOB (image/text) field??



    Good Hunting!

    AJ Ahrens


    webmaster@kritter.net

  • I know how to store the document as a blob in the SQL Server using VB.NET. I guess my primary concern is retreiving the blob data and opening it up in Microsoft Word using VB.NET.


    Arthur Lorenzini

  • How about storing the file on a shared network drive and simply saving the path on sql server (much less overhead on the server).

  • I thought about that and it is a way to go but the app. spec and security issues require the documents to be stored directly on the SQL Server. I don't contro those issues.


    Arthur Lorenzini

  • this is csharp but it can give you a start

    http://www.eggheadcafe.com/index/System.Windows.Forms_ConvertEventArgs_390d2fc4-8bd2-452e-b9fd-695da56a6680.asp

    but this ones seems to do the job (it's for a image but I guess the process is pretty much the same).

    http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=1328&lngWId=10

  • Here is some VBScript code which uses GetChunk and BinaryWrite methods. You also need to change ContentType to application/msword:

    Function OutputDocumentImage

     ' Clear out the existing HTTP header information Response.Expires = 0

    Response.Buffer = TRUE

    Response.Clear

    SQLString = "SELECT WordDocument FROM Translations WHERE TranslationID = " & TransID

     RS.Open SQLString, DB, 3

    IF RS.RecordCount > 0 THEN ActualSize = RS("WordDocument").ActualSize

    IF ActualSize > 0 AND isReader THEN Response.ContentType = "application/msword" ' Change the HTTP header to reflect that an image is being passed.

    ImageBin = RS("WordDocument").GetChunk(ActualSize)

    Response.BinaryWrite ImageBin

     ELSE RS.Close Show404

     END IF

     ELSE RS.Close Show404

     END IF

    RS.Close

    Response.End

    End Function

  • I haven't dabbled with .NET yet, but in regular old VB you could retrieve the image field using an ADO recordset, then write the contents of the image field to a file (e.g. in the Windows TEMP dir) using an ADO Stream object.  Pretty quick and efficient.  I'm sure there is a VB.NET/ADO.NET equivalent.

  • If you're writing a Web App, Paul's ASP.NET solution looks good; I reformatted it for readability:

    Function OutputDocumentImage

      '' Clear out the existing HTTP header information Response.Expires = 0

      Response.Buffer = TRUE

      Response.Clear

      SQLString = "SELECT WordDocument FROM Translations WHERE TranslationID = " & TransID

      RS.Open SQLString, DB, 3

      IF RS.RecordCount > 0 THEN

        ActualSize = RS("WordDocument").ActualSize

        '' Change the HTTP header to reflect that an image is being passed.

        IF ActualSize > 0 AND isReader THEN

          Response.ContentType = "application/msword"

          ImageBin = RS("WordDocument").GetChunk(ActualSize)

          Response.BinaryWrite ImageBin

        ELSE

          RS.Close

          Show404

        END IF

      ELSE

        RS.Close

        Show404

      END IF

      RS.Close

      Response.End

    End Function

    Note that the user's browser options may decide whether the "application/msword" is

    actually opened or a save-file option box is offered.

    If you're writing a Windows Forms App, the suggestion from milzs looks good.  Then, you'll have to open the Word

    application and give it focus. The code below is based on VBA that I've used; the same or similar options come up in

    Visual Studio, so it'll probably work there, too:

      Dim zFile as String, zCmd as String

      zFile = "Drive:\directory\filename.doc"

      zCmd = "Word.exe"

      Shell(zCmd & " " & zFile, AppWinStyle.NormalFocus)

    '' Here's a trick if Word.exe is not in the user's path,

    ''  or if it's in different locations on different user machines.

      Dim oWord as Object, zPath as String

      Set oWord = GetObject("","Word.Application")

      zPath = oWord.Path

      '' Example result: C:\Program Files\Microsoft Office\Office10

      Set oWord = Nothing

      Dim zFile as String, q as String, zCmd as String

      zFile = "Drive:\directory\filename.doc"

      q = """

      zCmd = q & zPath & "\Word.exe" & q

      Shell(zCmd & " " & zFile, AppWinStyle.NormalFocus)


    Regards,

    Bob Monahon

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply