开发者

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

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 get开发者_如何转开发ting 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.


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.


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...


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜