【问题标题】:Accessing document properties - Excel Workbook/CSV in VB访问文档属性 - VB 中的 Excel 工作簿/CSV
【发布时间】:2014-01-01 05:32:28
【问题描述】:

我有一个从另一个执行一些VBA 代码的过程写入的 csv 文件,我想在VB.NET 中将最后修改/保存的日期写入控制台。以下代码不断返回以下错误

The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

我哪里错了

 VB
 Dim xlApp As New Microsoft.Office.Interop.Excel.Application
 Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
 xlWorkBook = xlApp.Workbooks.Open("C:\Book3.csv")
 Dim DocProps As Object = xlWorkBook.BuiltinDocumentProperties
 MsgBox(DocProps("Last Save Time").value)

 C#
 Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application()
 Microsoft.Office.Interop.Excel.Workbook xlWorkBook = default(Microsoft.Office.Interop.Excel.Workbook)
 xlWorkBook = xlApp.Workbooks.Open("C:\\Book3.csv")
 object DocProps = xlWorkBook.BuiltinDocumentProperties
 Interaction.MsgBox(DocProps("Last Save Time").value)

http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.builtindocumentproperties.aspx

编辑: 仍然没有快乐。似乎DocumentProperties 都没有任何值。认为这可能是 csv 文件而不是 excel 工作簿的问题,但 csv 文档的属性太不确定为什么这不适用于 csv 文件。

        '~~> Define your Excel Objects
        Dim xlApp As New Microsoft.Office.Interop.Excel.Application
        Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
        Dim DocProps As Object, DProps As Object

        xlWorkBook = xlApp.Workbooks.Open("C:\Book3.csv")

        DocProps = xlWorkBook.BuiltinDocumentProperties

        '~~> Display Excel
        xlApp.Visible = False

        '~~> Loop via all properties
        If Not (DocProps Is Nothing) Then
            Dim i As Integer
            For i = 1 To DocProps.Count - 1
                Try
                    DProps = DocProps(i)
                    Console.WriteLine("{0} -> {1}", DProps.Name, DProps.value)
                Catch
                End Try
            Next i
        End If

        '~~> Save and Close the File
        xlWorkBook.Close(True)

        '~~> Quit the Excel Application
        xlApp.Quit()

        '~~> Clean Up
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
            xlApp = Nothing
        Catch ex As Exception
            xlApp = Nothing
        Finally
            GC.Collect()
        End Try
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook)
            xlWorkBook = Nothing
        Catch ex As Exception
            xlWorkBook = Nothing
        Finally
            GC.Collect()
        End Try

【问题讨论】:

  • 我认为您实际上并没有使用VB6?可能应该删除该标签。

标签: c# vb.net excel visual-studio csv


【解决方案1】:

也许这可以帮助你。我认为您需要将该属性称为索引。

http://msdn.microsoft.com/en-us/library/office/ff197172.aspx

【讨论】:

  • 没问题,抱歉我帮不上什么忙。希望你能解决:)
【解决方案2】:

Try giving this a shot from MSDN

Dim properties As Microsoft.Office.Core.DocumentProperties

properties = DirectCast(Globals.ThisWorkbook.BuiltinDocumentProperties, _
                        Microsoft.Office.Core.DocumentProperties)

Dim prop As Microsoft.Office.Core.DocumentProperty
prop = properties.Item("Last Save Time")

【讨论】:

  • 我认为朝着正确的方向前进,但尚未解决。它似乎不会为我投射 Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Core.DocumentProperties'. msdn.microsoft.com/en-us/library/vstudio/…
  • 是的,我也明白了。我不确定我还能做什么,但这里有一个 c# 解决方案的链接。 stackoverflow.com/q/1137763/128221
  • 注意:不会找到上次保存时间 - 它区分大小写并且是“上次保存时间”
【解决方案3】:

这是旧帖子,但我想提供解决方案。

它驻留在此处:https://support.microsoft.com/en-us/kb/303296

应用此功能后,您的代码应该看起来像(在 C# 中):

Microsoft.Office.Interop.Excel.Application xlApp = 
    new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook xlWorkBook = 
    default(Microsoft.Office.Interop.Excel.Workbook);
xlWorkBook = xlApp.Workbooks.Open("C:\\Book3.csv");
object DocProps = xlWorkBook.BuiltinDocumentProperties;

string strIndex = "Last Save Time";
string strValue;
object oDocSaveProp = typeDocBuiltInProps.InvokeMember("Item", 
                           BindingFlags.Default | 
                           BindingFlags.GetProperty, 
                           null,oDocBuiltInProps, 
                           new object[] {strIndex} );
Type typeDocSaveProp = oDocSaveProp.GetType();
strValue = typeDocSaveProp.InvokeMember("Value", 
                           BindingFlags.Default |
                           BindingFlags.GetProperty,
                           null,oDocSaveProp,
                           new object[] {} ).ToString();
MessageBox.Show(strValue, "Last Save Time");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多