[英]Set Word Document Range to Excel Range
I have a larger project which helps generate letters in Excel based on multiple reports (on other sheets) and feeds each letter into a common Word document with page breaks inserted in between each letter. 我有一个较大的项目,该项目可以帮助您基于多个报告(在其他工作表上)在Excel中生成字母,并将每个字母输入到共同的Word文档中,并在每个字母之间插入分页符。 I have attempted to resolve an issue where an error was being randomly thrown stating that the clipboard was not valid.
我试图解决一个错误,该错误被随机抛出,指出剪贴板无效。 The following code produces this error on occasion:
以下代码有时会产生此错误:
Error-prone code: 容易出错的代码:
Sub ExportToWordDoc(ws As Worksheet, wordDoc As Word.Document, classCount As Long)
Application.CutCopyMode = False
ws.Range("A1:J" & classCount + 8).Copy
DoEvents 'added in attempt to resolve random error
Application.Wait (Now + TimeValue("0:00:01")) 'also added in attempt to resolve error
wordDoc.Range(wordDoc.Content.End - 1).Paste 'line causes intermittent error
wordDoc.Range(wordDoc.Content.End - 1).InsertBreak Type:=7
End Sub
I believe that the ultimate solution will be to avoid using the clipboard for migrating the data over. 我相信最终的解决方案将是避免使用剪贴板来迁移数据。 Is there a way to do the following?
有没有办法做到以下几点? Currently, the code below produces a type mismatch error.
当前,下面的代码产生类型不匹配错误。
Sub ExportToWordDoc(ws As Worksheet, wordDoc As Word.Document, classCount As Long)
wordDoc.Range(wordDoc.Content.End - 1).Text = ws.Range("A1:J" & classCount + 8).value
wordDoc.Range(wordDoc.Content.End - 1).InsertBreak Type:=7
End Sub
Any help would be much appreciated. 任何帮助将非常感激。
FYI: The number of letters generated can be between 10 and 100. 仅供参考:生成的字母数可以在10到100之间。
Perhaps you can find a better way in this code. 也许您可以在此代码中找到更好的方法。 This example takes the range A1:A10 on Sheet 1 and exports it to the first table in an existing Word document named "Table Report".
本示例采用工作表1上的范围A1:A10,并将其导出到现有Word文档“表报告”中的第一个表中。 Note : It doesn't use copy.
注意 :它不使用复制。
Sub Export_Table_Data_Word()
'Name of the existing Word document
Const stWordDocument As String = "Table Report.docx"
'Word objects.
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wdCell As Word.Cell
'Excel objects
Dim wbBook As Workbook
Dim wsSheet As Worksheet
'Count used in a FOR loop to fill the Word table.
Dim lnCountItems As Long
'Variant to hold the data to be exported.
Dim vaData As Variant
'Initialize the Excel objects
Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets("Sheet1")
vaData = wsSheet.Range("A1:A10").Value
'Instantiate Word and open the "Table Reports" document.
Set wdApp = New Word.Application
Set wdDoc = wdApp.Documents.Open(wbBook.Path &; "\" &; stWordDocument)
lnCountItems = 1
'Place the data from the variant into the table in the Word doc.
For Each wdCell In wdDoc.Tables(1).Columns(1).Cells
wdCell.Range.Text = vaData(lnCountItems, 1)
lnCountItems = lnCountItems + 1
Next wdCell
'Save and close the Word doc.
With wdDoc
.Save
.Close
End With
wdApp.Quit
'Null out the variables.
Set wdCell = Nothing
Set wdDoc = Nothing
Set wdApp = Nothing
MsgBox "The " &; stWordDocument &; "'s table has succcessfully " &; vbNewLine &; _
"been updated!", vbInformation
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.