I have couple of ".xls" files on a folder and need to convert it into tab seperated values
Found a vb script for this ..please some body sugest how to do this?
I am getting couple of error when running this.I am not a vb programmer.Experts...please help
Public Sub Main()
Dim WScript As Object = Nothing '' with out nothing it was showing an error
Dim oExcel As Object
Dim oBook As Object
If WScript.Arguments.Count < 2 Then
WScript.Echo("Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv")
Wscript.Quit()
End If
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Open(WScript.Arguments.Item(0)) ''item o might be excel
oBook = oExcel.Workbooks.Open("C:\Users\5A5.xls")
oBook.SaveAs(WScript.Arguments.Item(1), -4158)
oBook.Close(False)
oExcel.Quit()
WScript.Echo("Done")
End Sub
The exception:
System.Reflection.TargetInvocationException:
Exception has been thrown by the target of an invocation.
---> System.NullReferenceException: Object variable or With block variable not set.
Your problem is you are trying to use WScript but it has not been initialized (it's set to Nothing).
Try it without it:
Dim oExcel As Object
Dim oBook As Object
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Open("C:\Users\5A5.xls")
oBook.SaveAs("C:\Users\5A5.txt", -4158)
oBook.Close(False)
oExcel.Quit()
Related
To create an instance of excel using VB.Net, we can write the following piece of code:
Imports System
Imports Microsoft.Office.Interop.Excel
Imports Word = Microsoft.Office.Interop.Word
Module Program
'declaration statements
Dim oXL As Microsoft.Office.Interop.Excel.Application
Dim oWB As Workbook
Dim oSheet As Worksheet
Dim oWord As Word.Application
Dim oDoc As Word.Document
Sub Main(args As String())
'Console.WriteLine("Hello World!")
oXL = CreateObject("Excel.Application")
oXL.Visible = True
oXL.DisplayAlerts = True
Console.WriteLine("Type returned by CreateObject: {0}", oXL.GetType())
oWB = oXL.Workbooks.Add
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Add
Console.ReadLine()
oXL.Quit() 'Not getting the warning
oWord.Quit(False) 'Not getting the warning
End Sub
End Module
Till here all is good. However I also came across the following statement in the MSDN Link:
Use the Application property to return the Application object.
I checked and found that the APPLICATION property is a member of _Application interface, the usage criteria of which as per this link is mentioned below.
Use this primary interface only when the method you want to use shares the same name as an event of the COM object
However this statement is not clear to me. So can someone please explain (if possible with an example) when should I use the Application property of the _Application interface to return an Application object? Also is the object returned by the Application property any different from the one being returned by createObject in the statement oXL = CreateObject("Excel.Application")?
I'm hoping someone is able to point me in the correct direction on this. I've spent hours trying to get my code to work without success.
I wrote a program in Visual Studio using VB.NET that would open a directory, iterate through all the files, and rename each/copy them to to a new folder. I'm now trying to get the program to read a particular attribute of each file and include that in the file name. This will either be the Media Created or Date Created attribute. It appears I can do this using the Folder.GetDetailsOf method.
The example code given by Microsoft for VB is as shown below:
Private Sub btnGetDetailsOf_Click()
Dim objShell As Shell
Dim objFolder As Folder
Set objShell = New Shell
Set objFolder = objShell.NameSpace("C:\WINDOWS")
If (Not objFolder Is Nothing) Then
Dim objFolderItem As FolderItem
Set objFolderItem = objFolder.ParseName("clock.avi")
If (Not objFolderItem Is Nothing) Then
Dim szItem As String
szItem = objFolder.GetDetailsOf(objFolderItem, 2)
End If
Set objFolderItem = Nothing
End If
Set objFolder = Nothing
Set objShell = Nothing
End Sub
However, when trying to compile I get lots of errors, including Shell, Folder, and FolderItem not being defined.
After lots of searching and reading articles I've got to this point with my code, but it throws an error when trying to set objFolderItem.
Dim di As New DirectoryInfo(c:\folder)
Dim fileArray As FileInfo() = di.GetFiles()
Dim file As FileInfo
Dim objShell As Object
Dim objFolder As Object
Dim objFolderItem As Object
objShell = CreateObject("Shell.Application")
objFolder = objShell.NameSpace(c:\folder) 'create folder object
For Each file In fileArray
objFolderItem = objFolder.ParseName(file.Name) 'create file object
MsgBox(objFolder.getdetailsof(objFolderItem, 201))
Next file
When I check the running code it appears objShell.NameSpace doesn't return anything.
Can someone advise how I can create objShell correcting in VB.NET or provide an updated version of Microsoft's example code so I can use that as a basis?
Thanks
Thanks for your help. I managed to get this working by importing Microsoft Shell Controls And Automation and using the code below:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim objShell As Object
Dim objFolder As Object
objShell = New Shell32.Shell
objFolder = objShell.NameSpace("D:\FOLDERNAME")
If (Not objFolder Is Nothing) Then
Dim objFolderItem As Object
objFolderItem = objFolder.ParseName("FILENAME")
MsgBox(objFolderItem.name & " " & objFolder.getdetailsof(objFolderItem, 208))
objFolderItem = Nothing
End If
objFolder = Nothing
objShell = Nothing
End Sub
Many thanks for your help again
That's a lot of code for a simple problem. You don't need to use the shell. To copy/move:
My.Computer.FileSystem.CopyDirectory(<SourceDir>, <DestinationDir>)
To rename a file:
My.Computer.FileSystem.RenameFile("CurrentName", "DesiredName")
To get a file's attributes:
attributes = File.GetAttributes(path)
I am trying to make a visio diagram that you click a button and it searchs and excel file for the "location" once it find the location in the excel file it then copies over a URL and proceeds to open that URL with the default browser. I keep getting runtime error 438: Object doesn't support this property or method. Any ideas?
Option Compare Text
Private Sub Mail_Room_Click()
Dim XLApp As Excel.Application
Dim XLWB As Excel.Workbook
Set XLApp = New Excel.Application
Set XLWB = XLApp.Workbook.Open("C:\printers\schprint.xlsx")
Set XLWsht = XLWB.Sheets(1)
Dim URL As String
Dim Location As String
Location = "Mail Room"
URL = ""
For Each i In XLWsht.Range("D2:D11")
If StrComp(i.Cells.Value, Location) = 0 Then
URL = i.Cells.Offset(7, 0).Value
Exit For
End If
Next i
CreateObject("WScript.Shell").Run (URL)
End Sub
I think you're just missing as 's' in XLApp.WorkbookS.Open("C:\printers\schprint.xlsx")
That should fix the problem.
This question already has answers here:
How do I properly clean up Excel interop objects?
(43 answers)
Closed 8 years ago.
Dim oXL As Object
Dim oWB As Object
Dim oSheet As Object
' Start Excel and get Application object.
oXL = CreateObject("Excel.Application")
oXL.Visible = True
' Get a new workbook.
oWB = oXL.Workbooks.Add
oSheet = oWB.ActiveSheet
.........
oXL.Quit()
oWB = Nothing
oXL = Nothing
oSheet = Nothing
I can see the application opened in task manager ... Why ?
You need to do what #Afnan Makhdoom said, but not only what he said you also need to call...
GC.Collect()
This cleans up the objects being used and will remove it from task manager. Just releasing the objects won't remove it from task manager, you need to force the garbage collector.
Try this so it will remove the task manager process after you open the excel file:
Dim proc As System.Diagnostics.Process
For Each proc In System.Diagnostics.Process.GetProcessesByName("EXCEL")
proc.Kill()
Next
What you need to do is release the objects first, it will surely exit the Excel.exe
oSheet.Close(False)
oXL.Quit()
releaseObject(oXL)
releaseObject(oWB)
releaseObject(oSheet)
oWB = Nothing
oXL = Nothing
oSheet = Nothing
what do you think is wrong with the commented line in my vb.net code below? it returns the error message "member not found". thank you.
Public Function tae(ByVal SourcePath As String)
Dim oxlApp As Excel.Application
Dim oxlBook As Excel.Workbook
Dim oxlSheet As Excel.Worksheet
oxlApp = CType(CreateObject("Excel.Application"), Excel.Application)
oxlBook = CType(oxlApp.Workbooks.Open(SourcePath), Excel.Workbook) //somethings wrong here
oxlSheet = CType(oxlBook.Worksheets(1), Excel.Worksheet)
oxlApp.Workbooks.Close()
oxlApp.Quit()
End Function
i tried to add the following references and its now ok:
ms excel x object library
ms office x object library
visual basic for applications