热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用VBA将表添加到Word

我正在尝试使用以下代码将Excel中的表添加到word中,但是在使用我创建的书签实

我正在尝试使用以下代码将Excel中的表添加到word中,但是在使用我创建的书签实际将表粘贴到文档中时,我遇到了一个小问题。该代码没有粘贴我想要的表,而是粘贴了以下以灰色突出显示的文本:

Bookmarks("bkChart1").Range

这是我当前正在使用的代码:

Option Explicit
Sub CreateWordDocuments()
Dim CustRow,CustCol,LastRow,TemplRow As Long
Dim WordTable As Word.Table
Dim tbl,sectiontbl,positiontbl,mintbl,avtbl As Excel.Range
Dim DocLoc,TagName,TagValue,TemplName,FileName As String
Dim WordDoc,Wordapp As Object
Dim WordContent As Word.Range
With Sheet1
If .Range("B3").Value = Empty Then
MsgBox "Please select a template from the drop down list"
.Range("F3").Select
Exit Sub
End If
TemplRow = .Range("B3").Value 'Set Template Row
TemplName = .Range("F3").Value 'Set Template Name
DocLoc = Sheet9.Range("F" & TemplRow).Value 'Word Document Filename
'Open Word Template
On Error Resume Next 'If Word is already running
Set Wordapp = GetObject("Word.Application")
If Err.Number <> 0 Then
'Launch a new instance of Word
Err.Clear
'On Error GoTo Error_Handler
Set Wordapp = CreateObject("Word.Application") 'launches word application
Wordapp.Visible = True 'Make the application visible to the user
End If
''''''''''''''''''''''insering tables'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set WordDoc = Wordapp.Documents.Open(FileName:=DocLoc,ReadOnly:=False) 'Open Template
'Copy Range from Excel
Set tbl = ThisWorkbook.Worksheets(Sheet5.Name).ListObjects("tbl").Range
'Copy Excel Table Range
tbl.Copy
'Paste Table into MS Word
WordDoc.Bookmarks("SummaryTable").Range.PasteExcelTable _
LinkedToExcel:=False,_
WordFormatting:=False,_
RTF:=False
'Autofit Table so it fits inside Word Document
Set WordTable = WordDoc.Tables(1)
WordTable.AutoFitBehavior (wdAutoFitWindow)
EndRoutine:
'Optimize Code
Application.ScreenUpdating = True
Application.EnableEvents = True
'Clear The Clipboard
Application.CutCopyMode = False
End With
End Sub

我不确定发生什么恶作剧,因此不胜感激!





推荐阅读
author-avatar
加勒比海盗530
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有