打开现有的Word文档

时间:2017-11-15 08:41:07

标签: vb.net ms-word filestream file-structure

Dim myStream As Stream = Nothing
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "C:\Temp"

If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
    Try
        myStream = openFileDialog1.OpenFile()
        If (myStream IsNot Nothing) Then
            Dim oWord As Word.Application
            Dim oDoc As Word.Document
            Dim oPara1 As Word.Paragraph
            oWord = CreateObject("Word.Application")
            oWord = New Word.Application
            oWord.Visible = True
            oDoc = oWord.Documents.Add

            'Open an existing document.

            oWord.Documents.Open(openFileDialog1.FileName, ReadOnly:=False)
            oDoc = oWord.ActiveDocument

            'Insert a paragraph at the beginning of the document.

            oPara1 = oDoc.Content.Paragraphs.Add
            oPara1.Range.Text = "Heading 1"
            oPara1.Range.Font.Bold = True
            oPara1.Format.SpaceAfter = 24    '24 pt spacing after paragraph.
            oPara1.Range.InsertParagraphAfter()
        End If

    Catch Ex As Exception
        MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
    Finally

    ' Check this again, since we need to make sure we didn't throw an exception on open.

        If (myStream IsNot Nothing) Then
            myStream.Close()
        End If
    End Try
End If

当我运行程序的这一部分时,word文档打开,但它以只读模式打开。如何确保从代码中以编辑模式打开文档?

Screenshot

0 个答案:

没有答案