Hello,
How can I open MS Word document using VB.NET?
Printable View
Hello,
How can I open MS Word document using VB.NET?
Below is the code to open Word document.
Code:Private Sub button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) '
' Use the custom dialog to get the title of the document from the user
Dim theQueryDialog As New TitleQuery
If theQueryDialog.ShowDialog() = DialogResult.OK Then
Dim missing As Object = System.Reflection.Missing.Value
Dim fileName As Object = "normal.dot"
' template file name
Dim newTemplate As Object = False
Dim docType As Object = 0
Dim isVisible As Object = True
' Create a new Document, by calling the Add function in the Documents collection
Dim aDoc As Word.Document = WordApp.Documents.Add(fileName, newTemplate, docType, isVisible)
' need to see the created document, so make it visible
WordApp.Visible = True
aDoc.Activate()
' Global Constant enumerations are members of Word and can beassignedproperties
' Set alignment to the center of the document WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
' Toggle the title to a Bold Font
WordApp.Selection.Font.Bold = CInt(Word.WdConstants.wdToggle)
End If ' Type the Text of the Title that was inputted by the user in the CustomDialog WordApp.Selection.TypeText(theQueryDialog.Title); }}
End Sub 'button2_Click
This given code is to Open MS Word document using Asp.NET
Code:Try
Dim stream As FileStream = File.Open("d:\word.doc", FileMode.Open) 'replace the location with your word doc.
stream.Position = 0
' Now read s into a byte buffer.
Dim bytes() As Byte = New Byte(stream.Length) {}
Dim numBytesToRead As Integer = CType(stream.Length, Integer)
Dim numBytesRead As Integer = 0
While numBytesToRead > 0
' Read may return anything from 0 to numBytesToRead.
Dim n As Integer = stream.Read(bytes, numBytesRead, numBytesToRead)
' The end of the file is reached.
If n = 0 Then
Exit While
End If
numBytesRead += n
numBytesToRead -= n
End While
stream.Close()
Response.Clear()
Response.ContentType = "application/word"
Response.AddHeader("Content-disposition", "filename=output.doc")
Response.OutputStream.Write(bytes, 0, bytes.Length)
Response.OutputStream.Flush()
Response.OutputStream.Close()
Response.Flush()
Response.Close()
Catch ex As Exception
Throw ex
End Try