Automatizace aplikace Microsoft Word k provedení hromadné korespondence z visual c #

Microsoft Visual Basic .NET verzi tohoto článku naleznete v tématu 301656.

Souhrn

Tento článek ukazuje, jak automatizovat aplikaci Microsoft Word k vytvoření dokumentu hromadné korespondence pomocí microsoft Visual C# 2005 nebo Microsoft Visual C# .NET.

Další informace

Automatizace je proces, který umožňuje aplikacím napsaným v jazycích, jako je Visual C# 2005 nebo Visual C# .NET, programově řídit jiné aplikace. Automatizace Wordu umožňuje provádět akce, jako je vytváření nových dokumentů, přidávání textu do dokumentů a formátování dokumentů. Ve Wordu a dalších aplikacích Microsoft Office je možné prostřednictvím automatizace provádět prakticky všechny akce, které můžete provádět ručně prostřednictvím uživatelského rozhraní.

Word zpřístupňuje tuto programovou funkci prostřednictvím objektového modelu. Objektový model je kolekce tříd a metod, které slouží jako protějšky logických součástí aplikace Word. Například existuje objekt Application, objekt Document a objekt Paragraph, z nichž každá obsahuje funkce těchto komponent ve Wordu. Pokud chcete získat přístup k objektového modelu z Visual C# 2005 nebo Visual C# .NET, můžete nastavit odkaz na projekt na knihovnu typů.

Tento článek ukazuje, jak nastavit správný odkaz na projekt na knihovnu typů wordu pro Visual C# .NET a poskytuje ukázkový kód pro automatizaci Wordu.

Vytvoření ukázky automation

  1. Spusťte Microsoft Visual Studio 2005 nebo Microsoft Visual Studio .NET.

  2. V nabídce Soubor klikněte na Nový a potom klikněte na Project. V typech projektů Visual C# vyberte aplikaci pro Windows. Formulář1 je ve výchozím nastavení vytvořen.

  3. Přidejte odkaz na knihovnu objektů Microsoft Wordu 11.0 v sadě Visual Studio 2005 nebo do knihovny objektů Microsoft Wordu v sadě Visual Studio .NET. Postupujte takto:

    1. V nabídce Projekt klikněte na Přidat odkaz.

    2. Na kartě COM vyhledejte Microsoft Word Object Library a klepněte na tlačítko Vybrat.

      V sadě Visual Studio 2005 vyhledejte microsoft Word 11.0 Object Library na kartě COM .

      Poznámka Microsoft Office 2003 obsahuje primární sestavení vzájemné spolupráce (PIA). Sada Microsoft Office XP neobsahuje osobní údaje, ale je možné je stáhnout.

    3. Kliknutím na tlačítko OK v dialogovém okně Přidat odkazy potvrďte výběry.

  4. V nabídce Zobrazení vyberte sadu nástrojů, aby se zobrazila sada nástrojů, a pak přidejte tlačítko do formuláře Form1.

  5. Poklikáte na Tlačítko1. Zobrazí se okno kódu formuláře.

  6. V okně kódu nahraďte následující kód.

      private void button1_Click(object sender, System.EventArgs e)
      {
      }
    
    

    S:

    Word.Application wrdApp;
    Word._Document wrdDoc;
    Object oMissing = System.Reflection.Missing.Value;
    Object oFalse = false;
    
    private void InsertLines(int LineNum)
    {
    int iCount;
    
    // Insert "LineNum" blank lines.
    for(iCount = 1; iCount<=LineNum; iCount++) 
    {
    wrdApp.Selection.TypeParagraph();
    }
    }
    
    private void FillRow(Word._Document oDoc, int Row, string Text1,
    string Text2, string Text3, string Text4)
    {
    // Insert the data into the specific cell.
    oDoc.Tables[1].Cell(Row,1).Range.InsertAfter(Text1);
    oDoc.Tables[1].Cell(Row,2).Range.InsertAfter(Text2);
    oDoc.Tables[1].Cell(Row,3).Range.InsertAfter(Text3);
    oDoc.Tables[1].Cell(Row,4).Range.InsertAfter(Text4);
    }
    
    private void CreateMailMergeDataFile()
    {
    Word._Document oDataDoc;
    int iCount;
    
    Object oName = "C:\\DataDoc.doc";
    Object oHeader = "FirstName, LastName, Address, CityStateZip";
    wrdDoc.MailMerge.CreateDataSource(ref oName,ref oMissing, 
    ref oMissing,ref oHeader, ref oMissing, ref oMissing, 
    ref oMissing, ref oMissing, ref oMissing);
    
    // Open the file to insert data.
    oDataDoc = wrdApp.Documents.Open(ref oName,ref oMissing,
    ref oMissing, ref oMissing,ref oMissing,ref oMissing,
    ref oMissing,ref oMissing,ref oMissing,ref oMissing,
    ref oMissing,ref oMissing,ref oMissing,ref oMissing,
    ref oMissing/*, ref oMissing */);
    
    for (iCount=1; iCount<=2; iCount++)
    {
    oDataDoc.Tables[1].Rows.Add(ref oMissing);
    }
    // Fill in the data.
    FillRow(oDataDoc, 2, "Steve", "DeBroux", 
    "4567 Main Street", "Buffalo, NY  98052");
    FillRow(oDataDoc, 3, "Jan", "Miksovsky", 
    "1234 5th Street", "Charlotte, NC  98765");
    FillRow(oDataDoc, 4, "Brian", "Valentine", 
    "12348 78th Street  Apt. 214", 
    "Lubbock, TX  25874");
    // Save and close the file.
    oDataDoc.Save();
    oDataDoc.Close(ref oFalse, ref oMissing, ref oMissing);
    }
    
    private void button1_Click(object sender, System.EventArgs e)
    {
    Word.Selection wrdSelection;
    Word.MailMerge wrdMailMerge;
    Word.MailMergeFields wrdMergeFields;
    Word.Table wrdTable;
    string StrToAdd;
    
    // Create an instance of Word  and make it visible.
    wrdApp = new Word.Application();
    wrdApp.Visible = true;
    
    // Add a new document.
    wrdDoc = wrdApp.Documents.Add(ref oMissing,ref oMissing,
    ref oMissing,ref oMissing);
    wrdDoc.Select();
    
    wrdSelection = wrdApp.Selection;
    wrdMailMerge = wrdDoc.MailMerge;
    
    // Create a MailMerge Data file.
    CreateMailMergeDataFile();
    
    // Create a string and insert it into the document.
    StrToAdd = "State University\r\nElectrical Engineering Department";
    wrdSelection.ParagraphFormat.Alignment  = 
    Word.WdParagraphAlignment.wdAlignParagraphCenter;
    wrdSelection.TypeText(StrToAdd);
    
    InsertLines(4);
    
    // Insert merge data.
    wrdSelection.ParagraphFormat.Alignment = 
    Word.WdParagraphAlignment.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 = 
    Word.WdParagraphAlignment.wdAlignParagraphRight;
    
    Object objDate = "dddd, MMMM dd, yyyy";
    wrdSelection.InsertDateTime(ref objDate,ref oFalse,ref oMissing, 
    ref oMissing, ref oMissing);
    
    InsertLines(2);
    
    // Justify the rest of the document.
    wrdSelection.ParagraphFormat.Alignment = 
    Word.WdParagraphAlignment.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.
    wrdTable = wrdDoc.Tables.Add(wrdSelection.Range,9,4, 
    ref oMissing, ref oMissing);
    // Set the column widths.
    wrdTable.Columns[1].SetWidth(51, Word.WdRulerStyle.wdAdjustNone);
    wrdTable.Columns[2].SetWidth(170, Word.WdRulerStyle.wdAdjustNone);
    wrdTable.Columns[3].SetWidth(100, Word.WdRulerStyle.wdAdjustNone);
    wrdTable.Columns[4].SetWidth(111, Word.WdRulerStyle.wdAdjustNone);
    // Set the shading on the first row to light gray.
    wrdTable.Rows[1].Cells.Shading.BackgroundPatternColorIndex = 
    Word.WdColorIndex.wdGray25;
    // Bold the first row.
    wrdTable.Rows[1].Range.Bold = 1;
    // Center the text in Cell (1,1).
    wrdTable.Cell(1, 1).Range.Paragraphs.Alignment = 
    Word.WdParagraphAlignment.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.
    Object oConst1 = Word.WdGoToItem.wdGoToLine;
    Object oConst2 = Word.WdGoToDirection.wdGoToLast;
    wrdApp.Selection.GoTo(ref oConst1,ref oConst2,ref oMissing,ref oMissing);
    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 Web site at ";
    wrdSelection.TypeText(StrToAdd);
    // Insert a hyperlink to the Web page.
    Object oAddress = "http://www.ee.stateu.tld";
    Object oRange = wrdSelection.Range;
    wrdSelection.Hyperlinks.Add(oRange, ref oAddress,ref oMissing,
    ref oMissing, ref oMissing, ref oMissing);
    // 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.\r\n\r\n"  +
    "Sincerely,\r\n\r\n" +
    "Kathryn M. Hinsch\r\n" +
    "Department of Electrical Engineering \r\n";
    wrdSelection.TypeText(StrToAdd);
    
    // Perform mail merge.
    wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;
    wrdMailMerge.Execute(ref oFalse);
    
    // Close the original form document.
    wrdDoc.Saved = true;
    wrdDoc.Close(ref oFalse,ref oMissing,ref oMissing);
    
    // Release References.
    wrdSelection = null;
    wrdMailMerge = null;
    wrdMergeFields = null;
    wrdDoc = null;
    wrdApp = null;
    
    // Clean up temp file.
    System.IO.File.Delete("C:\\DataDoc.doc");
    } 
    
    

    Poznámka Musíte změnit kód v sadě Visual Studio 2005. Visual C# ve výchozím nastavení přidá jeden formulář do projektu při vytváření projektu model Windows Forms. Formulář má název Form1. Dva soubory, které představují formulář, mají název Form1.cs a Form1.designer.cs. Kód napíšete v Souboru Form1.cs. Soubor Form1.designer.cs je místo, kde návrhář model Windows Forms zapíše kód, který implementuje všechny akce, které jste provedli přetažením ovládacích prvků z panelu nástrojů.

    Další informace o model Windows Forms Designer v jazyce Visual C# 2005 naleznete na následujícím webu Microsoft Developer Network (MSDN): Creating a Project (Visual C#)Note Microsoft Office Word 2003 has a additional argument for the Open method of the document. Pokud používáte WORD 2003 PIA, odeberte zápis komentáře pro další parametr metody Open.

  7. Do horní části souboru Form1.cs přidejte následující položky:

    using Word = Microsoft.Office.Interop.Word;
    
    
  8. Stisknutím klávesy F5 sestavte a spusťte program.

  9. Kliknutím na Tlačítko1 spustíte automatizaci Wordu a provedete hromadnou korespondenci.

Odkazy

Další informace naleznete na následujícím webu Microsoft Developer Network (MSDN): Microsoft Office Development with Visual Studio Microsoft Office Development with Visual Studio

Další informace získáte v následujícím článku znalostní báze Microsoft Knowledge Base:

285332 Jak automatizovat Word 2002 pomocí jazyka Visual Basic vytvořit hromadnou korespondenci