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 

Explanation about the function SearchFolders

Search Folder function is used to grab the search results in a collection and will return while the fuction is called. With this function we can just pass the parameters to the recursive function ListFolders and get the returned collection.

Explanation about the function ListFolders


Declare and intialize the objects, and variables that are required for the functions
Dim fold As Scripting.Folder
Dim foldSub As Scripting.Folder
Dim fil As File
Dim fso As New FileSystemObject
Dim strOu ...

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 

How this function list folders and files recursively

Declare all necessary objects
  
  Dim fold As Scripting.Folder
    Dim foldSub As Scripting.Folder
    Dim fil As File
    Dim fso As New FileSystemObject
    Dim strOutput As String

For the scripting.*  needs Microsoft Scripting runtime reference

Read more here:   List Folders And Files