此代码可以在 VB 中运行,但在 VB.NET 中却出现异常,为什么?

发布于 2024-11-28 14:01:12 字数 576 浏览 1 评论 0原文

目标实现: 我想更改数据透视表的 SourceData。

我想将其从 'C:\[file.xlsx]SheetName'!RangeName 更改为 'SheetName'!RangeName

我有以下实现:

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

发生错误时str 具有值 'SheetName'!RangeName (这很好..)

我还在异常之前收到一个消息框: <代码> 无法打开源文件 C:\file.xlsx (这不是很明显,因为它正在使用中)

请帮忙..这在 VB 中作为宏运行。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

铜锣湾横着走 2024-12-05 14:01:12

根据 Marc Gravell 的建议,我将评论更改为答案:

我可以想到代码在 VB.NET 中崩溃的两个原因:

  • 如果在 VB 中,您在代码中的某个位置使用了 On Error Resume Next(哦,恐怖),然后你在 VB 中看不到它,但会在 VB.NET 中看到它(因为它不允许忽略错误)。

  • 您标记了 Excel。我假设您从旧版本的 Excel 升级到了新版本,或者您将代码从 Excel 移至独立的 VB.NET。大量与 VB 无关的更改可能会导致弹出错误。

为什么你会收到有关打印机不可用的错误(或乔恩给你的任何其他指示),我不知道,但你可以在 http://superuser.com

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.

洒一地阳光 2024-12-05 14:01:12

据推测,此错误与 没有可用的打印机。我不知道为什么你会在 VB.NET 中得到它而不是在 VB 中......但你可能想查看你的打印机权限等。

编辑: 这篇文章也可能有用,显示 DCOM 服务更改关于身份。

编辑:哦,看起来这个错误可能意味着多种情况。 此 MSDN 页面 在区域设置上下文...

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

童话 2024-12-05 14:01:12

这是 VB.Net 版本,就像您在 vb.net-exe 中使用它一样。它在 EXCEL.VBA 中不起作用,但据我了解,您想要 VB.Net 版本?

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

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
天邊彩虹 2024-12-05 14:01:12

互操作基本上很糟糕。检查您的进程(任务管理器)...应该至少有一个在桌面上不可见的 excel.exe 进程。杀死他们,因为他们可能正在使用该文件。这会导致“无法打开源文件 C:\file.xlsx”错误。谁知道它也可能修复您的其他错误。

如果没有,请检查您是否安装了最新的互操作程序集并使用它们。当您处理 Office 产品时,它会很有帮助。
http://msdn.microsoft.com/en-我们/库/15s06t57(v=VS.100).aspx

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文