VB.net Excel ExportAsFixedFormat失败从HRESULT的异常:0x80010105(RPC

编程入门 行业动态 更新时间:2024-10-25 20:22:22
本文介绍了VB Excel ExportAsFixedFormat失败从HRESULT的异常:0x80010105(RPC_E_SERVERFAULT))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试编写一个非常简单的VB应用程序,它将打开一个excel文件并将其保存为excel。

我正在处理的环境是如下:

  • Windows 10
  • Visual Studio 2016
  • Office 2016

我已成功设法打开Excel表,并保存到其他位置。但是,当尝试打开excel并保存为pdf时,我收到以下错误消息:

服务器抛出异常。 (HRESULT的异常:0x80010105(RPC_E_SERVERFAULT))

查看错误的详细信息,我可以看到以下内容:

System.Runtime.InteropServices.COMException {服务器抛出异常(来自HRESULT的异常:0x80010105(RPC_E_SERVERFAULT))}

错误代码:-2147417851

我正在使用的代码如下:

Dim xlApp As New Excel。应用程序 Dim xlWorkBook作为Excel.Workbook Dim xlWorkSheet作为Excel.Worksheet xlApp.Visible = False xlApp.AlertBeforeOverwriting = False Dim sheetname As String =d:\test\test.xlsx xlWorkBook = xlApp.Workbooks.Open(sheetname) xlWorkBook.Activate() xlWorkSheet = xlWorkBook.Sheets(Sheet1) xlWorkSheet.Activate() xlApp.DisplayAlerts = False xlWorkSheet.ExportAsFixedFormat(Type:= Excel.XlFixedFormatType.xlTypePDF,Filename:= $​​ b $ bd:\test\test.pdf,质量:= Excel.XlFixedFormatQuality.xlQualityStandard _ ,IncludeDocProperties:= True,IgnorePrintAreas:= True,OpenAfterPublish:= False) xlWorkBook .Close(SaveChanges:= False) xlApp.Quit() System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp):xlApp = Nothing System.Runtime.InteropServices.Marshal.ReleaseComObject (xlWorkBook):xlWorkBook = Nothing System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet):xlWorkSheet = Nothing

帮助!

由于Excel表格具有公式,将内容复制并粘贴到新工作表中(粘贴特殊值和数字格式)和运行上面的代码

解决方案

对!感谢所有试图帮助的人。通过阅读您的回复,我一直在努力,直到我想出了这个问题。

看来,ExportAsFixedFormat不喜欢带有公式的Excel表格。为了解决这个问题,我刚刚创建了一个新的空白工作表,并将内容从我的主页复制并粘贴(仅限于值)。这似乎工作完美。然后我在我的代码中自动化如下:

Dim xlworksheet_static As Excel.Worksheet = xlWorkBook.Worksheets(2) xlWorkSheet.Range(A1,H35)。Copy() xlworksheet_static.Activate() xlworksheet_static.Range(A1,H35)。 xlworksheet_static.PasteSpecial(Excel.XlPasteType.xlPasteValuesAndNumberFormats) xlworksheet_static.ExportAsFixedFormat(Type:= Excel.XlFixedFormatType.xlTypePDF,Filename:= $​​ b $ bd:\test\test.pdf ,质量:= Excel.XlFixedFormatQuality.xlQualityStandard _ ,IncludeDocProperties:= True,IgnorePrintAreas:= True,OpenAfterPublish:= False)

可能不是最直接的方式,但我无法让它工作,否则!

I am trying to write a very simple VB app which would open an excel file and save it as excel.

The environment I am working on is as follows:

  • Windows 10
  • Visual Studio 2016
  • Office 2016

I have successfully managed to open the excel sheet and save to another location. However when trying to open the excel and save as pdf, I get the following error message

The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))

Looking into the details of the error, I can see the following:

System.Runtime.InteropServices.COMException {"The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))"}

error code: -2147417851

The code I am using to do this is as follows:

Dim xlApp As New Excel.Application Dim xlWorkBook As Excel.Workbook Dim xlWorkSheet As Excel.Worksheet xlApp.Visible = False xlApp.AlertBeforeOverwriting = False Dim sheetname As String = "d:\test\test.xlsx" xlWorkBook = xlApp.Workbooks.Open(sheetname) xlWorkBook.Activate() xlWorkSheet = xlWorkBook.Sheets("Sheet1") xlWorkSheet.Activate() xlApp.DisplayAlerts = False xlWorkSheet.ExportAsFixedFormat(Type:=Excel.XlFixedFormatType.xlTypePDF, Filename:= "d:\test\test.pdf", Quality:=Excel.XlFixedFormatQuality.xlQualityStandard _ , IncludeDocProperties:=True, IgnorePrintAreas:=True, OpenAfterPublish:= False) xlWorkBook.Close(SaveChanges:=False) xlApp.Quit() System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp) : xlApp = Nothing System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook) : xlWorkBook = Nothing System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet) : xlWorkSheet = Nothing

Help!

issue seem to arise due to the Excel sheet having formulas, copying and pasting the content into a new sheet (paste special values and number formatting only) and running the code above works

解决方案

Right! Thank you to all that tried to help. By reading your responses I kept trying till I figured out the issue.

It appears that ExportAsFixedFormat doesn't like Excel sheets with formulas in. To work around this issue, I just created a new blank worksheet and copied and pasted (values only) the content from my main sheet into it. This seems to work perfectly. I then automated this in my code as follows:

Dim xlworksheet_static As Excel.Worksheet = xlWorkBook.Worksheets(2) xlWorkSheet.Range("A1", "H35").Copy() xlworksheet_static.Activate() xlworksheet_static.Range("A1", "H35").Select() xlworksheet_static.PasteSpecial(Excel.XlPasteType.xlPasteValuesAndNumberFormats) xlworksheet_static.ExportAsFixedFormat(Type:=Excel.XlFixedFormatType.xlTypePDF, Filename:= "d:\test\test.pdf", Quality:=Excel.XlFixedFormatQuality.xlQualityStandard _ , IncludeDocProperties:=True, IgnorePrintAreas:=True, OpenAfterPublish:= False)

Probably not the most straight forward way but I couldn't get it to work otherwise!

更多推荐

VB.net Excel ExportAsFixedFormat失败从HRESULT的异常:0x80010105(RPC

本文发布于:2023-11-06 01:40:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562399.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   Excel   VB   net   ExportAsFixedFormat

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!