473,769 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Word Automation

Hi all,

I have a problem automating word (office xp).
I am trying to create a mailmerge datasource using this code:

object oFieldsVector = (object)fieldsV ector;

object oFileName = "doc123";//aDoc.Name;

aDoc.MailMerge. CreateDataSourc e(ref oFileName, ref missing, ref missing, ref
oFieldsVector, ref missing, ref missing, ref missing, ref missing, ref
missing);

aDoc.MailMerge. EditDataSource( );

Word.Document aTable = WordApp.ActiveD ocument;

Word.Table table = aTable.Tables.I tem(1);

for (int j=1;j<dataSourc e.Rows.Count;j+ +)

{

table.Select();

table.Rows.Add( ref missing);

}

the problem is that when i get to the row adding part, a dialog opens up in
the word application asking something about the columns.

this issue never came up in all MS and others examples on the web.

did anyone see this too?

what am i doing wrong? (this is actually almost coppied from MSDN sample).

thanx,

Picho
Jul 21 '05 #1
5 5882
Picho,

Here is some sample code that I used in VB.NET for performing a mail merge.

Dim wrdApp As Word.Applicatio n
Dim wrdDoc As Word.Document
Dim wrdMailMerge As Word.MailMerge
Dim strDataFile As String
' Create an instance of Word and make it visible
wrdApp = CType(GetObject ("", "Word.Applicati on"), Word.Applicatio n)
wrdApp.Visible = True
' Open the template
wrdDoc = wrdApp.Document s.Open(Template Name, False, True)
wrdMailMerge = wrdDoc.MailMerg e
Call wrdMailMerge.Op enDataSource(Fi leName, 0, False, True)
wrdMailMerge.De stination = Word.WdMailMerg eDestination.wd SendToNewDocume nt
wrdMailMerge.Ex ecute(False)
' Close the original form document
wrdDoc.Saved = True
wrdDoc.Close(Fa lse)
System.Runtime. InteropServices .Marshal.Releas eComObject(wrdM ailMerge)
wrdMailMerge = Nothing
System.Runtime. InteropServices .Marshal.Releas eComObject(wrdD oc)
wrdDoc = Nothing
System.Runtime. InteropServices .Marshal.Releas eComObject(wrdA pp)
wrdApp = Nothing
GC.Collect()
"Picho" <pi***********@ telhai.ac.il> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Hi all,

I have a problem automating word (office xp).
I am trying to create a mailmerge datasource using this code:

object oFieldsVector = (object)fieldsV ector;

object oFileName = "doc123";//aDoc.Name;

aDoc.MailMerge. CreateDataSourc e(ref oFileName, ref missing, ref missing, ref oFieldsVector, ref missing, ref missing, ref missing, ref missing, ref
missing);

aDoc.MailMerge. EditDataSource( );

Word.Document aTable = WordApp.ActiveD ocument;

Word.Table table = aTable.Tables.I tem(1);

for (int j=1;j<dataSourc e.Rows.Count;j+ +)

{

table.Select();

table.Rows.Add( ref missing);

}

the problem is that when i get to the row adding part, a dialog opens up in the word application asking something about the columns.

this issue never came up in all MS and others examples on the web.

did anyone see this too?

what am i doing wrong? (this is actually almost coppied from MSDN sample).

thanx,

Picho

Jul 21 '05 #2
Thanx for the reply. I do know how to open a datasource and execute the
merge. the problem starts when I want to create an internal dataSource for
the new document.

Picho
"solex" <so***@nowhere. com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Picho,

Here is some sample code that I used in VB.NET for performing a mail merge.
Dim wrdApp As Word.Applicatio n
Dim wrdDoc As Word.Document
Dim wrdMailMerge As Word.MailMerge
Dim strDataFile As String
' Create an instance of Word and make it visible
wrdApp = CType(GetObject ("", "Word.Applicati on"), Word.Applicatio n)
wrdApp.Visible = True
' Open the template
wrdDoc = wrdApp.Document s.Open(Template Name, False, True)
wrdMailMerge = wrdDoc.MailMerg e
Call wrdMailMerge.Op enDataSource(Fi leName, 0, False, True)
wrdMailMerge.De stination = Word.WdMailMerg eDestination.wd SendToNewDocume nt
wrdMailMerge.Ex ecute(False)
' Close the original form document
wrdDoc.Saved = True
wrdDoc.Close(Fa lse)
System.Runtime. InteropServices .Marshal.Releas eComObject(wrdM ailMerge)
wrdMailMerge = Nothing
System.Runtime. InteropServices .Marshal.Releas eComObject(wrdD oc)
wrdDoc = Nothing
System.Runtime. InteropServices .Marshal.Releas eComObject(wrdA pp)
wrdApp = Nothing
GC.Collect()
"Picho" <pi***********@ telhai.ac.il> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Hi all,

I have a problem automating word (office xp).
I am trying to create a mailmerge datasource using this code:

object oFieldsVector = (object)fieldsV ector;

object oFileName = "doc123";//aDoc.Name;

aDoc.MailMerge. CreateDataSourc e(ref oFileName, ref missing, ref missing,

ref
oFieldsVector, ref missing, ref missing, ref missing, ref missing, ref
missing);

aDoc.MailMerge. EditDataSource( );

Word.Document aTable = WordApp.ActiveD ocument;

Word.Table table = aTable.Tables.I tem(1);

for (int j=1;j<dataSourc e.Rows.Count;j+ +)

{

table.Select();

table.Rows.Add( ref missing);

}

the problem is that when i get to the row adding part, a dialog opens up

in
the word application asking something about the columns.

this issue never came up in all MS and others examples on the web.

did anyone see this too?

what am i doing wrong? (this is actually almost coppied from MSDN sample).
thanx,

Picho


Jul 21 '05 #3
Hi Picho,

Which version of Word are you trying to automate? Do you have a particular
reason for wanting to use a Word table for the data source? Generally, a
delimited text source (which can be saved as a Word document) will do just as
well and will be faster to generate.

There are reasons for using Word tables:
- you want to include text formatting in the data source in the merge
- you want to include graphical objects in the data source
- you want to preserve leading or trailing spaces in the data

If none of the above apply, put your data together in delimited format, dump
it into a Word document (or a text file, but a Word doc is generally "safer"
across all versions of Word), save that and link it in as the data source.
I have a problem automating word (office xp).
I am trying to create a mailmerge datasource using this code:

object oFieldsVector = (object)fieldsV ector;

object oFileName = "doc123";//aDoc.Name;

aDoc.MailMerge. CreateDataSourc e(ref oFileName, ref missing, ref missing, ref
oFieldsVector, ref missing, ref missing, ref missing, ref missing, ref
missing);

aDoc.MailMerge. EditDataSource( );

Word.Document aTable = WordApp.ActiveD ocument;

Word.Table table = aTable.Tables.I tem(1);

for (int j=1;j<dataSourc e.Rows.Count;j+ +)

{

table.Select();

table.Rows.Add( ref missing);

}

the problem is that when i get to the row adding part, a dialog opens up in
the word application asking something about the columns.

this issue never came up in all MS and others examples on the web.

did anyone see this too?

what am i doing wrong? (this is actually almost coppied from MSDN sample).


-- Cindy

Jul 21 '05 #4
Thanx Cindy,

I thought that I can link a "virtual" dataSource to the Doc.
My app is supposed to open a new doc with a new dataSource.
otherwise the user will have to handle the savings of two files: the doc and
the datasource.

correct me if I am wrong, but it is impossible to do right?

Picho
"Cindy M -WordMVP-" <C.*********@hi speed.ch> wrote in message
news:VA.00008fd 6.006149d2@spee dy...
Hi Picho,

Which version of Word are you trying to automate? Do you have a particular
reason for wanting to use a Word table for the data source? Generally, a
delimited text source (which can be saved as a Word document) will do just as well and will be faster to generate.

There are reasons for using Word tables:
- you want to include text formatting in the data source in the merge
- you want to include graphical objects in the data source
- you want to preserve leading or trailing spaces in the data

If none of the above apply, put your data together in delimited format, dump it into a Word document (or a text file, but a Word doc is generally "safer" across all versions of Word), save that and link it in as the data source.
I have a problem automating word (office xp).
I am trying to create a mailmerge datasource using this code:

object oFieldsVector = (object)fieldsV ector;

object oFileName = "doc123";//aDoc.Name;

aDoc.MailMerge. CreateDataSourc e(ref oFileName, ref missing, ref missing, ref oFieldsVector, ref missing, ref missing, ref missing, ref missing, ref
missing);

aDoc.MailMerge. EditDataSource( );

Word.Document aTable = WordApp.ActiveD ocument;

Word.Table table = aTable.Tables.I tem(1);

for (int j=1;j<dataSourc e.Rows.Count;j+ +)

{

table.Select();

table.Rows.Add( ref missing);

}

the problem is that when i get to the row adding part, a dialog opens up in the word application asking something about the columns.

this issue never came up in all MS and others examples on the web.

did anyone see this too?

what am i doing wrong? (this is actually almost coppied from MSDN sample).


-- Cindy

Jul 21 '05 #5
Hi Picho,
I thought that I can link a "virtual" dataSource to the Doc.
My app is supposed to open a new doc with a new dataSource.
otherwise the user will have to handle the savings of two files: the doc and
the datasource.

correct me if I am wrong, but it is impossible to do right?

I don't know of any way for you to create a "virtual data source", but you
should be able to link up the data source for the user, and save him that
step. CreateDataSourc e does NOT create a "virtual data source" - notice you
have to give it a file name? And EditDataSource actually opens the file
(again).

Think of your requirement as separate tasks, and work on each task
individually, until you get what you need:
- creating the data source
- opening or creating the main merge document
- linking the data source (OpenDataSource method)
- generating the main merge document content
- presenting the result to the user

When looked at like this, it doesn't matter WHAT you use for the data source,
as long as the version of Word you're automating supports linking to it using
OpenDataSource with no problems. Given everything I know about your project,
my recommendation would be:

- concatenate the data into a delimited string
- create a new Word document
- put the string into the Word document (doc.Range.Text = szString - worlds
faster than the approach proposed in your first message)
- save the document and close it
- create another new document
- use OpenDataSource to link to the data document
- insert the text and merge fields

-- Cindy

Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
12088
by: mickeydisn | last post by:
Sub: C++ Word automation Extract text hello. I want extact text form a word document using a visual c++ programme. I have see a lot of documentation. and my analysis is that I must use a "word automation". I have foud a lot of exemple to use it but I need your precious help to
3
615
by: Mike MacSween | last post by:
Office 2000 From Access I've been starting an instance of word and doing a mail merge. Everything hunky-dory until I applied SP1 yesterday. Now this: dim wrd as Object set wrd = CreateObject("Word.Application")
2
2614
by: kids | last post by:
Does anybody know any reason which could cause Ms. word automation to crash? I try to call word automation to open a document and use find and replace function. For some reason it works but I don't know why it crash some time.
4
7683
by: Daniel | last post by:
Hello, i have a problem with the word automation from c#. First, i want to mention, that i don't have any dependencies from word in my c#-project, i want to use the system.reflection model to handle the automation. So, i'm using the following code to create a new word document: ---Code---
1
1309
by: Franck | last post by:
hello, I've found this tutorial for word automation in asp http://www.codeproject.com/aspnet/wordapplication.asp TO work I need to include the "Microsoft Word 10.0 Object Library dll" But i canot find it on my PC Has anyone found a place to donwload it?
4
2755
by: Charles Law | last post by:
Does anyone know which files need to be distributed with a VB.NET application to enable Word automation on a m/c that does not have Word installed? Is this even possible / permissible? I currently get the error "File or assembly name Microsoft.office.interop.word or one of its dependencies was not found". TIA Charles
2
3452
by: johnc | last post by:
Hi all Been struggling to get word automation working via ASP on the server. The COM objects etc are installed and there already exists an Excel automation application which works, done by a different developer. I've been asked to create an application that will ultimely open a word template insert some data in and save it as a word document with a timestamped filename. Problem the code I am currently using either says the...
1
2683
by: Steve | last post by:
I am trying to figure out how I can center and size a word application active window within my vb.net form when the word document is open using automation so that the word window doesn't extend beyond/overlap the form window (top, left, width or top). Thanks Steve
1
1972
by: apondu | last post by:
Hi, can someone help me and provide me the information on how to supress the default action of the shortcut keys of word during word automation. I am using C#.Net I have written a code but it generates a exception "Object Reference not set
0
1756
by: Pramod Elig | last post by:
I developed this code which works fine in my current system which has office 2007, but when I run in word 2000 this doesn’t work. Can you please help me to code word automation using late binding. I want to create text, tables. Word.Application WApp = new Word.Application(); // Open Microsoft Word application object objMissing = System.Reflection.Missing.Value; // create the place holder object Word.Document...
0
10208
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9987
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9857
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8867
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7404
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2812
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.