Word Automation

Discussion in 'Microsoft Dot NET' started by Picho, Dec 18, 2003.

  1. Picho

    Picho Guest

    Hi all,

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

    object oFieldsVector = (object)fieldsVector;

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

    aDoc.MailMerge.CreateDataSource(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.ActiveDocument;

    Word.Table table = aTable.Tables.Item(1);

    for (int j=1;j<dataSource.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
     
    Picho, Dec 18, 2003
    #1
    1. Advertisements

  2. Picho

    solex Guest

    Picho,

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

    Dim wrdApp As Word.Application
    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.Application"), Word.Application)
    wrdApp.Visible = True
    ' Open the template
    wrdDoc = wrdApp.Documents.Open(TemplateName, False, True)
    wrdMailMerge = wrdDoc.MailMerge
    Call wrdMailMerge.OpenDataSource(FileName, 0, False, True)
    wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument
    wrdMailMerge.Execute(False)
    ' Close the original form document
    wrdDoc.Saved = True
    wrdDoc.Close(False)
    System.Runtime.InteropServices.Marshal.ReleaseComObject(wrdMailMerge)
    wrdMailMerge = Nothing
    System.Runtime.InteropServices.Marshal.ReleaseComObject(wrdDoc)
    wrdDoc = Nothing
    System.Runtime.InteropServices.Marshal.ReleaseComObject(wrdApp)
    wrdApp = Nothing
    GC.Collect()


    "Picho" <> wrote in message
    news:%...
    > Hi all,
    >
    > I have a problem automating word (office xp).
    > I am trying to create a mailmerge datasource using this code:
    >
    > object oFieldsVector = (object)fieldsVector;
    >
    > object oFileName = "doc123";//aDoc.Name;
    >
    > aDoc.MailMerge.CreateDataSource(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.ActiveDocument;
    >
    > Word.Table table = aTable.Tables.Item(1);
    >
    > for (int j=1;j<dataSource.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
    >
    >
     
    solex, Dec 18, 2003
    #2
    1. Advertisements

  3. Picho

    Picho Guest

    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" <> wrote in message
    news:%...
    > Picho,
    >
    > Here is some sample code that I used in VB.NET for performing a mail

    merge.
    >
    > Dim wrdApp As Word.Application
    > 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.Application"), Word.Application)
    > wrdApp.Visible = True
    > ' Open the template
    > wrdDoc = wrdApp.Documents.Open(TemplateName, False, True)
    > wrdMailMerge = wrdDoc.MailMerge
    > Call wrdMailMerge.OpenDataSource(FileName, 0, False, True)
    > wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument
    > wrdMailMerge.Execute(False)
    > ' Close the original form document
    > wrdDoc.Saved = True
    > wrdDoc.Close(False)
    > System.Runtime.InteropServices.Marshal.ReleaseComObject(wrdMailMerge)
    > wrdMailMerge = Nothing
    > System.Runtime.InteropServices.Marshal.ReleaseComObject(wrdDoc)
    > wrdDoc = Nothing
    > System.Runtime.InteropServices.Marshal.ReleaseComObject(wrdApp)
    > wrdApp = Nothing
    > GC.Collect()
    >
    >
    > "Picho" <> wrote in message
    > news:%...
    > > Hi all,
    > >
    > > I have a problem automating word (office xp).
    > > I am trying to create a mailmerge datasource using this code:
    > >
    > > object oFieldsVector = (object)fieldsVector;
    > >
    > > object oFileName = "doc123";//aDoc.Name;
    > >
    > > aDoc.MailMerge.CreateDataSource(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.ActiveDocument;
    > >
    > > Word.Table table = aTable.Tables.Item(1);
    > >
    > > for (int j=1;j<dataSource.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
    > >
    > >

    >
    >
     
    Picho, Dec 21, 2003
    #3
  4. 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)fieldsVector;
    >
    > object oFileName = "doc123";//aDoc.Name;
    >
    > aDoc.MailMerge.CreateDataSource(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.ActiveDocument;
    >
    > Word.Table table = aTable.Tables.Item(1);
    >
    > for (int j=1;j<dataSource.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
     
    Cindy M -WordMVP-, Dec 24, 2003
    #4
  5. Picho

    Picho Guest

    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-" <> wrote in message
    news:VA.00008fd6.006149d2@speedy...
    > 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)fieldsVector;
    > >
    > > object oFileName = "doc123";//aDoc.Name;
    > >
    > > aDoc.MailMerge.CreateDataSource(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.ActiveDocument;
    > >
    > > Word.Table table = aTable.Tables.Item(1);
    > >
    > > for (int j=1;j<dataSource.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
    >
     
    Picho, Dec 24, 2003
    #5
  6. 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. CreateDataSource 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
     
    Cindy M -WordMVP-, Dec 25, 2003
    #6
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephane Belzile

    ASP impersonation problem with word automation

    Stephane Belzile, Oct 30, 2003, in forum: Microsoft Dot NET
    Replies:
    0
    Views:
    243
    Stephane Belzile
    Oct 30, 2003
  2. Mark Overstreet
    Replies:
    1
    Views:
    180
    Steve Drake
    Nov 20, 2003
  3. R1gg4
    Replies:
    0
    Views:
    212
    R1gg4
    Aug 10, 2006
  4. amit
    Replies:
    1
    Views:
    297
    AMDRIT
    Apr 3, 2007
  5. apondu
    Replies:
    0
    Views:
    222
    apondu
    Jul 19, 2007
Loading...

Share This Page