使用VBA将Excel范围复制到Word文件时遇到问题 [英] Having problems copying an Excel range to a Word file using VBA
问题描述
我查看了一些VBA代码,了解如何从Excel复制范围并将其粘贴到Word文档,但是我无法使其正常工作,它创建了pdf文件,但是该文件已损坏.
I have looked at some VBA codes on how to copy a range from Excel and paste to a Word Document, but I can't get it to work, it creates a pdf file, but the file is corrupted.
我有以下VBA代码:
Sub CopyToWordAndPrintPDF()
'PURPOSE: Copy/Paste An Excel Table Into a New Word Document
'NOTE: Must have Word Object Library Active in Order to Run _
(VBE > Tools > References > Microsoft Word 1x.0 Object Library)
'Name of the existing Word document
Const stWordDocument As String = "C:\Users\SDETHBP\Documents\FCM\FCM Ulvetræning Øvelser\U7-U12\Word Forside\Forside fra Excel.docx"
'Word objects/declared variables.
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
'Excel objects
Dim wbBook As Workbook
Dim wsSheet As Worksheet
'Instantiate Word and open the "Table Reports" document.
Set wdApp = New Word.Application
'Making word App Visible
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Add(stWordDocument)
' copy content to word
ThisWorkbook.Worksheets("U7AB1").Range("A1:N24").Copy
' Pastes it to the selected Word doc template.
wdApp.Documents.Add
wdApp.Selection.Paste
' Sets your printer in Word to Adobe PDF and then prints the whole doc.
wdApp.WordBasic.FilePrintSetup Printer:="Adobe PDF", DoNotSetAsSysDefault:=1
wdApp.ActiveDocument.PrintOut
'Cleaning
Set wsCell = Nothing
Application.StatusBar = "Cleaning up..."
Set wdDoc = Nothing
wdApp.Visible = True
Set wdApp = Nothing
Application.StatusBar = False
End Sub
我希望有人可以引导我.
I hope someone can guide me.
## EDIT尝试:##我已经尝试过了,然后将我现有的word文件填充到excel表格的范围内,但是我的水印随后位于"table"表的后面.而且我看不到它(知道我还没有保存为pdf).所以atm.问题是布局,如果我选择单元格/范围手册,然后将em复制到单词上,那很好,我可以看到我的水印.
##EDIT Try:## I have tried this and that take my existing word file and fill the range from the excel sheet into it, but my watermark is then behind the "table" and I can't see it (know that I don't save as pdf yet). So atm. it's the layout that's the problem, if I select the cells/range manual, and then copy em to the word, it's fine, I can see my watermark.
Sub ExcelRangeToWord()
'PURPOSE: Copy/Paste An Excel Table Into a New Word Document
'NOTE: Must have Word Object Library Active in Order to Run _
(VBE > Tools > References > Microsoft Word 12.0 Object Library)
'SOURCE: www.TheSpreadsheetGuru.com
Dim tbl As Excel.Range
Dim WordApp As Word.Application
Dim myDoc As Word.Document
Dim WordTable As Word.Table
'Optimize Code
Application.ScreenUpdating = False
Application.EnableEvents = False
'Copy Range from Excel
Set tbl = ThisWorkbook.Worksheets("U7AB1").Range("A1:N24")
'Create an Instance of MS Word
On Error Resume Next
'Is MS Word already opened?
Set WordApp = GetObject(class:="Word.Application")
'Clear the error between errors
Err.Clear
'If MS Word is not already open then open MS Word
If WordApp Is Nothing Then Set WordApp = CreateObject(class:="Word.Application")
'Handle if the Word Application is not found
If Err.Number = 429 Then
MsgBox "Microsoft Word could not be found, aborting."
GoTo EndRoutine
End If
On Error GoTo 0
'Make MS Word Visible and Active
WordApp.Visible = True
WordApp.Activate
'Create a New Document
'Set myDoc = WordApp.Documents.Add
'Change: [Set myDoc = WordApp.Documents.Add] to:
Set myDoc = WordApp.Documents.Open("C:\Users\SDETHBP\Documents\FCM\FCM Ulvetræning Øvelser\U7-U12\Word Forside\Forside fra Excel.docx")
'Copy Excel Table Range
tbl.Copy
'Paste Table into MS Word
myDoc.Paragraphs(1).Range.PasteExcelTable _
LinkedToExcel:=False, _
WordFormatting:=False, _
RTF:=False
EndRoutine:
'Optimize Code
Application.ScreenUpdating = True
Application.EnableEvents = True
'Clear The Clipboard
Application.CutCopyMode = False
End Sub
推荐答案
例如:
Sub CopyToWordAndPrintPDF()
'PURPOSE: Copy/Paste An Excel Table Into a New Word Document
'NOTE: Must have Word Object Library Active in Order to Run _
(VBE > Tools > References > Microsoft Word #.0 Object Library)
'Name of the existing Word document
Const stWordDocument As String = "C:\Users\SDETHBP\Documents\FCM\FCM Ulvetræning Øvelser\U7-U12\Word Forside\Forside fra Excel.docx"
'Word objects/declared variables.
Dim wdApp As New Word.Application, wdDoc As Word.Document
With wdApp
.Visible = False
' Open the Word document
Set wdDoc = .Documents.Open(Filename:=stWordDocument, AddToRecentFiles:=False, Visible:=False)
' copy content to word
ThisWorkbook.Worksheets("U7AB1").Range("A1:N24").Copy
' Pastes it to the selected Word template.
With wdDoc
.Range.Characters.Last.Paste ' or, for example: .PasteExcelTable False, False, True
' Saves then prints the doc.
.SaveAs2 Filename:=Split(stWordDocument, ".doc")(0) & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
.Close False
End With
.Quit
End With
Set wdDoc = Nothing: Set wdApp = Nothing
End Sub
使用工作簿的名称&路径而不是文档名称&PDF的路径,更改:
To use the workbook's name & path instead of the document's name & path for the PDF, change:
Split(stWordDocument, ".doc")(0)
收件人:
Split(ThisWorkbook.FullName, ".xls")(0)
这篇关于使用VBA将Excel范围复制到Word文件时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!