2017-03-31 178 views
0

目標是在Access應用程序中運行此子例程,該應用程序將PDF文檔嵌入到新的Excel工作簿中。該子工作正常與文本文件,但會給出錯誤「運行時錯誤'1004':文檔未保存。」與一個PDF文件。代碼在SaveAs方法中斷。爲什麼一個文件類型嵌入,並且PDF文件類型不起作用?任何幫助將不勝感激。將PDF嵌入到電子表格中

Sub SavePDFInWorkBook() 
    Dim xlObj As New Excel.Application ' Excel application 
    Dim wbkObj As Excel.workbook  ' workbook object 
    Dim wstObj As Excel.Worksheet  ' worksheet object 
    Dim oleObj As Excel.OLEObject  ' embeded object 
    Dim EmbedFile, MyWbk As String 

    MyWbk = "M:\SS\DD\Projects\cash_dep2000\TEST.xlsx" 
    EmbedFile = "M:\SS\DD\Projects\cash_dep2000\DaytonFreight.pdf" 

    Set wbkObj = xlObj.Workbooks.Add ' workbook is added in open condition in memory 
    Set wstObj = wbkObj.Worksheets(1) ' workbook is created with one worksheet 
    Set oleObj = wstObj.OLEObjects.Add(, EmbedFile, False, True) 

    wbkObj.SaveAs (MyWbk)    ' saves workbook to folder 
    wbkObj.Close      ' closes workbook in folder 
    Set oleObj = Nothing 
    Set wstObj = Nothing 
    Set wbkObj = Nothing 
    Set xlObj = Nothing 

    MsgBox ("Test Complete") 
End Sub 
+0

經過測試的代碼。嵌入pdf時也沒有獲取文件來保存。圖像實際上並未添加到工作表中,只是一個帶有圖像名稱的框,除了pdf類型,只是一個很大的空框和沒有文件名。這些是實際文件的鏈接。這是你想要的嗎? PDF鏈接的作品,只是不會保存文件。 – June7

回答

0

這個工程嵌入pdf圖標/鏈接,但它也打開pdf。加重。
ActiveSheet.OLEObjects.Add(Filename:="C:\Users\June\MyStuff\DMV.pdf", Link:=False, _ DisplayAsIcon:=True, IconFileName:= _ "C:\WINDOWS\Installer\{AC76BA86-7AD7-1033-7B44-AB0000000001}\PDFFile_8.ico", _ IconIndex:=0, IconLabel:="whatever you want").Activate

我可以手動保存文件,但保存代碼爲宏啓用工作簿錯誤。更加惡化。也許修改爲您的對象操作代碼將工作。