Microsoft Word automatiseren met Behulp van Visual Basic om een nieuw document te maken

Samenvatting

In dit stapsgewijze artikel wordt beschreven hoe u een nieuw document maakt in Word met behulp van Automation van Visual Basic.

Voorbeeldcode

De voorbeeldcode in dit artikel laat zien hoe u het volgende doet:

  • Alinea's met tekst en opmaak invoegen.
  • Door verschillende bereiken in een document bladeren en wijzigen.
  • Tabellen invoegen, tabellen opmaken en de tabellen vullen met gegevens.
  • Voeg een grafiek toe.

Als u een nieuw Word-document wilt maken met behulp van Automation van Visual Basic, voert u de volgende stappen uit:

  1. Maak in Visual Basic een nieuw Standard EXE-project. Form1 wordt standaard gemaakt.

  2. Klik in het menu Project opVerwijzingen, klik op een van de volgende opties en klik vervolgens op OK:

    • Klik voor Office Word 2007 op Microsoft Word 12.0-objectbibliotheek.
    • Klik voor Word 2003 op Microsoft Word 11.0-objectbibliotheek.
    • Klik voor Word 2002 op Microsoft Word 10.0-objectbibliotheek.
    • Klik voor Word 2000 op Microsoft Word 9.0-objectbibliotheek.
  3. Voeg een CommandButton-besturingselement toe aan Form1.

  4. Voeg de volgende code toe aan de klik-gebeurtenis voor Command1:

     Dim oWord As Word.Application
     Dim oDoc As Word.Document
     Dim oTable As Word.Table
     Dim oPara1 As Word.Paragraph, oPara2 As Word.Paragraph
     Dim oPara3 As Word.Paragraph, oPara4 As Word.Paragraph
     Dim oRng As Word.Range
     Dim oShape As Word.InlineShape
     Dim oChart As Object
     Dim Pos as Double
    
     'Start Word and open the document template.
     Set oWord = CreateObject("Word.Application")
     oWord.Visible = True
     Set oDoc = oWord.Documents.Add
    
     'Insert a paragraph at the beginning of the document.
     Set oPara1 = oDoc.Content.Paragraphs.Add
     oPara1.Range.Text = "Heading 1"
     oPara1.Range.Font.Bold = True
     oPara1.Format.SpaceAfter = 24    '24 pt spacing after paragraph.
     oPara1.Range.InsertParagraphAfter
    
     'Insert a paragraph at the end of the document.
     '** \endofdoc is a predefined bookmark.
     Set oPara2 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks("\endofdoc").Range)
     oPara2.Range.Text = "Heading 2"
     oPara2.Format.SpaceAfter = 6
     oPara2.Range.InsertParagraphAfter
    
     'Insert another paragraph.
     Set oPara3 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks("\endofdoc").Range)
     oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:"
     oPara3.Range.Font.Bold = False
     oPara3.Format.SpaceAfter = 24
     oPara3.Range.InsertParagraphAfter
    
     'Insert a 3 x 5 table, fill it with data and make the first row
     'bold,italic.
     Dim r As Integer, c As Integer
     Set oTable = oDoc.Tables.Add(oDoc.Bookmarks("\endofdoc").Range, 3, 5)
     oTable.Range.ParagraphFormat.SpaceAfter = 6
     For r = 1 To 3
         For c = 1 To 5
             oTable.Cell(r, c).Range.Text = "r" & r & "c" & c
         Next
     Next
     oTable.Rows(1).Range.Font.Bold = True
     oTable.Rows(1).Range.Font.Italic = True
    
     'Add some text after the table.
     'oTable.Range.InsertParagraphAfter
     Set oPara4 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks("\endofdoc").Range)
     oPara4.Range.InsertParagraphBefore
     oPara4.Range.Text = "And here's another table:"
     oPara4.Format.SpaceAfter = 24
     oPara4.Range.InsertParagraphAfter
    
     'Insert a 5 x 2 table, fill it with data and change the column widths.
     Set oTable = oDoc.Tables.Add(oDoc.Bookmarks("\endofdoc").Range, 5, 2)
     oTable.Range.ParagraphFormat.SpaceAfter = 6
     For r = 1 To 5
         For c = 1 To 2
             oTable.Cell(r, c).Range.Text = "r" & r & "c" & c
         Next
     Next
     oTable.Columns(1).Width = oWord.InchesToPoints(2)   'Change width of columns 1 & 2.
     oTable.Columns(2).Width = oWord.InchesToPoints(3)
    
     'Keep inserting text. When you get to 7 inches from top of the
     'document, insert a hard page break.
     Pos = oWord.InchesToPoints(7)
     oDoc.Bookmarks("\endofdoc").Range.InsertParagraphAfter
     Do
         Set oRng = oDoc.Bookmarks("\endofdoc").Range
         oRng.ParagraphFormat.SpaceAfter = 6
         oRng.InsertAfter "A line of text"
         oRng.InsertParagraphAfter
     Loop While Pos >= oRng.Information(wdVerticalPositionRelativeToPage)
     oRng.Collapse (wdCollapseEnd)
     oRng.InsertBreak wdPageBreak
     oRng.Collapse wdCollapseEnd
     oRng.InsertAfter "We're now on page 2. Here's my chart:"
     oRng.InsertParagraphAfter
    
     'Insert a chart and change the chart.
     Set oShape = oDoc.Bookmarks("\endofdoc").Range.InlineShapes.AddOLEObject( _
         ClassType:="MSGraph.Chart.8", FileName _
         :="", LinkToFile:=False, DisplayAsIcon:=False)
     Set oChart = oShape.OLEFormat.Object
     oChart.charttype = 4 'xlLine = 4
     oChart.Application.Update
     oChart.Application.Quit
     '... If desired, you can proceed from here using the Microsoft Graph 
     'Object model on the oChart object to make additional changes to the
     'chart.
     oShape.Width = oWord.InchesToPoints(6.25)
     oShape.Height = oWord.InchesToPoints(3.57)
    
     'Add text after the chart.
     Set oRng = oDoc.Bookmarks("\endofdoc").Range
     oRng.InsertParagraphAfter
     oRng.InsertAfter "THE END."
    
     'All done. Unload this form.
     Unload Me
    
    
  5. Druk op F5 om het programma uit te voeren en klik vervolgens op Command1.

Nadat de code is voltooid, bekijkt u het document dat voor u is gemaakt. Het document bevat twee pagina's met opgemaakte alinea's, tabellen en een grafiek.

Een sjabloon gebruiken

Als u Automation gebruikt om documenten te bouwen die allemaal een gemeenschappelijke indeling hebben, kunt u het proces starten met een nieuw document dat is gebaseerd op een vooraf opgemaakte sjabloon. Het gebruik van een sjabloon met uw Word Automation-client heeft twee belangrijke voordelen ten opzichte van het bouwen van een document vanuit niets:

  • U kunt meer controle hebben over de opmaak en plaatsing van objecten in uw documenten.
  • U kunt uw documenten bouwen met minder code.

Met behulp van een sjabloon kunt u de plaatsing van tabellen, alinea's en andere objecten in het document verfijnen en opmaak toevoegen aan die objecten. Met behulp van Automation kunt u een nieuw document maken op basis van uw sjabloon met code zoals:

oWord.Documents.Add "<Path to your template>\MyTemplate.dot"

In uw sjabloon kunt u bladwijzers definiƫren zodat uw Automation-client als volgt variabele tekst op een specifieke locatie in het document kan invullen:

oDoc.Bookmarks("MyBookmark").Range.Text = "Some Text Here"

Een ander voordeel van het gebruik van een sjabloon is dat u als volgt opmaakstijlen kunt maken en opslaan die u tijdens runtime wilt toepassen:

oDoc.Bookmarks("MyBookmark").Range.Style = "MyStyle"

of

oWord.Selection.Style = "MyStyle"

Verwijzingen

Klik voor meer informatie op de onderstaande artikelnummers om de artikelen in de Microsoft Knowledge Base weer te geven:

285332 Word 2002 automatiseren met Visual Basic voor het maken van afdruk samenvoegen

Microsoft Office-ontwikkeling met Visual Studio

(c) Microsoft Corporation 2001, Alle rechten voorbehouden. Bijdragen van Lync B. Turner, Microsoft Corporation.