Closed Thread
Results 1 to 7 of 7

Thread: Mail Merge Delphi

  1. #1
    2710 is offline Programmer
    Join Date
    Sep 2008
    Posts
    108
    Rep Power
    0

    Mail Merge Delphi

    Code:
    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;
    
    type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
        procedure InsertLines(LineNum : Integer);
        procedure CreateMailMergeDataFile;
        procedure FillRow(Doc : Variant; Row : Integer;
                     Text1,Text2,Text3,Text4 : String);
      private
        { Private declarations }
    
      public
        wrdApp, wrdDoc: Variant;
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    uses ComObj;
    
    Const wdAlignParagraphLeft = 0;
    Const wdAlignParagraphCenter = 1;
    Const wdAlignParagraphRight = 2;
    Const wdAlignParagraphJustify = 3;
    Const wdAdjustNone = 0;
    Const wdGray25 = 16;
    Const wdGoToLine = 3;
    Const wdGoToLast = -1;
    Const wdSendToNewDocument = 0;
    
    {$R *.DFM}
    
    procedure TForm1.InsertLines(LineNum : Integer);
    var
      iCount : Integer;
    begin
      for iCount := 1 to LineNum do
         wrdApp.Selection.TypeParagraph;
    end;
    
    procedure TForm1.FillRow(Doc : Variant; Row : Integer;
                     Text1,Text2,Text3,Text4 : String);
    
    begin
      Doc.Tables.Item(1).Cell(Row,1).Range.InsertAfter(Text1);
      Doc.Tables.Item(1).Cell(Row,2).Range.InsertAfter(Text2);
      Doc.Tables.Item(1).Cell(Row,3).Range.InsertAfter(Text3);
      Doc.Tables.Item(1).Cell(Row,4).Range.InsertAfter(Text4);
    end;
    
    procedure TForm1.CreateMailMergeDataFile;
    var
      wrdDataDoc : Variant;
      iCount : Integer;
    begin
      // Create a data source at C:\DataDoc.doc containing the field data
      wrdDoc.MailMerge.CreateDataSource('C:\DataDoc.doc',,,'FirstName, LastName,' +
           ' Address, CityStateZip');
      // Open the file to insert data
      wrdDataDoc := wrdApp.Documents.Open('C:\DataDoc.doc');
      for iCount := 1 to 2 do
        wrdDataDoc.Tables.Item(1).Rows.Add;
      // Fill in the data
      FillRow(wrdDataDoc, 2, 'Steve', 'DeBroux',
            '4567 Main Street', 'Buffalo, NY  98052');
      FillRow(wrdDataDoc, 3, 'Jan', 'Miksovsky',
            '1234 5th Street', 'Charlotte, NC  98765');
      FillRow(wrdDataDoc, 4, 'Brian', 'Valentine',
            '12348 78th Street  Apt. 214', 'Lubbock, TX  25874');
      // Save and close the file
      wrdDataDoc.Save;
      wrdDataDoc.Close(False);
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      StrToAdd : String;
      wrdSelection, wrdMailMerge, wrdMergeFields : Variant;
    begin
      // Create an instance of Word and make it visible
      wrdApp := CreateOleObject('Word.Application');
      wrdApp.Visible := True;
      // Create a new document
      wrdDoc := wrdApp.Documents.Add();
      wrdDoc.Select;
    
      wrdSelection := wrdApp.Selection;
      wrdMailMerge := wrdDoc.MailMerge;
    
      // Create MailMerge data file
      CreateMailMergeDataFile;
    
    
      // Create a string and insert it into the document
      StrToAdd := 'State University' + Chr(13) +
                  'Electrical Engineering Department';
      wrdSelection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
      wrdSelection.TypeText(StrToAdd);
    
      InsertLines(4);
    
      // Insert Merge Data
      wrdSelection.ParagraphFormat.Alignment := wdAlignParagraphLeft;
      wrdMergeFields := wrdMailMerge.Fields;
    
      wrdMergeFields.Add(wrdSelection.Range,'FirstName');
      wrdSelection.TypeText(' ');
      wrdMergeFields.Add(wrdSelection.Range,'LastName');
      wrdSelection.TypeParagraph;
      wrdMergeFields.Add(wrdSelection.Range,'Address');
      wrdSelection.TypeParagraph;
      wrdMergeFields.Add(wrdSelection.Range,'CityStateZip');
    
      InsertLines(2);
    
      // Right justify the line and insert a date field with
      // the current date
      wrdSelection.ParagraphFormat.Alignment := wdAlignParagraphRight;
      wrdSelection.InsertDateTime('dddd, MMMM dd, yyyy',False);
    
      InsertLines(2);
    
      // Justify the rest of the document
      wrdSelection.ParagraphFormat.Alignment := wdAlignParagraphJustify;
    
      wrdSelection.TypeText('Dear ');
      wrdMergeFields.Add(wrdSelection.Range,'FirstName');
    
      wrdSelection.TypeText(',');
      InsertLines(2);
    
      // Create a string and insert it into the document
      StrToAdd := 'Thank you for your recent request for next ' +
          'semester''s class schedule for the Electrical ' +
          'Engineering Department.  Enclosed with this ' +
          'letter is a booklet containing all the classes ' +
          'offered next semester at State University.  ' +
          'Several new classes will be offered in the ' +
          'Electrical Engineering Department next semester.  ' +
          'These classes are listed below.';
      wrdSelection.TypeText(StrToAdd);
    
      InsertLines(2);
    
      // Insert a new table with 9 rows and 4 columns
      wrdDoc.Tables.Add(wrdSelection.Range,9,4);
      wrdDoc.Tables.Item(1).Columns.Item(1).SetWidth(51,wdAdjustNone);
      wrdDoc.Tables.Item(1).Columns.Item(2).SetWidth(170,wdAdjustNone);
      wrdDoc.Tables.Item(1).Columns.Item(3).SetWidth(100,wdAdjustNone);
      wrdDoc.Tables.Item(1).Columns.Item(4).SetWidth(111,wdAdjustNone);
      // Set the shading on the first row to light gray
    
      wrdDoc.Tables.Item(1).Rows.Item(1).Cells
          .Shading.BackgroundPatternColorIndex := wdGray25;
      // BOLD the first row
      wrdDoc.Tables.Item(1).Rows.Item(1).Range.Bold := True;
      // Center the text in Cell (1,1)
      wrdDoc.Tables.Item(1).Cell(1,1).Range.Paragraphs.Alignment :=
            wdAlignParagraphCenter;
    
      // Fill each row of the table with data
      FillRow(wrdDoc, 1, 'Class Number', 'Class Name', 'Class Time', 
         'Instructor');
      FillRow(wrdDoc, 2, 'EE220', 'Introduction to Electronics II',
         '1:00-2:00 M,W,F', 'Dr. Jensen');
      FillRow(wrdDoc, 3, 'EE230', 'Electromagnetic Field Theory I',
         '10:00-11:30 T,T', 'Dr. Crump');
      FillRow(wrdDoc, 4, 'EE300', 'Feedback Control Systems',
         '9:00-10:00 M,W,F', 'Dr. Murdy');
      FillRow(wrdDoc, 5, 'EE325', 'Advanced Digital Design',
         '9:00-10:30 T,T', 'Dr. Alley');
      FillRow(wrdDoc, 6, 'EE350', 'Advanced Communication Systems',
         '9:00-10:30 T,T', 'Dr. Taylor');
      FillRow(wrdDoc, 7, 'EE400', 'Advanced Microwave Theory',
         '1:00-2:30 T,T', 'Dr. Lee');
      FillRow(wrdDoc, 8, 'EE450', 'Plasma Theory',
         '1:00-2:00 M,W,F', 'Dr. Davis');
      FillRow(wrdDoc, 9, 'EE500', 'Principles of VLSI Design',
         '3:00-4:00 M,W,F', 'Dr. Ellison');
    
      // Go to the end of the document
    
      wrdApp.Selection.GoTo(wdGotoLine,wdGoToLast);
      InsertLines(2);
    
      // Create a string and insert it into the document
      StrToAdd := 'For additional information regarding the ' +
                 'Department of Electrical Engineering, ' +
                 'you can visit our website at ';
      wrdSelection.TypeText(StrToAdd);
      // Insert a hyperlink to the web page
      wrdSelection.Hyperlinks.Add(wrdSelection.Range,'http://www.ee.stateu.tld');
      // Create a string and insert it into the document
      StrToAdd := '.  Thank you for your interest in the classes ' +
                 'offered in the Department of Electrical ' +
                 'Engineering.  If you have any other questions, ' +
                 'please feel free to give us a call at ' +
                 '555-1212.' + Chr(13) + Chr(13) +
                 'Sincerely,' + Chr(13) + Chr(13) +
                 'Kathryn M. Hinsch' + Chr(13) +
                 'Department of Electrical Engineering' + Chr(13);
      wrdSelection.TypeText(StrToAdd);
    
      // Perform mail merge
      wrdMailMerge.Destination := wdSendToNewDocument;
      wrdMailMerge.Execute(False);
    
      // Close the original form document
      wrdDoc.Saved := True;
      wrdDoc.Close(False);
    
      // Notify the user we are done.
      ShowMessage('Mail Merge Complete.');
    
      // Clean up temp file
      DeleteFile('C:\DataDoc.doc');
    
    end;
    
    end.
    So I am studying this piece of code (I am only A-Level Standard) and I need some help. This code was acquired from Microsoft's site for helping with mail merging in MS Word initiated by Delphi.

    1) Starting from the top, I do not understand this:

    Code:
    Doc.Tables.Item(1).Cell(Row,1).Range.InsertAfter(Text1);
    ...
    FillRow(wrdDataDoc, 2, 'Steve', 'DeBroux', '4567 Main Street', 'Buffalo, NY  98052');
    Essentially the code is:

    wrdDataDoc.Tables.Item(1).Cell(2,1).Range.InsertAf ter('Steve');

    I assume the Cell(x,y) is some kind of coordinates system with row and column am I right? Can simple texts that are added be interpreted to have a cell like structure? I am taking a guess that this line:

    wrdDoc.MailMerge.CreateDataSource('C:\DataDoc.doc' ,,,'FirstName, LastName,' + ' Address, CityStateZip');

    is what initiated the cell like structure. What I am saying is, that if you manually inserted the column headers with like a typetext command so that it looks exactly like this mailmerge.createdatasource command, you wouldn't be able to apply the Cell(x,y) command to it would you?

    I have just tried to highlight it (With the mail merge command method), and to my surprised, it highlighted the blank space underneath the headers in blocks, as if it were a table...

    2) Next is the bolded part:

    Code:
    wrdDataDoc := wrdApp.Documents.Open('C:\DataDoc.doc');
      for iCount := 1 to 2 do
        wrdDataDoc.Tables.Item(1).Rows.Add;
    Surely there are 3 pieces of data, wouldnt you need to add 3 rows? I stopped the running of the program here, and tested and found that a row had already been added prior to this piece of code...why is this?

    3) The next thing im wondering is why we needed to create the mailmerge data document when it is never called again in the later code. I have a feeling that it is somehow recorded into the 'mail merge' process through this line:

    wrdDoc.MailMerge.CreateDataSource('C:\DataDoc.doc' ,,,'FirstName, LastName,' + ' Address, CityStateZip');

    Which is then handled by MS Word when the .mailmerge command is used. Am I right?

    4) And also, things like:

    wrdSelection.InsertDateTime('dddd, MMMM dd, yyyy',False);

    Why do they have 'false' at the end?

    5) Is this how MS Word actually does Mail merge? Ie, create another doc, then creates a table and uses that table.

    Thanks. Sorry for all these questions, but I am still learning

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Mail Merge Delphi

    You're basically programmatically telling Word to run a mail merge, and providing it with the data it needs for the merge. You're creating the data for the mailmerge on the fly in your program.

    MS Word has a LOT of options for how to do a mail merge, but yes, this looks legit.

    You'll probably want to look on MSDN for how to program Word. http://msdn.microsoft.com/en-us/libr...ice.10%29.aspx
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    2710 is offline Programmer
    Join Date
    Sep 2008
    Posts
    108
    Rep Power
    0

    Re: Mail Merge Delphi

    I know that delphi is basically telling word to run a mail merge, but I still want to understand the code. So if anyone can answer the questions, that would be helpful, thanks

    And in the meantime, Ill get reading on that link you provided, which looks extremely looong

  5. #4
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Mail Merge Delphi

    Most everything the Delphi code is doing is determined by Word, not Delphi. It's more "Word programming" than "Delphi programming", if that makes any sense.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    2710 is offline Programmer
    Join Date
    Sep 2008
    Posts
    108
    Rep Power
    0

    Re: Mail Merge Delphi

    OK thanks. Do you happen to know the code to change the style of Word? Because in Word 2007, the style is set to 'Normal' and the spaces in between sentences are huuge, and I want to set ti to 'No Spacing'

    Thanks

  7. #6
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Mail Merge Delphi

    Not off hand. I don't like Word
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  8. #7
    2710 is offline Programmer
    Join Date
    Sep 2008
    Posts
    108
    Rep Power
    0

    Re: Mail Merge Delphi

    Ok nevermind, I got it to look nice with the 'normal' Setting

    Does anyone know how bold/italic the writing through delphi?

    I have:

    WrdSelected.TypeText('String....etc')

    How do I Bold/Italics this?

    Thanks

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. document generation/mail merge suggestions
    By xsonline in forum PHP Development
    Replies: 0
    Last Post: 02-03-2011, 05:34 AM
  2. A few problems with Delphi involving Mail Merge, SQL + Databases.
    By Ilikestring in forum Pascal and Delphi
    Replies: 11
    Last Post: 04-20-2010, 08:27 AM
  3. Mail Merge - Data Source with Variable Length
    By Ilikestring in forum Pascal and Delphi
    Replies: 5
    Last Post: 04-18-2010, 06:24 PM
  4. migration from of app delphi 2 to delphi 7
    By porno.shome in forum Pascal and Delphi
    Replies: 3
    Last Post: 04-22-2008, 08:57 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts