我有一个csv文件是从另一个执行某些VBA
代码的过程编写的,我想在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)
编辑: 仍然没有快乐。好像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
答案 0 :(得分:1)
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")
答案 1 :(得分:1)
旧帖这是,但我想提供解决方案。
它位于此处: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");
答案 2 :(得分:0)
也许这可以帮到你。我认为您需要将该属性称为索引。
http://msdn.microsoft.com/en-us/library/office/ff197172.aspx