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
LVL 1
BakersvilleAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rajapandian_81Commented:
Refer below link
http://blog-dotnetsp.blogspot.com/2010/02/automation-of-mail-merge-from-windows.html

If it doesn't help post your code to find error.
0
BakersvilleAuthor Commented:
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
0
rajapandian_81Commented:
Bakersville:

WordApp.Documents.Open will not accept single argument. Try to give all required arguments.
0
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

13598Commented:
raja   I use it that way and it works. What do you mean all required arguments?
0
13598Commented:
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

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

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


screencap.jpg
0
rajapandian_81Commented:
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).
0
13598Commented:
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.
0
13598Commented:
The error you are getting could be caused because of versions.
I found this regarding your error where this person also received the AccessViolationException error:
'In my project, I reference the .NET word .dll for Word 12.0,
but I install Office 2000(Word 9.0) on my PC.
It causes the issue of the dependency of .dll and .exe.
After I update my Office to Office 2007, the program runs fine.'

You need to check the references you are using as well as what you have installed as well as the version the document was created under.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
13598Commented:
I would also try to open the document manually on the same machine and make sure there are no issues with permissions.
0
BakersvilleAuthor Commented:
I am using office 2000 an dnow only using reference library 9.0.

The other image attached.
screencap.jpg
0
13598Commented:
Let me change my references to match yours now. Can you open that document manually with no problems or messages?
0
BakersvilleAuthor Commented:
Yes i can, i have no problems in opening it
0
13598Commented:
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.
0
BakersvilleAuthor Commented:
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
0
13598Commented:
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
0
13598Commented:
Sorry make sure you use your path not mine.D:\Internal\Project1\Template.doc
0
BakersvilleAuthor Commented:
Going home now.  will work on this in the morning

Thank you
0
BakersvilleAuthor Commented:
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
0
BakersvilleAuthor Commented:
Thank you again, this was very helpful and found the problem in the end...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.