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.WordDocDim 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 |
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
>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![]()
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....![]()
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.