December 6, 2004 at 11:14 am
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
December 6, 2004 at 11:26 am
Have you tried writing the word document to a BLOB (image/text) field??
Good Hunting!
AJ Ahrens
webmaster@kritter.net
December 6, 2004 at 11:29 am
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
December 6, 2004 at 11:33 am
How about storing the file on a shared network drive and simply saving the path on sql server (much less overhead on the server).
December 6, 2004 at 11:39 am
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
December 6, 2004 at 11:49 am
this is csharp but it can give you a start
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
December 7, 2004 at 3:21 am
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
December 7, 2004 at 6:50 am
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.
December 7, 2004 at 1:04 pm
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)
Bob Monahon
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply