ExportAsFixedFormat为PDF System.Runtime.InteropServices.COMException:

时间:2019-12-05 21:02:51

标签: excel vb.net export-to-pdf

我正在尝试将数据从Visual Studio vb.net中的应用程序发送到模板.xls,然后将其另存为.pdf。 一切正常,除了尝试导出为pdf并保存文件时,出现以下异常;

System.Runtime.InteropServices.COMException: 'Document not saved. The document may be open, or an error may have been encountered when saving.'

我正在使用以下代码:

    Private Sub exportXlsData()

        'Gets resource path of template
        Dim exeDir As New IO.FileInfo(Reflection.Assembly.GetExecutingAssembly.FullName)
        Dim xlPath = IO.Path.Combine(exeDir.DirectoryName, "Template.xlsx")

        'dims excel
        Dim xls As Microsoft.Office.Interop.Excel.Application
        Dim xlsWorkBook As Microsoft.Office.Interop.Excel.Workbook
        Dim xlsWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value

        'opens sheet
        xls = New Microsoft.Office.Interop.Excel.Application
        xlsWorkBook = xls.Workbooks.Open(xlPath)
        xlsWorkSheet = xlsWorkBook.Sheets("sheet1")
        xls.Visible = False

        'export data to excel
        xlsWorkSheet.Cells(1, 1) = "TEST WORKS YEWWW"

        'save pdf
        xlsWorkSheet.ExportAsFixedFormat(0, "c:\test.pdf")

        xlsWorkBook.Close()
        xls.Quit()

    End Sub

我知道问题出在

xlsWorkSheet.ExportAsFixedFormat(0, "c:\test.pdf")

并尝试了一些不同的情况,例如

xlsWorkSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= -

但是Visual Studio无法识别xlTypePDF。

我有Office 365,并已添加参考。

任何帮助将不胜感激。

0 个答案:

没有答案