如何在VBA中用Range.Paste方法为Word插入的Excel表格添加边框
问题描述
我正在通过VBA将Excel中的表格复制到Word文档中,现需在同一个子程序中为该插入的表格添加边框。以下是我当前使用的代码,恳请提供解决方案。
当前代码
Dim ObjWord As Object Dim ws As Worksheet Set ObjWord = CreateObject("Word.Application") ObjWord.Visible = True Dim docWord As Object Dim oCC As ContentControl ObjWord.Activate Set docWord = ObjWord.Documents.Open(Filename:="\\\CPFSPRDAPAWSE01\userdata$\NTiwari\Desktop\Automations\abc Memo.docx", ReadOnly:=False) docWord.Activate For Each oCC In ActiveDocument.ContentControls Select Case oCC.Title Case "IssuerName" 'This is the Title being referenced for CC in Word oCC.Range.Text = "abc" & Right(xyz, 3) & " pqr" 'This is the named cell being referenced in excel Case "CommitteeType" oCC.LockContents = False oCC.DropdownListEntries(2).Select oCC.LockContents = True Case "CommitteeDate" oCC.Range.Text = Date Case "Methodology" oCC.Range.Text = "fgs" Case "Methodology2" oCC.Range.Text = "NA" Case "ESGYesNo" oCC.DropdownListEntries(1).Select Case "ESGComments" oCC.DropdownListEntries(1).Select Case "FinancialObsYesNo" oCC.DropdownListEntries(1).Select Case "ContractualPayObsYesNo" oCC.DropdownListEntries(1).Select Case "DataChecksYesNo" oCC.DropdownListEntries(1).Select Case "DataCheckResult" oCC.DropdownListEntries(1).Select Case "FinancialObsYesNo" oCC.DropdownListEntries(1).Select Case "RatingAction" oCC.DropdownListEntries(2).Select Case "BasisForAction" oCC.Range.Text = "text" End Select Next oCC ThisWorkbook.Sheets("Participants - C").Range("C7:D13").Copy docWord.Bookmarks("TransParties").Range.Paste docWord.SaveAs "\\CPFSPRDAPAWSE01\userdata$\NTiwari\Desktop\Automations\abc Docs\" & "DealName " & " abc Memo.docx" docWord.Close End Sub
解决方案
在粘贴表格后、保存文档前,添加代码获取插入的表格并设置边框。由于表格粘贴到了TransParties书签位置,可通过书签范围的Tables集合定位目标表格,然后统一配置边框样式:
修改后的完整代码(含边框设置)
Dim ObjWord As Object Dim ws As Worksheet Set ObjWord = CreateObject("Word.Application") ObjWord.Visible = True Dim docWord As Object Dim oCC As ContentControl ObjWord.Activate Set docWord = ObjWord.Documents.Open(Filename:="\\\CPFSPRDAPAWSE01\userdata$\NTiwari\Desktop\Automations\abc Memo.docx", ReadOnly:=False) docWord.Activate For Each oCC In ActiveDocument.ContentControls Select Case oCC.Title Case "IssuerName" 'This is the Title being referenced for CC in Word oCC.Range.Text = "abc" & Right(xyz, 3) & " pqr" 'This is the named cell being referenced in excel Case "CommitteeType" oCC.LockContents = False oCC.DropdownListEntries(2).Select oCC.LockContents = True Case "CommitteeDate" oCC.Range.Text = Date Case "Methodology" oCC.Range.Text = "fgs" Case "Methodology2" oCC.Range.Text = "NA" Case "ESGYesNo" oCC.DropdownListEntries(1).Select Case "ESGComments" oCC.DropdownListEntries(1).Select Case "FinancialObsYesNo" oCC.DropdownListEntries(1).Select Case "ContractualPayObsYesNo" oCC.DropdownListEntries(1).Select Case "DataChecksYesNo" oCC.DropdownListEntries(1).Select Case "DataCheckResult" oCC.DropdownListEntries(1).Select Case "FinancialObsYesNo" oCC.DropdownListEntries(1).Select Case "RatingAction" oCC.DropdownListEntries(2).Select Case "BasisForAction" oCC.Range.Text = "text" End Select Next oCC ThisWorkbook.Sheets("Participants - C").Range("C7:D13").Copy docWord.Bookmarks("TransParties").Range.Paste ' ---------- 添加表格边框的代码开始 ---------- Dim targetTable As Object ' 获取书签位置内的表格(假设粘贴后书签范围内只有一个表格) Set targetTable = docWord.Bookmarks("TransParties").Range.Tables(1) ' 设置表格所有边框的样式、粗细和颜色 With targetTable.Borders .LineStyle = 1 ' 实线(对应Word常量wdLineStyleSingle) .LineWidth = 2 ' 0.5磅粗细(对应Word常量wdLineWidth05pt) .Color = RGB(0, 0, 0) ' 黑色边框 End With ' ---------- 添加表格边框的代码结束 ---------- docWord.SaveAs "\\CPFSPRDAPAWSE01\userdata$\NTiwari\Desktop\Automations\abc Docs\" & "DealName " & " abc Memo.docx" docWord.Close End Sub
关键说明
- 若书签范围内可能存在多个表格,可调整
Tables(1)的索引,或通过循环遍历表格确认目标; - 边框参数可按需修改:
LineStyle:0(无框)、1(实线)、2(虚线)等;LineWidth:1(0.25磅)、2(0.5磅)、4(1磅)等;Color:通过RGB(r,g,b)设置自定义颜色。
内容的提问来源于stack exchange,提问作者Nikita Tiwari
相关产品推荐
相关产品推荐

