We are looking for some qualified individuals to work on a new Visual Basic site. The first task is developing a high-quality resource guide for Visual Basic developers. This resource guide will be a well-organized list of web sites that have very good resources about Visual Basic (from beginners stuff through advanced with topics like MTS and ASP). It will gradually expand to include other resources such as free source code and online shopping of development tools. If you are interested in working on a web site dedicated to Visual Basic jump to http://www.allvbstuff.com and fill out the form online. Thanks!
Download a free copy of NetMon - Your Internet Performance Monitor at http://www.codeoftheweek.com/netmon/index.html
In this issue we show how to integrate Microsoft Word with Visual Basic. This issue shows how to open Word, close Word, open documents and save them in the same or different file formats.
Questions? Email us at email@example.com.
The cWordIntegration class module contains a couple of properties and several methods for dealing with Word from Visual Basic. Using the source code supplied you can easily work with Word files in your application. An especially nice feature of this particular class is the ability to do batch conversion of files from one format to another using the file conversion features of Word.
To use this class you must add a reference to the Word library. To do this, click on the Project menu and the Reference option. Scroll down to the line that says "Microsoft Word 8.0 Object Library" and select it. To use this, you must have previously installed Word 97 or higher.
We have only touched on the capabilities that can be programmed with Word and Visual Basic. Be sure to check out the help file that comes with Microsoft Word called VBAWRD8.hlp. It contains a large amount of useful information about the object model that is exposed for your use.
If you have any questions about using this class, let us know at firstname.lastname@example.org
Public Filename As String
Name of the file to open. This is the full path name to the file on disk.
Public Property Get AppIsOpened() As Boolean
Returns True if this instance of the class opened a copy of Word.
Public Property Let FileFormat(vFileFormat As Word.WdSaveFormat) Public Property Get FileFormat() As Word.WdSaveFormat
The FileFormat property allows you to change the format that the Word document is saved as. Using this feature you can use Word as a simple file conversion program to convert documents from one format to another which Word support. This feature will only work using the FileSaveAs method since the FileSave method ignores the FileFormat property.
Refer to the SaveFormat property in the help file Vbawrd8.hlp (which is usually located in the "C:\Program Files\Microsoft Office\Office" folder. Some of the more common choices are:
Public Sub AppOpen(bVisible As Boolean)
Opens an instance of Word. If bVisible is True it will become the active window. If it is False Word would be hidden from view and your application would be able to stay in focus.
Public Sub AppClose()
Force the instance of Word started by this class to close. The user will be prompted to save changes if your program does not save them before closing.
Public Sub FileOpen()
FileOpen opens the document specified by Filename
Public Sub FileClose(bSave As Boolean)
The FileClose method has a parameter called bSave that should be True to force saving the file first or False to not save the changes made to the document. It then closes the document specified by Filename.
Public Sub FileSave()
FileSave saves the document specified by Filename.
Public Sub FileSaveAs()
FileSaveAs is the only method that allows you to save the file using a different format (see FileFormat in the Properties section).
Public Sub FileFormatList(lst As Variant)
This routine will load a combo box or list box with the available document formats which are installed on your system. It loads the ItemData property with the numeric value that you can pass to the FileFormat property. Using this option you can provide a mechanism to allow your users to save documents in additional formats, such as HTML.
See each function/property for details. In general errors are raised if parameters are not valid.
The below sample describes how to use the cWordIntegration class. It assumes the following: A form has been created with five Command buttons called cmdFormat, cmdClose, cmdOpen, cmdOpenFile, and cmdSaveAs. There are two text boxes called txtFilename and txtSaveFilename. There is one combo box called cmbFormat. This form allows you to open, close and save a file in Word format (or any other format that Word supports).
Option Explicit Dim WordObj As New cWordIntegration Private Sub cmdClose_Click() WordObj.AppClose End Sub Private Sub cmdFormat_Change() End Sub Private Sub cmbFormat_Click() If cmbFormat.ListIndex = -1 Then Exit Sub End If WordObj.FileFormat = cmbFormat.ItemData(cmbFormat.ListIndex) End Sub Private Sub cmdFormat_Click() On Error GoTo Handler WordObj.FileFormatList cmbFormat Exit Sub Handler: MsgBox Err.Description End Sub Private Sub cmdOpen_Click() On Error GoTo Handler WordObj.AppOpen True Exit Sub Handler: MsgBox Err.Description End Sub Private Sub cmdOpenFile_Click() On Error GoTo Handler WordObj.Filename = txtFilename WordObj.FileOpen Exit Sub Handler: MsgBox Err.Description End Sub Private Sub cmdSaveAs_Click() On Error GoTo Handler WordObj.Filename = txtSaveFilename WordObj.FileSaveAs Exit Sub Handler: MsgBox Err.Description End Sub
To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address: http://www.codeoftheweek.com/membersonly/bi/0095.html