Reputation: 1670
I have a csv file written from another procedure that executes some VBA
code and I want to write the last modified/saved date to the console in VB.NET
. The following code keeps returning the following error
The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
Where am I going wrong
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)
EDIT: Still having no joy. It seems as if none of the DocumentProperties
have any values. Think this might be an issue with csv files rather than excel workbooks but csv documents have properties too so unsure as to why this wouldn't work with csv files.
'~~> 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
Upvotes: 2
Views: 4216
Reputation: 2558
Old post this is, but I wanted to make the solution available.
It resides here: https://support.microsoft.com/en-us/kb/303296
With this applied, your code should look (in 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");
Upvotes: 1
Reputation: 918
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")
Upvotes: 1
Reputation: 144
Maybe this can help you out. I think you need to refer to the property as an index.
http://msdn.microsoft.com/en-us/library/office/ff197172.aspx
Upvotes: 0