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

asked on

open a word document in word from vb.net

Hi all

Can anyone help me, i'm trying to run a mail merge from my project in vb.net i've created my csv file for word to use in .net, created the autorun command to merge and save the file, however, i can not get .net to open ms word.

everything i have search and tried i keep getting errors.

I keep getting errors like Word.Application and Word.Document is not declared.

I've added Microsoft Office 12.0 and 9.0 object library as a reference.

Can anyone help me please

Baker
SOLUTION
Avatar of rajapandian_81
rajapandian_81
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 Bakersville

ASKER

Hi rajapandian

Thank you for your reply.

The error i get now is AccessViolationExemption error and the code is

Imports Microsoft.Office.Interop.Word
Imports System.Diagnostics.Process
Imports System.Data.SqlClient
Imports System.Data
Imports System.IO

Public Class VeiwJoiners

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


Dim WordApp As New Application
        Dim DocToOpen As Document

        Try

            DocToOpen = WordApp.Documents.Open("D:\Internal\Project1\Template.doc")

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

End Sub
End Class

Hope this helps

Baker
Bakersville:

WordApp.Documents.Open will not accept single argument. Try to give all required arguments.
raja   I use it that way and it works. What do you mean all required arguments?
You need to fully qualify your declarations so the program knows it is a word application not just a regular application.
Instead of this:
Dim WordApp As New Application
        Dim DocToOpen As Document

Try this:
Dim WordApp As New Microsoft.Office.Interop.Word.Application
        Dim DocToOpen As Microsoft.Office.Interop.Word.Document

raja, i'll have to agree with 13598. What do you mean?

Thank you 13598. But i get the same error as before.
Could  you either post a print screen of your references or tell me exactly what your reference to word interop is?
Hi 13598, i hope this is what you wanted


screencap.jpg
I have added Microsoft Word 11.0 Object Library as reference.

It requires 16 arguments. (Please refer the same in link I given in my earlier post).
It shows better if you select the project (windowsapplication1) and click on properties (above the project the leftmost icon ) and then click on the References tab.
If you can't get there then I need you to at least expand the nodes for word so i can actually see them.
ASKER CERTIFIED SOLUTION
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
I would also try to open the document manually on the same machine and make sure there are no issues with permissions.
I am using office 2000 an dnow only using reference library 9.0.

The other image attached.
screencap.jpg
Let me change my references to match yours now. Can you open that document manually with no problems or messages?
Yes i can, i have no problems in opening it
OK I have this code:
Try
            Dim WordApp As New Microsoft.Office.Interop.Word.Application
            Dim DocToOpen As Microsoft.Office.Interop.Word.Document



            DocToOpen = WordApp.Documents.Open("C:\Me\AutomaticallyCopyingProjectB4NextBuild.doc")
            WordApp.Visible = True
            Me.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

And it opens just fine no errors.
Can you try opening word, typing a few words, saving it and then change your code to open that document to completely rule out this is a specific document issue?

Also can you make sure you don't have word sessions running in the background from running your code. Use Taskmanager  Processes and look for WINWORD.
Thank you.

I have tried your code, but still getting the error up.

Really don't understand this.

I have screen capped the error.  But word is now opening, it is not opening the file...

If i think i am right, it has something to do with Raja and the arguments..

I will investigate and hopefully can get it sorted.

I have tried using the Dim oMissing As [Object] = System.Reflection.Missing.Value from raja above, but still no luck?
screencap.jpg
Well I got mine to fail by opening the document and exiting without closing. It leaves behind the temp file. When I try to run it again it gives me a warning and then an error.
Check your D:\Internal\Project1 folder for any word temp files (it will be somehing like ~$mplate.doc) and make sure to delete them. Also make sure to close out all word instances running in the background by going into taskmanager. processes sort by name and end process tree of any WINWORD process running. Or if you don't feel comfortable doing this simply reboot your PC.
Then use this code ( It may force a warning message that would give us a better clue):
 Try
            Dim WordApp As New Microsoft.Office.Interop.Word.Application
            Dim DocToOpen As Microsoft.Office.Interop.Word.Document
            DocToOpen = WordApp.Documents.Open("C:\Me\Template.doc")
            WordApp.Visible = True
            WordApp.Activate()
            Me.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
Sorry make sure you use your path not mine.D:\Internal\Project1\Template.doc
Going home now.  will work on this in the morning

Thank you
Thank you all..  Got it working this morning.  It does not like to work on office 2000 so install office 2003 and used the 11.0 library adn now working ok.

Thank you again
Thank you again, this was very helpful and found the problem in the end...