【问题标题】:Having problems copying an Excel range to a Word file using VBA使用 VBA 将 Excel 范围复制到 Word 文件时出现问题
【发布时间】:2021-02-28 09:54:58
【问题描述】:

我查看了一些有关如何从 Excel 复制范围并粘贴到 Word 文档的 VBA 代码,但我无法使用它,它会创建一个 pdf 文件,但文件已损坏。

我有以下 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

希望有人能指导一下。

##编辑尝试:## 我已经尝试过这个,并且将我现有的word文件并将excel表中的范围填充到其中,但是我的水印在“表格”后面,我看不到它(知道我还没有保存为pdf )。 所以atm。就是布局的问题,如果我选择单元格/范围手册,然后将em复制到word中,没关系,我可以看到我的水印。

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  

【问题讨论】:

    标签: excel vba ms-word


    【解决方案1】:

    例如:

    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 的文档名称和路径,请更改:

    Split(stWordDocument, ".doc")(0)
    

    到:

    Split(ThisWorkbook.FullName, ".xls")(0)
    

    【讨论】:

    • 嗨 Macropod 谢谢。它说关于“.PrintOut”有一个错误,如果我点击它,它会询问我是否要将更改保存到 word 文件并且我不想这样做,所以如果我选择“Don 't Save" 然后我没有得到 pdf 文件。
    • @ThomasBøgPetersen - 在上面的答案中,pdf 是由以 .SaveAs2 开头的行创建的。 .PrintOut 行是不必要的,因为您没有使用 Acrobat 打印机来创建 pdf。提示保存文档是因为 .Quit 关闭了 Word。由于该文档此时仍处于打开状态并且有未保存的更改,因此您会收到提示。
    • 嗯,好的。可以看到 pdf 已创建,但 docx 文件内的水印被覆盖或位于 excel 表中添加的内容后面的图层上。如何从水印顶部的excel表中添加内容,就像我打开excel表并手动选择从A1到N24的范围/单元格一样,复制em并将em粘贴到word文档中。这是手动方式,我猜对了i.imgur.com/WmzcBKq.png这是自动方式,它不显示word文档中的水印(错误)i.imgur.com/LVc2khy.png
    • @ThomasBøgPetersen - 您需要 edit your question 才能在评论中包含您链接的图像。您还需要解释水印是如何插入到文档中的。如果您使用 Word 的插入水印选项,它将在标题中,但我怀疑您没有这样做。所有这些都是您问题的基础。除非我们可以先重新创建确切的设置,否则我们无法重现您的问题或正确调试您的代码。
    • 我已经编辑了我的答案以删除 PrintOut - 我认为从 OP 的代码中得到的结果是预期的结果 - 并将其替换为关闭文档的代码。不过,我们仍然需要对所谓的水印进行一些解释。如果它是页眉中的真正水印/形状,通常不会被隐藏。
    猜你喜欢
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多