To Open a word document through vb.net - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

To Open a word document through vb.net

priya_sankapriya_sanka Posts: 15Member
Hi,
I want to know what components or references to be added to access a word document through vb.net.

Thanks.

Comments

  • xtabmvpxtabmvp Posts: 23Member

    You need to add a Reference to the MS Word Object Library (and, although this may be optional) the MS Office Object Library. Go to References: Add Reference: COM and select them.

    You should declare Word app and Word doc variables, similar to:
    [code]
    Dim MSWord As New Word.Application
    Dim WordDoc As New Word.Document
    [/code]

    Code similar to the following will open (and display) a word doc:
    [code]
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    WordDoc = MSWord.Documents.Open("C:Demo.Doc")

    ' If you want the user to see it ...
    MSWord.WindowState = Word.WdWindowState.wdWindowStateNormal
    MSWord.Visible = True

    End Sub
    [/code]

    Important to remember to close the Word objects, otherwise they remain running in the background and may cause problems:

    [code]
    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    ' Close it all down
    MSWord.Quit()
    MSWord = Nothing
    End Sub
    [/code]

    Hope this helps

    XTab

    ==============================


    : Hi,
    : I want to know what components or references to be added to access a word document through vb.net.
    :
    : Thanks.
    :

  • priya_sankapriya_sanka Posts: 15Member
    Hi,
    Thank you very much for ur reply which helped me much.


    :
    : You need to add a Reference to the MS Word Object Library (and, although this may be optional) the MS Office Object Library. Go to References: Add Reference: COM and select them.
    :
    : You should declare Word app and Word doc variables, similar to:
    : [code]
    : Dim MSWord As New Word.Application
    : Dim WordDoc As New Word.Document
    : [/code]
    :
    : Code similar to the following will open (and display) a word doc:
    : [code]
    : Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    : WordDoc = MSWord.Documents.Open("C:Demo.Doc")
    :
    : ' If you want the user to see it ...
    : MSWord.WindowState = Word.WdWindowState.wdWindowStateNormal
    : MSWord.Visible = True
    :
    : End Sub
    : [/code]
    :
    : Important to remember to close the Word objects, otherwise they remain running in the background and may cause problems:
    :
    : [code]
    : Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    : ' Close it all down
    : MSWord.Quit()
    : MSWord = Nothing
    : End Sub
    : [/code]
    :
    : Hope this helps
    :
    : XTab
    :
    : ==============================
    :
    :
    : : Hi,
    : : I want to know what components or references to be added to access a word document through vb.net.
    : :
    : : Thanks.
    : :
    :
    :

Sign In or Register to comment.