Opening a Word file stored as a Resource

I can add a Word file as a resource to my application, and it then shows up in the Solution Explorer. But how can I then open this file from within my application? I know I need to add a reference to the Word object library, but what then?
[239 byte] By [Arbu] at [2007-12-25]
# 1

Actually if all you want to do is display the file to your user...then you must make sure the Word application is available to the user....and you can open the resource with word without making a reference to the object library....

1. store your document as a binary resource

2. write the resource out to a file

3. start the process using the file you just wrote:



Public
Sub RunMyWordDoc()
Dim b As Byte() = My.Resources.WordDoc

Dim TheFIlePath As String = "C:\MyDoc.doc"

Dim TempFile As System.IO.FileStream = IO.File.Create(TheFIlePath)
TempFile.Write(b, 0, b.Length)
TempFile.Close()
Process.Start(TheFIlePath)
End Sub

DMan1 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2
Thanks. Actually I do want to modify the document, so I will need the reference to the Word library. I am having some success with the code below, but I can't manage to delete the temporary file at the end of the process - Word acts as if I'm opening it rather than just borrowing it as a template. How would I fix this?

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim b As Byte() = My.Resources.QUOTE
Dim TheFilePath As String = "C:\MyDocr.doc"
Dim TempFile As System.IO.FileStream

TempFile = IO.File.Create(TheFilePath)
TempFile.Write(b, 0, b.Length)
TempFile.Close()

Process.Start(TheFilePath)

Dim f As New Word.Application
Dim p As Word.Document = f.Documents.Add(TheFilePath) 'TheFilePath parameter is for a template, I'm not trying to open the file.

p.Range(0, 0).InsertAfter("ADDED!" & vbCrLf)
f.Visible = True
IO.File.Delete(TheFilePath) 'this fails, because "Locked For Editing"
End Sub

Arbu at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

>Process.Start(TheFilePath) <-this line opens the document with word...and will lock the document!

You can do a file save as and then delete the temp file as an alternative

DMan1 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4
I still get the same error, even if I save p. Plus I don't really want to save p - I just want to present it to the user as an open document. It's up to him where/whether he saves it.
Arbu at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5

If it is absolutely neccesary for you to delete the tempaorary file then you have only a couple of choices...

1. save the file as another name

2. present the save as dialog to the user to save the file

3. or wait until the word process has ended and then delete the file (look into the process class for "waitforexit")

Basically any time an application is using a file...you will not be able to delete it....

DMan1 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 6
Well I think it's rather silly that a Word document can't "let go" of a template after it has been used to create that document. So I shall ignore the word document constructor that allows you to open a document with a template, and just open the template and copy it into a new document:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim b As Byte() = My.Resources.QUOTE
Dim TheFilePath As String = "C:\newder.doc"
Dim TempFile As System.IO.FileStream

If Not IO.File.Exists(TheFilePath) Then
TempFile = IO.File.Create(TheFilePath)
TempFile.Write(b, 0, b.Length)
TempFile.Close()
End If

Dim f As New Word.Application
With f
With .Documents.Open(TheFilePath)
.Content.Copy()
.Close()
End With

My.Application.DoEvents() 'make sure it's closed.

IO.File.Delete(TheFilePath)

With .Documents.Add

.Content.Paste()
My.Computer.Clipboard.Clear()

.Range(0, 0).InsertAfter("ADDED!" & vbCrLf)

End With
.Visible = True
End With

End Sub

Seems to work OK.

Arbu at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...