This Code works VB but gives exception in VB.NET, Why?

馋奶兔 提交于 2019-12-11 12:38:07

问题


Aim to achieve : I want to change the SourceData of the Pivot table.

I want to change it from 'C:\[file.xlsx]SheetName'!RangeName to 'SheetName'!RangeName

I have the following implementation :

For Each table In sheet.PivotTables
    Dim str = "'" + Split(table.SourceData, "]")(1)
    table.SourceData = str   // Gives Exception here
Next table

// Exception: Exception from HRESULT: 0x800A03EC

At the time of error str has value 'SheetName'!RangeName (This is fine.. )

I am also getting a message box before the exception : Can not open the source file C:\file.xlsx (Isn't that obvious because its in use)

Please help.. this works in VB as a Macro.


回答1:


This is the VB.Net version as if you would use it in an vb.net-exe. It will not work in EXCEL.VBA, but as I understand it you wanted a VB.Net version?

Imports Microsoft.Office.Interop

Sub ChangePivotTableSourceData()
    Dim e As New Excel.Application
    e.Workbooks.Open("c:\testdoc.xlsx", , True) 'Open the doc readonly
    Dim sheet As Excel.Worksheet = e.ActiveSheet

    For Each table As Excel.PivotTable In sheet.PivotTables
        table.SourceData = "'" + Split(table.SourceData, "]")(1)
    Next

    e.ActiveWorkbook.SaveAs("c:\testdoc_changed.xlsx") 'Save under another name
    e.ActiveWorkbook.Close()
    e.Quit()
    sheet = Nothing
    e = Nothing
End Sub



回答2:


By suggestion of Marc Gravell, I change my comment into an answer:

I can think of two reasons why your code breaks in VB.NET:

  • If in VB you use somewhere in your code On Error Resume Next (oh horror), then you don't see it in VB, but will see it in VB.NET (because it doesn't allow errors to be ignored).

  • You tagged Excel. I assume you went from an older version of Excel to a newer version or you moved your code from Excel to stand-alone VB.NET. A whole host of non VB-related changes might cause an error to popup.

Why you get an error about a printer not being available (or any of the other pointers Jon gave you), I don't know, but you might try that specific question on http://superuser.com.




回答3:


Supposedly, this error is to do with no printer being available. I don't know why you'd get it in VB.NET but not in VB... but you might want to look at your printer permissions etc.

EDIT: This post may be useful as well, showing DCOM service changes with regards to identity.

EDIT: Ooh, looks like the error can mean multiple things. This MSDN page mentions it in a regional settings context...




回答4:


Interop sucks basically. Check your processes (task manager) ... there should be at least one excel.exe process that isn't visible on the desktop. Kill them since they might have the file in use. That causes your 'Can not open the source file C:\file.xlsx' error. And who knows it might also fix your other error.

If it doesn't, check if you installed the latest interop assemblies and use those. It helps when you're dealing with Office products. http://msdn.microsoft.com/en-us/library/15s06t57(v=VS.100).aspx



来源:https://stackoverflow.com/questions/6982138/this-code-works-vb-but-gives-exception-in-vb-net-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!