# 1 Re: Reading Word Documents in Web app(VB.Net)
First create you ASP.Net web application, add one class file called “WordApp.vb” and paste the following code
Imports System
Imports System.ComponentModel
Public Class WordAppl
Private oWordAppl As Word.ApplicationClass
Private oDoc As Word.Document
Public Sub New()
oWordAppl = New Word.ApplicationClass
End Sub
' Open a file (the file must exists) and activate it
Public Sub Open(ByVal strFileName As String)
Dim fileName As Object = strFileName
Dim readOnly1 As Object = False
Dim isVisible As Object = True
Dim missing As Object = System.Reflection.Missing.Value
oDoc = oWordAppl.Documents.Open(fileName, missing, readOnly1, missing, missing, missing, missing, missing, missing, missing, missing, isVisible)
oDoc.Activate()
End Sub
Public Sub Open()
Dim missing As Object = System.Reflection.Missing.Value
oDoc = oWordAppl.Documents.Add(missing, missing, missing, missing)
oDoc.Activate()
End Sub
Public Sub Quit()
Dim missing As Object = System.Reflection.Missing.Value
oWordAppl.Application.Quit(missing, missing, missing)
End Sub
Public Sub Save()
oDoc.Save()
End Sub
Public Sub SaveAs(ByVal strFileName As String)
Dim missing As Object = System.Reflection.Missing.Value
Dim fileName As Object = strFileName
oDoc.SaveAs(fileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
End Sub
Public Sub FindAndReplacement(ByVal strFind As String, ByVal strReplace As String)
Try
oWordAppl.Selection.HomeKey(Word.WdUnits.wdStory, Word.WdMovementType.wdExtend)
With oWordAppl.Selection.Find
.Text = strFind
With .Replacement
.Text = strReplace
End With
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With
Catch ex As Exception
'ClientUtilities.ShowErrorMessage(ex)
End Try
End Sub
End Class
When you compile the above code, you may get an error. To overcome this error, do add Microsoft Word 10.0 Object Library (available under COM tab) as reference.
Next you have to specify the template path, document storing location. Better you can maintain this information in your web config file. The web config contains the following keys for Template path and Document path. Just add the following keys in web config file.
<configuration>
<appSettings>
<add key="WordMod" value="C:\\WordApplication\\template\\" />
<add key="WordDoc" value="C:\WordApplication\document\" />
</appSettings>
<system.web>
...
<system.web>
<configuration>
Put your template file based on configuration information. Next create web form and add one button control in it and add the following code under button click event.
Imports System
Imports System.ComponentModel
Imports System.Configuration
Imports System.Collections
Public Class Nominate
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Put user code to initialize the page here
Dim test As New WordAppl
Dim TemplatePath As String = ConfigurationSettings.AppSettings("WordMod")
Dim DocumentPath As String = ConfigurationSettings.AppSettings("WordDoc")
test.Open(TemplatePath & "Test.dot")
test.SaveAs(DocumentPath & "Test.doc")
test.Quit()
End Sub
End Class
When you click button in your web page, it creates new document available in your document folder as per you config information
OR
1.Open VS.NET , Select Asp.Net Web Application.
2.Add Reference to the Microsoft Word Object Library
3.On the WebForm , Add a button
4.Don't Forget to Import Microsoft.Office.Interop.Word
also assign word=Microsoft.Office.Interop.Word i.e
the Import statement will be Imports word=Microsoft.Office.Interop.Word
this is done so as to avoid long names
5.On the Click Event of the Button Write the Following Code
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
Dim oPara1 As Word.Paragraph
Dim oPara2 As Word.Paragraph
Dim oPara3 As Word.Paragraph, oPara4 As Word.Paragraph
Try
'Start Word and open the document template.
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Add
oPara1 = oDoc.Content.Paragraphs.Add
oPara1.Range.Text = " Employee Details Template"
oPara1.Range.Font.Bold = True
oPara1.Range.InsertParagraphAfter()
oPara2 = oDoc.Content.Paragraphs.Add
Dim empno As String = "Some Employee Number" ' this can even come from ur database
oPara2.Range.Text = "Employee No: " & empno
' oPara2.Alignment.wdAlignParagraphCenter = 1
oPara2.Range.InsertParagraphAfter()
oPara3 = oDoc.Content.Paragraphs.Add
Dim ENAME As String = "Some Employee Name" ' this can even come from ur database
oPara3.Range.Text = "Employee NAME: " & ENAME
oPara3.Range.InsertParagraphAfter()
Catch ex As Exception
' oWord.Visible = True
response.write(ex.Message)
End Try
End Sub