2

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)

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

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.

console output

        '~~> 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
TylerDurden
  • 1,632
  • 1
  • 20
  • 30

3 Answers3

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")
Jeff
  • 908
  • 2
  • 9
  • 23
  • Heading down the right track I think but didnt solve it yet. It wont seem to cast for me `Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Core.DocumentProperties'.` http://msdn.microsoft.com/en-us/library/vstudio/dhxe2d75(v=vs.100).aspx – TylerDurden Dec 13 '13 at 15:23
  • Yeah, I'm getting that too. I'm not sure what else I can do, but here is a link to a c# solution. http://stackoverflow.com/q/1137763/128221 – Jeff Dec 13 '13 at 16:11
  • 1
    Note: Last Save Time won't be found - its case sensitive and is "Last save time" – namford Nov 12 '14 at 11:50
1

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");
ib11
  • 2,530
  • 3
  • 22
  • 55
0

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

Community
  • 1
  • 1
Callum Kerr
  • 144
  • 1
  • 11