Link to home
Start Free TrialLog in
Avatar of RIAS
RIASFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Open a word document

Hello,

How can I open a word document from vb.net desktop app.
Also,if a word object is created ,please let me know how to close it (instance created only by the project )without leaving a instance in the task manager.


Cheers
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Try..

        Dim objWord As Word.Application
        Dim objDoc As Word.Document
        objWord = CreateObject("Word.Application")
        objDoc = objWord.Documents.Open("E:\\Pawan.docx")
        objWord.Visible = True

Open in new window


Ref - http://www.authorcode.com/how-to-open-existing-word-document-in-vb-net/
Avatar of RIAS

ASKER

pawan,
How to dispose of objword?
Try setting the object to Nothing.
Avatar of RIAS

ASKER

This still leaves a instance in my task manager
Try this..

 Dim oWord As Word.Application
        Dim oDoc As Word.Document
        oWord = CreateObject("Word.Application")
        oWord.Visible = True
        oDoc = oWord.Documents.Add("E:\\Pawan.doc")
        oDoc.Quit(Word.WdSaveOptions.wdSaveChanges)
        oDoc = Nothing

Open in new window

Avatar of RIAS

ASKER

Pawan,

Thanks.

 oWord needs to be quit . It leaves a istance in task manger.
Not worked?
Avatar of RIAS

ASKER

Nope
Avatar of Scott McDaniel (EE MVE )
Try to Close the ActiveDocument as well:

oDoc = oWord.Documents.Add("E:\\Pawan.doc")
oDoc.ActiveDocument.Close(Word.wdSaveOptions.wdSaveChanges)
oDoc.Quit(Word.WdSaveOptions.wdSaveChanges)
oDoc = Nothing

https://msdn.microsoft.com/en-us/library/af6z0wa2.aspx?f=255&MSPPError=-2147217396
try this..

Imports System.IO
Imports System.Net.Mime.MediaTypeNames
Imports System.Diagnostics.Process
Imports Microsoft.Office.Interop

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim oWord As Word.Application
        Dim oDoc As Word.Document
        oWord = CreateObject("Word.Application")
        oWord.Visible = True
        oDoc = oWord.Documents.Add("E:\\Pawan.doc")
        KillProcess("Winword")

    End Sub


    Private Sub KillProcess(processName As String)
        Dim myproc As New System.Diagnostics.Process()
        For Each thisproc As Process In Process.GetProcessesByName(processName)
            If thisproc.CloseMainWindow() Then
                thisproc.Kill()
            End If
        Next
    End Sub

End Class

Open in new window

Give DocX a try

DocX is a .NET library that allows developers to manipulate Word 2007/2010/2013 files, in an easy and intuitive manner. DocX is fast, lightweight and best of all it does not require Microsoft Word or Office to be installed.

https://docx.codeplex.com/
Hi Rias,
Any luck  :) ?

Regards,
Pawan
Avatar of RIAS

ASKER

Pawan,
Just trying now...gimme a minute...
Just know that after a few kill, Word will want to open in safemode
Avatar of RIAS

ASKER

Pawan,
It killed all the word documrnts including the ones not opened by app .
Thanks
ASKER CERTIFIED SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RIAS

ASKER

Pawan,

Just a simple twick, can you please open any other word document and then try your code.
Please let me know if the other document closes as well .

Cheers
Hi Rias,
I tired that case, it is working on my machine. It is not closing other word documents, You can try out.

Regards,
Pawan
Avatar of RIAS

ASKER

ok, will try here.
I guess the difference is my word object is global .
Avatar of RIAS

ASKER

Pawan,
I need to use

    oWord = GetObject("Word.Application")

as I am doing mail merge of many documents.
I create my object on form load and then use it by getobject .
Avatar of RIAS

ASKER

Tried it and it worked!!!!
Avatar of RIAS

ASKER

Thanks a ton!