本帖最后由 hong90342 于 2025-12-18 11:45 编辑
VBA把快表明细表写入到word表格中
适用版本:企业版V989
说明:放到对应事件/按钮下执行Sub InsertTableRows();
- Sub InsertTableRows()
- Dim _word As Object = AutomationFactory.CreateObject("Word.Application")
- _word.visible = True
- _word.Documents.open("d:\word\demo.docx")'ActiveDocument
- ActiveWorkbook.WorkbookSet.GetLock()
- _Word.ActiveDocument.bookmarks("BT").Range.Text = Range("F_62861").Cells(0, 0).Text
- Dim tb = _word.ActiveDocument.Tables(1)
- Dim AB2 As Integer
- Dim y2 As Integer
- AB2 = Range("F_62855").Rows.Count -2
- '------循环插入表格行数-----
- For y2 = 1 To AB2
- tb.rows.add()
- Next
- '------循环表格内容-----
- Dim ii As Integer
- For ii = 1 To tb.rows.count
-
- tb.Cell(ii, 1).Range.Text = Range("F_62855").Cells(ii -2, 0).Text
- tb.Cell(ii, 2).Range.Text = Range("F_62856").Cells(ii -2 , 0).Text
- tb.Cell(ii, 3).Range.Text = Range("F_62857").Cells(ii -2, 0).Text
- tb.Cell(ii, 4).Range.Text = Range("F_62858").Cells(ii -2, 0).Text
- tb.Cell(ii, 5).Range.Text = Range("F_62859").Cells(ii -2, 0).Text
- tb.Cell(ii, 6).Range.Text = Range("F_62860").Cells(ii -2, 0).Text
- Next
- ActiveWorkbook.WorkbookSet.ReleaseLock()
- End Sub
复制代码
|
|
hong90342