Bakersville
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Bakersville:
WordApp.Documents.Open will not accept single argument. Try to give all required arguments.
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.W ord.Applic ation
Dim DocToOpen As Microsoft.Office.Interop.W ord.Docume nt
Instead of this:
Dim WordApp As New Application
Dim DocToOpen As Document
Try this:
Dim WordApp As New Microsoft.Office.Interop.W
Dim DocToOpen As Microsoft.Office.Interop.W
ASKER
raja, i'll have to agree with 13598. What do you mean?
ASKER
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?
ASKER
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 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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I would also try to open the document manually on the same machine and make sure there are no issues with permissions.
ASKER
I am using office 2000 an dnow only using reference library 9.0.
The other image attached.
screencap.jpg
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?
ASKER
Yes i can, i have no problems in opening it
OK I have this code:
Try
Dim WordApp As New Microsoft.Office.Interop.W ord.Applic ation
Dim DocToOpen As Microsoft.Office.Interop.W ord.Docume nt
DocToOpen = WordApp.Documents.Open("C: \Me\Automa ticallyCop yingProjec tB4NextBui ld.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.
Try
Dim WordApp As New Microsoft.Office.Interop.W
Dim DocToOpen As Microsoft.Office.Interop.W
DocToOpen = WordApp.Documents.Open("C:
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.
ASKER
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
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.
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.W ord.Applic ation
Dim DocToOpen As Microsoft.Office.Interop.W ord.Docume nt
DocToOpen = WordApp.Documents.Open("C: \Me\Templa te.doc")
WordApp.Visible = True
WordApp.Activate()
Me.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
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.W
Dim DocToOpen As Microsoft.Office.Interop.W
DocToOpen = WordApp.Documents.Open("C:
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.d oc
ASKER
Going home now. will work on this in the morning
Thank you
Thank you
ASKER
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
ASKER
Thank you again, this was very helpful and found the problem in the end...
ASKER
Thank you for your reply.
The error i get now is AccessViolationExemption error and the code is
Imports Microsoft.Office.Interop.W
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:
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class
Hope this helps
Baker