访问文档属性 – VB中的Excel工作簿/ CSV

我有一个csv文件从其他程序写入,执行一些VBA代码,我想写最后修改/保存date到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 

尝试从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") 

旧post是这样的,但我想提供解决scheme。

它驻留在这里: 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"); 

也许这可以帮助你。 我认为你需要把这个财产作为一个指标。

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