Export to Excel using VB

In this article, I am going to explain how to export data to a spreadsheet from the application. This code needs the excel application to be installed in the machine.Make sure  Excel is referenced in the project.

For a silent operations (no Windows) objXLApp.Visible as false and for formatting we need to format range object of worksheet.

Define WorkBook

For accessing a work sheet we must initialize Application and work book.
     Dim objXLApp As New Excel.Application
     Dim objExBook As Excel.Workbook

Access the Work Book and WorkSheets

Workbooks are the equivalent of a spread sheet file in the disk. Hence, to create to a new file we need to create a new workbook as follows.
     Set objExBook = objXLApp.Workbooks.Add

Every workbook will load with three work sheets so probably we may be intereste ...

Simple function to read entire text file by VB open file

To just read an entire text file the following function can be used. This function is not using any external references. Since File Open is a built in function.

This function opens a given file using file numbers. So we need to close the file by file no. Or else the file will be locked.

This function does not use FileSystemObject and using open file function to open the text file

Private Sub Command1_Click()
    MsgBox SimpleFileRead("c:\boot.ini")
End Sub
'*  Purpose :   Read entire text file using File Open
'*  Inputs  :   strPath(String)path of the text file ...

Visual Basic Function to calculate the available space in a drive

This function can be used to calculate the available space in a drive

Public Function GetDriveAvailableSpave(ByVal strDrive As String) As Double
    Dim fso As New FileSystemObject
    GetDriveAvailableSpave = fso.GetDrive(strDrive).AvailableSpace
End Function

Microsoft Scripting runtime should be referenced


Sample function to count words in a text file using VB6

As all we now there is no function available to count words in Classic VB out of the box. However writing functions for counting words are very easier.
Most of the text related operation we may need to count the words in a given string. Alternatively, some times we may need to find the words in a file or even need to count the words in all files in a given directory. For all the above requirements, we can customize the following function easily.

In this article, I am going to explain how to count the words in a text file. We can call this function as follows.

Read more here:   Count words in a text file by Classic VB 

Get the Disk Partition Type Using VB6

' **
'*  Purpose :   To Get The disk partition type
'*  Inputs  :   strServer       :   Server to connect
'*  Returns :   Collection with partition type info as comma seperated values
'*              like [PartitionName],[PartitionType] C:,NTFS
' **

Public Function DiskPartitionType(ByVal strServer As String) As Collection
    Dim objWMIService, objItem
    Dim colItems
    Read more here:   Get the Disk Partition Type Using VB6 

Search Files, File Contents(text) and Folders recursively by File Extension using VB6

Basically any recursive function can be made from a simple normal non recursive function.
To traverse the folder structure we need to perform the following steps
1. Get Access to the folder you starts with
2. Get access to all files and read data using file system object
3. add all the files which satisfies the search criteria to a collection
4. Take the next sub folder and go to do the steps from point 1

This article demonstrates how to recursively search directories, subfolders and files. Files can be searched based on a certain criteria. Directory recursion is vital for developers who extensively work on file related tasks. The FileSystemObject makes this is e Read more here:   VB6 Recursive File Content Search 

Get file names using VB6

There are several situations we are looking for getting all the file names from a folder. This is not a tough task, but the real pain is in getting list of files from FTP folders.

Internet Transfer Control

To get the file names in the ftp location with internet transfer control . Internet transfer control has to be referenced in the calling form. This control can interact with the remote FTP server and get the information and do the necessary actions.  We can issue a DIR command to fetch the data. One thing about the inet control is, it will be executing command in asynchronous manner. So you need to wait till it finishes its operations.  We can put a looping construct to keep it idle but wait till the execution re ...

Read more here:   Get file names from FTP Folder 

Sample code to get the file count of a folder using VB6

This function counts only the folder we specified. It will not include the sub folders. To access subfolders we need to use a recursive function.


FileSystemObject is a very good object for working with IO operations in classic VB. It has lot of interesting functions. With little bit work we can achieve what we need.


To use fileSystemObject to find the count of files

Samples to create a text file from collection

This function will be able to create a new text file or append an existing text file.

All the collection elements will be placed in the same order as it is in the collection.

Read more here:   Collection To Text File 

Sample program to read and search text file line by line into a collection

This function exports the text file content to collection based on the line breaks. If search text is not passed, then  the entire content will be exported to the collection line by line. If search content is specified then the search results will be exported to the collection. If the search by flag is passed as regex then regular expressions will be used to search the file content.

Source Code

Public Enum GrabMode
    Include = 0

VB6 Read Text File

With this artcle, I am going to explain various ways we can Read Text File into a string variable using Classic VisualBasic.

The first and easy way to acess a txt file is using the simple open method available in VB. For this method, we need to allocate a unique file number for opening. And the same number should be used to close.

There are various modes to open some of them are Append, Binary, Input, Output, or Random. by default Random will be used.

In this code I have closed the connection after error has been handled(or intended to be handled). This close method  will not do anything if the file number is not used to open. so it is safe even error has been occured before opening.

Using FileSystemObject in VB6 Read Text File

With FileSystemObject File IO operations are very easier in VB. Almost all the required functions are well defined. For reading, we hav ...

Read more here:   Read Text File into string VB6 

Write a text file from string

This is a sample program to write text file from string. The string should be well formed, The string will be written as it is. So the line breaks should be placed when prearing the string before passing it to the function.

Visual basic is a nice language to write a simple File.Io operations. It is not only simpler to write but also very easy to debug. Using this kind of helpers can improve the code re-usablity also.I have written already a lot of articles related to File operations like reading file, searching files using regular expressions.

There are various ways to read write files.  I prefer the file system objects to handle the file system, As it looks very neat and easy to be handled. When ever working in file stream it is really necessary to close the stream. Otherwise it will be really not efficient.

By using the IsOverwrite parameter we can append text to the existing file. If IsOverwrite is false then the tex ...

Read more here:   Write a text file from strings 

List files and folders including subfolders using VB 6.0

While programming file, folders and IO related applications, scanning a directory is an important task which may need frequently too. Sometimes we may need to list files in the directory as well the subfolder also. In .Net this is very easy, you need to specify a parameter to include the subfolders. It does all internally and you will get access to all the folders from a single function itself.

How to List Folder and List Files

This Function loops thro the folder and scan for all objects if it is a file it List files and if it is a directory it goes in to the directory and scans further down. Hence entire directory structure can be scanned and able to List files and List Folders including subfolders.

This is a very basic and simple function and can be customized to cater for suitable requirements.

• If only folder name needs to be printed then comment the line in file listing.

Read more here:   List Folders And Files 

Spam Bot Trap


Select Theme