Thursday, May 1, 2014

Visual Basic Tutorial | How to Make File (.txt or .docx) in Visual Basic 2010

Hey, I have a new tutorial to share to you guys, this tutorial is very exciting and good practice for newbies in programming.

Today we are going to work with files, different types of files, this tutorial is all about making file in visual basic 2008 and above versions. (I do not know if this codes will worked at vb6, well I guess it's worth a try)

Now, we all know that file is very important in digital world. OS and other programs are made out of file that contains a series of codes and instructions.

There are lots of common files that can be made in visual basic. I will give you some of the common files used nowadays.

Here are samples of common Text Files

.docMicrosoft Word Document
.docxMicrosoft Word Open XML Document
.logLog File
.msgOutlook Mail Message
.odtOpenDocument Text Document
.pagesPages Document
.rtfRich Text Format File
.texLaTeX Source Document
.txtPlain Text File
.wpdWordPerfect Document
.wpsMicrosoft Works Word Processor Document

and here are some of the common Data Files 

.csvComma Separated Values File
.datData File
.gbrGerber File
.gedGEDCOM Genealogy Data File
.ibooksMulti-Touch iBook
.keyKeynote Presentation
.keychainMac OS X Keychain File
.ppsPowerPoint Slide Show
.pptPowerPoint Presentation
.pptxPowerPoint Open XML Presentation
.sdfStandard Data File
.tarConsolidated Unix File Archive
.tax2012TurboTax 2012 Tax Return
.vcfvCard File
.xmlXML File

In visual basic we can make a file that is listed above, we just need a simple code yet powerful to create files.

How to make a static file?

If you want to create a static file with fix file name and fix location where to save it you will need to write this code.

Dim path as String = "C:\MyFile.txt"Dim FS as FileStream = File.Create(path)Msgbox(path + " created")

As you notice the file name and the location of the file to be created are programmatically fixed as "C:\MyFile.txt". This is the very basic code on how to create a static file.

How to make a Dynamic file?

If you are planning to create a file that you want to save it on a specific drives, put a specific file name and put a specific file extension you will need some additional controls on your project. Put this controls in your program.

1 ComboBox = List all the drives where to save the file
1 ComboBox = List all the specific file extensions 
1 Textbox = Field to type a specific file name for new file to be created
1 Button = Set as Create File

How to edit items on combo box?

Once all the controls are set and ready for coding, select 1 combo box and you will notice that there is a small arrow on top of it. Click it and select 'Edit Items', Now each item must be separated by new line, after typing an item just press Enter to go to the next line.

now that all the controls are ready, you will need the following code to create or make a dynamic file types in visual basic.

Dim path as string = Combobox1.text + Textbox1.text + Combobox2.textDim FS as FileStream = File.Create(path)Msgbox(path + " Has been created")
Visual Basic Tutorial | How to Make File (.txt  or .docx) in Visual Basic 2010
Your program must look like this


Note that this code must be written into the button1_click event that serve as the 'Create File' button.

To check that the file has been created, browse on the drive where you set as the location of the file you created.

I have uploaded a video tutorial on how you can make a file with visual basic 2010 for beginners just to get you some example or what is this tutorial about.

Watch Video Tutorial




No comments:

Post a Comment