Word VBA-macro's - tabellen: toevoegen, selecteren, herhalen, invoegen vanuit Excel

Tabel toevoegen aan Word-document

Deze eenvoudige macro voegt een tabel toe aan uw Word-document:

Sub VerySimpleTableAdd() Dim oTable As Table Set oTable = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=3, NumColumns:=3) End Sub

Tabel selecteren in Word

Deze macro selecteert de eerste tabel in het actieve Word-document:

Sub SelectTable() 'selecteert eerste tabel in actieve doc. If ActiveDocument.Tables.Count > 0 Then' om fouten te voorkomen, controleren we of er een tabel bestaat in actieve doc ActiveDocument.Tables(1).Selecteer End If End Sub

Loop door alle cellen in een tabel

Deze VBA-macro loopt door alle cellen in een tabel en schrijft het aantal cellen naar de cel:

Sub TableCycling() ' loop door alle cellen in tabel Dim nCounter As Long ' dit wordt in alle tabelcellen geschreven Dim oTable As Table Dim oRow As Row Dim oCell As Cell ActiveDocument.Range.InsertParagraphAfter 'maakt gewoon een nieuwe para aan het einde van doc , Tabel wordt hier gemaakt Stel oTable = ActiveDocument.Tables.Add(Range:=ActiveDocument.Paragraphs.Last.Range, NumRows:=3, NumColumns:=3) 'creëer tabel en wijs deze toe aan variabele For Each oRow In oTable. De buitenste lus van rijen gaat door rijen Voor elke oCell In oRow.Cells 'binnenste lus gaat nCounter = nCounter + 1 'verhoogt de teller oCell.Range.Text = nCounter' schrijft teller naar de cel Volgende oCell Volgende oRow 'toon resultaat van cel van tweede kolom in tweede rij Dim strTemp As String strTemp = oTable.Cell(2, 2).Range.Text MsgBox strTemp End Sub

Maak een Word-tabel vanuit een Excel-bestand

Dit VBA-voorbeeld maakt een tabel van een Excel-bestand:

Sub MakeTablefromExcelFile() 'advanced Dim oExcelApp, oExcelWorkbook, oExcelWorksheet, oExcelRange Dim nNumOfRows As Long Dim nNumOfCols As Long Dim strFile As String Dim oTable As Table 'word table Dim oRow As RowoCword As Long, y As Long 'counter for loops strFile = "c:\Users\Nenad\Desktop\BookSample.xlsx" 'wijzig naar het werkelijke pad Set oExcelApp = CreateObject("Excel.Application") oExcelApp.Visible = True Set oExcelWorkbook = oExcelApp.Workbooks.Open(strFile) 'open werkmap en wijs deze toe aan variabele Set oExcelWorksheet = oExcelWorkbook.Worksheets(1) 'ken eerste werkblad toe aan variabele Set oExcelRange = oExcelWorksheet.Range ("A1:C8") nNumOfRows = RoExcelRange. Count nNumOfCols = oExcelRange.Columns.Count ActiveDocument.Range.InsertParagraphNa 'net een nieuwe para aan het einde van het document te hebben gemaakt, wordt de tabel hier gemaakt. Stel oTable = ActiveDocument.Tables.Add(Range:=ActiveDocument.Paragraphs.Last.Rwww =nNumOfRows, NumColumns:=nNumOfCols) 'creat e tabel en wijs deze toe aan variabele '***real deal, tabel wordt hier gevuld For x = 1 To nNumOfRows For y = 1 To nNumOfCols oTable.Cell(x, y).Range.Text = oExcelRange.Cells(x, y ).Waarde Volgende y Volgende x '*** oExcelWorkbook.Close False oExcelApp.Quit With oTable.Rows(1).Range 'we kunnen nu wat schoonheid op onze tafel toepassen :) .Shading.Texture = wdTextureNone .Shading.ForegroundPatternColor = wdColorAutomatic .Shading.BackgroundPatternColor = wdColorYellow End With End Sub

U zal helpen de ontwikkeling van de site, het delen van de pagina met je vrienden

wave wave wave wave wave