简体   繁体   中英

Excel to Word to PDF using VBA

Background : With help from StackOverflow, I have successfully found a way to copy specific content (Text, Tables and Charts) from Excel to a Word template with bookmarks using VBA. While saving this, I don't want a.docx format, but instead, want to export it to.pdf. I tried used the ExportAsFixedFormat and ExportAsFixedFormat2 and was able to export it successfully.

Issue : The content on this.pdf file is exported as an image (I guess). I am unable to highlight or copy text from the file. What am I doing wrong and how can I fix this? (FYI, Content Copying is set to 'Allowed' on the pdf)

I am currently using ActiveDocument.ExportAsFixedFormat2 SaveName, wdExportFormatPDF, , wdExportOptimizeForPrint and have tried other variables too.

Any help will be greatly appreciated.

Code:

Option Explicit

Sub ExportFile()

    Dim wrdApp As Word.Application
    Dim WrdDoc As Word.Document
    Dim WrdRng As Word.Range
    Dim WrdShp As Word.InlineShape
    Dim SaveName As String
    
    Dim ChrObj As ChartObject
    
    Set wrdApp = New Word.Application
    'wrdApp.Visible = True
    'wrdApp.Activate
    
    With wrdApp
        
        .Documents.Add Environ("UserProfile") & "\Desktop\Template.dotx"
        
        
        With .Selection
        Range("XEX771").Copy
            .GoTo What:=-1, Name:="Bookmark1"
            .PasteSpecial xlPasteValues
            .GoTo What:=-1, Name:="Bookmark2"
        Range("AG696", Range("AG696").End(xlDown).End(xlToRight)).Copy
        Application.Wait Now() + #12:00:02 AM#
            .PasteExcelTable True, False, False
            .GoTo What:=-1, Name:="Bookmark3"
        Range("F26", Range("F26").End(xlDown).End(xlToRight)).Copy
        Application.Wait Now() + #12:00:02 AM#
            .PasteExcelTable True, False, False
            .GoTo What:=-1, Name:="Bookmark4"
        Range("XEO5").Copy
            .PasteSpecial xlPasteValues
            .GoTo What:=-1, Name:="Bookmark5"
        Range("K26", Range("K26").End(xlDown).End(xlToRight)).Copy
        Application.Wait Now() + #12:00:02 AM#
            .PasteExcelTable True, False, False
        End With
    
    Set ChrObj = ActiveSheet.ChartObjects(1)
        ChrObj.Chart.ChartArea.Copy
        
        Application.Wait Now() + #12:00:02 AM#
        
    .Selection.GoTo What:=-1, Name:="Bookmark6"
    .Selection.PasteSpecial DataType:=wdPasteMetafilePicture, Placement:=wdInLine
    
    Set ChrObj = ActiveSheet.ChartObjects(2)
        ChrObj.Chart.ChartArea.Copy
        
        Application.Wait Now() + #12:00:02 AM#
        
    .Selection.GoTo What:=-1, Name:="Bookmark7"
    .Selection.PasteSpecial DataType:=wdPasteMetafilePicture, Placement:=wdInLine

    Set ChrObj = ActiveSheet.ChartObjects(3)
        ChrObj.Chart.ChartArea.Copy
        
        Application.Wait Now() + #12:00:02 AM#
        
    .Selection.GoTo What:=-1, Name:="Bookmark8"
    .Selection.PasteSpecial DataType:=wdPasteMetafilePicture, Placement:=wdInLine
   
SaveName = Environ("UserProfile") & "\Desktop\FileName.pdf"

    .ActiveDocument.ExportAsFixedFormat2 SaveName, wdExportFormatPDF, , wdExportOptimizeForPrint

    End With

wrdApp.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
wrdApp.Quit

Set wrdApp = Nothing

End Sub

Using Selection is very inefficient - which may also help explain why you've inserted so many delays in your code. You also have numerous unnecessary.Goto and copy/paste operations. Try:

Sub ExportFile()
Dim wrdApp As New Word.Application, WrdDoc As Word.Document
Dim WrdRng As Word.Range, WrdShp As Word.InlineShape
Dim xlSheet As Excel.Worksheet: Set xlSheet = ActiveSheet
With wrdApp
  .Visible = False
  Set WrdDoc = .Documents.Add(Environ("UserProfile") & "\Desktop\Template.dotx")
  With WrdDoc
    .Bookmarks("Bookmark1").Range.Text = xlSheet.Range("XEX771").Text
    xlSheet.Range("AG696", Range("AG696").End(xlDown).End(xlToRight)).Copy
    .Bookmarks("Bookmark2").Range.PasteExcelTable True, False, False
    xlSheet.Range("F26", Range("F26").End(xlDown).End(xlToRight)).Copy
    .Bookmarks("Bookmark3").Range.PasteExcelTable True, False, False
    .Bookmarks("Bookmark4").Range.Text = xlSheet.Range("XEO5").Text
    xlSheet.Range("K26", Range("K26").End(xlDown).End(xlToRight)).Copy
    .Bookmarks("Bookmark5").Range.PasteExcelTable True, False, False
    xlSheet.ChartObjects(1).Chart.ChartArea.Copy
    .Bookmarks("Bookmark6").Range.PasteSpecial DataType:=wdPasteMetafilePicture, Placement:=wdInLine
    xlSheet.ChartObjects(2).Chart.ChartArea.Copy
    .Bookmarks("Bookmark7").Range.PasteSpecial DataType:=wdPasteMetafilePicture, Placement:=wdInLine
    xlSheet.ChartObjects(3).Chart.ChartArea.Copy
    .Bookmarks("Bookmark8").Range.PasteSpecial DataType:=wdPasteMetafilePicture, Placement:=wdInLine
    .SaveAs FileName:=Environ("UserProfile") & "\Desktop\FileName.pdf", _
      FileFormat:=wdFormatPDF, AddToRecentFiles:=False
    .Close False
  End With
  .Quit
End With
Set WrdDoc = Nothing: Set wrdApp = Nothing: Set xlSheet = Nothing
End Sub

This was a problem with the "Bitmap text when fonts may not be embedded" option while saving PDF files via MS Word. I referred to this page and added BitmapMissingFonts:=False. Resolved the issue.

.ActiveDocument.ExportAsFixedFormat2 SaveName, wdExportFormatPDF, BitmapMissingFonts:=False

Thanks, everyone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM