在相同的上下文中,
https://stackoverflow.com/questions/66407797/having-problems-copying-an-excel-range-to-a-word-file-using-vba
正如我所得到的帮助,我尝试做一个循环,因为我在工作表/书中的几个工作表上有相同的区域,我想通过循环将其复制到现有的word文档(逐张),将其另存为PDF并移动到下一张工作表。
我尝试了以下方法,但得到一个错误:“运行时错误'462‘-远程服务器机器不存在或不可用”。这是针对代码行的:
Set myDoc = WordApp.Documents.Open(Filename:=stWordDocument, AddToRecentFiles:=False, Visible:=False)
代码尝试来自BigBen的“更新代码”:
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 12.0 Object Library)
'SOURCE: www.TheSpreadsheetGuru.com
'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 test.docx"
'Word objects/declared variables.
Dim WordApp As Word.Application
Dim myDoc As Word.Document
Dim Ws As Worksheet
Dim myArr As Variant, a As Variant
Dim rangeArr As Variant
Dim i As Integer
'Optimize Code
Application.ScreenUpdating = False
Application.EnableEvents = False
'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
With WordApp
'Make MS Word Visible and Active
WordApp.Visible = False
'Create a loop
myArr = Array("U7AB1", "U7AB2", "U7BC1")
rangeArr = "A1:N24"
'Set myDoc = WordApp.Documents.Add
'Change: [Set myDoc = WordApp.Documents.Add] to:
Set myDoc = WordApp.Documents.Open(Filename:=stWordDocument, AddToRecentFiles:=False, Visible:=False)
For i = 0 To UBound(myArr)
Set Ws = Sheets(myArr(i))
With Ws
'Copy Excel content to word
ThisWorkbook.Worksheets(myArr(i)).Range(rangeArr).Copy
With Documents(stWordDocument).PageSetup
.LineNumbering.Active = False
.TopMargin = CentimetersToPoints(0)
.BottomMargin = CentimetersToPoints(0)
.LeftMargin = CentimetersToPoints(0)
.RightMargin = CentimetersToPoints(0)
'Paste it to the selected Word template
With myDoc
.Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False
.SaveAs2 Filename:=Split(stWordDocument, ".docx")(0) & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
'.SaveAs2 Filename:=ThisWorkbook.Name & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
.Close False
End With
End With
End With
Next
.Quit
End With
EndRoutine:
'Optimize Code
Application.ScreenUpdating = True
Application.EnableEvents = True
'Clear The Clipboard
Application.CutCopyMode = False
End Sub
有人能给我指路吗?这样我就可以得到一个PDF的per。在每张图纸上,它占据相同的区域,但将其另存为独立的pdf文件,最好以图纸name.pdf命名。从相同的word文件,它不应该保存为现在,但将使用,因为它有一个水印,这应该再次为所有图纸。
1
:使用VBA将Excel范围复制到Word文件时出现问题
发布于 2021-03-01 23:20:57
只是总结一下上面评论中的内容:
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 12.0 Object Library)
'SOURCE: www.TheSpreadsheetGuru.com
'filepath and word template
Const filePath As String = "C:\Users\SDETHBP\Documents\FCM\FCM Ulvetræning Øvelser\U7-U12\"
Const wordTemplate As String = "Word Forside\Forside fra Excel test.dotx"
'Word objects/declared variables.
Dim WordApp As Word.Application
Dim myDoc As Word.Document
Dim Ws As Worksheet
Dim myArr As Variant, a As Variant
Dim rangeArr As Variant
Dim i As Integer
'Optimize Code
Application.ScreenUpdating = False
Application.EnableEvents = False
'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
With WordApp
'Make MS Word Visible and Active
WordApp.Visible = False
'Create a loop
myArr = Array("U7AB1", "U7AB2", "U7BC1")
rangeArr = "A1:N24"
For i = 0 To UBound(myArr)
'Copy Excel content to word
ThisWorkbook.Worksheets(myArr(i)).Range(rangeArr).Copy
Set myDoc = WordApp.Documents.Add(Template:=filePath & wordTemplate, Visible:=False)
'With Documents(stWordDocument).PageSetup
With myDoc
With .PageSetup
.LineNumbering.Active = False
.TopMargin = CentimetersToPoints(0)
.BottomMargin = CentimetersToPoints(0)
.LeftMargin = CentimetersToPoints(0)
.RightMargin = CentimetersToPoints(0)
End With
'Paste it to the selected Word template
.Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False
.SaveAs2 Filename:=filePath & myArr(i) & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
.Close False
End With
Next
.Quit
End With
EndRoutine:
'Optimize Code
Application.ScreenUpdating = True
Application.EnableEvents = True
'Clear The Clipboard
Application.CutCopyMode = False
End Sub
https://stackoverflow.com/questions/66423823
复制相似问题