How to: Export a DataReport to Microsoft Word (.doc)
By John D. Conway
In this article we'll create a simple database in Microsoft Access and then create a user interface in Visual Basic 6.0.
Putting everything together should be relatively easy because we'll use Adodc Componenet to make the connection to our Database.
We'll then add a DataEnvironment and DataReport and within minutes we'll be able to export our DataReport to a Microsoft Word Document. (.doc)
Creating our Database in Microsoft Access:
Create a blank database in Microsoft Access and name the database: DataReport. Now create a table by going to: Create Table in Design View and enter the Field Name(s) and Data Type(s) as shown in the image below. Save the table and name it: Customers.
Now, open the Customers Table and fill in the information you see in the image below.
Now let's move on to creating a user interface in Visual Basic 6.0. We're almost there. We"ll just place a few controls and in a short time we'll be able to Export our DataReport to a Microsoft Word. (.doc)
Creating our User Interface in Visual Basic 6.0
Open Visual Basic and choose: Standard EXE. Add a Adodc Component by going to Projects, Components and choose Microsoft ADO Data Control. Click Apply and then Close. You will now have a this Component in your ToolBox. Add 9 TextBox(s) named: Text1, Text2, Text3 and so on. Now add 9 Labels named: Label1, Label2, Label3 and so on. Change the Caption Property for each Label as shown in the image below. Add the Adodc Control from our ToolBox named: ADODC1. Add a CommandButton named: Command1. Change the Caption of the CommandButton1 to View Report. Now we just need to make the connection.
If you're wanting to Export a DataReport to Microsoft Word then I would guess that you already know the steps that need to be taken. This expanation will help those who are doing this for the first time or maybe have forgotten a step or two.
Right click ADODC1 control that we'd placed on Form1 and right click ADODC Properties. Choose: Use Connection String and left click Build. When prompted Choose Microsoft Jet 4.0 OLE DB Provider and click Next. Now, click the button to the right where you see 1. Select or enter a database name. Go to the location where you'd saved your database and double click. Check or Test your connection. If everything is on track you'll see a messagebox with the message "Test Connection Succeeded"-- Press OK to close the message and then press OK again.
Now, go to where you see RecordSource and left click. For the CommandType choose 2 - adCmdTable. Now go to Table or Stored Procedure Name and choose Customers (or whatever you'd named your Table) Now just click Apply and then click OK.
Now we'll set the DataSorce and DataField Properties to each Textbox that we'd place onto our Form1 earlier.
Click Text1 and then go to the Properties Window. Set the DataSource Property to ADODC1 and the DataField Property to CustID. (Note: All of the remaining TextBox(s) DataSource Properties will be set to ADODC1) Now go to Text2 and Set the DataSource Property. Set the DataField Property to LastName. Go to Text3 and Set the DataSource Property. Set the DataField Property to FirstName. Set the DataSource and DataField Properties for the remaining TextBox(s).
Adding a DataEnvironment and DataReport
Go to Project and choose More ActiveX Designers. Now choose Data Environment. Once opened - right click on Connection1 and choose Properties. Choose Microsoft Jet 4.0 OLE DB. Now, click the button to the right where you see 1. Select or enter a database name. Go to the location where you'd saved your database and double click. Check or Test your connection. If everything is on track you'll see a messagebox with the message "Test Connection Succeeded". Click OK.
Right click on connection again but this time choose Add Command. Now right click on Command1 and click Properties. Set the DatabaseObject to Table and then set the ObjectName to Customers. Click Apply and then click OK.
The final result will look like the image below.
We are now ready to add a DataReport.
Go to Project and Choose Add Data Report. Adjust the size of the DataEnvironment and DataReport to resemble the image below.
Click the DataReport. Go to DataSource and change the property to DataEnvironment1 and then change the DataMember Property to Command1.
Now place your cursor on CustomerID - left click and hold-Now just drag over to Detail(Section1) You'll see two CustID. Choose the one on the left and drag it above to PageHeading(Section2) Repeat the same process to place LastName, FirstName, MI, Phone, Address, City, State and Zip.
Now double click the commandbutton that's on Form1 to bring up the Code Window. Enter DataReport1.Show in the Command1_Click event.The final result will resemble the image below.
Start the project and then click the CommandButton to View Report. The report will look similar to the image below.
Notice the two buttons at the top left corner of your report. The button on the left will Print and the one on the right will allow us to Export. We'll use the Export feature in a moment.
You may need to adjust here and there to achieve the proper width of each column. This is where it will going to get a little tricky because we're only allowed a certain amount of space for the fields that we want to export.
What may look acceptable in the DataReport will not in Microsoft Word. So, I guess we can call this the experimental stage of the process. I've exported several DataReports this way and each time the results may vary. You'll get the hang of it once you've messed with it a time or two.
If you find yourself having trouble - there's a download at the bottom of this article that has everything you need.
Export a DataReport to Microsoft Word (.doc)
Start the project and click the View Report Button on the form. Click the Export button. Choose a location where you want to save the Documnent. Go to where you see File Name and type in the name DataReport.doc (Note: You must include .doc extension at the end of the File Name). Now go to Save as Type and choose TXT(*.txt) Now just click Save.
Go to where you'd saved the DataReport and double click to open. You will get a prompt that states "Microsoft Word can't import the specified format. The feature is not currently installed. Would you like to install it now?" Choose NO. The DataReport will now open in Microsoft Word. If the Report in Microsoft Word doesnt resemble the image below - Go to File ( In Microsoft Word) and then down to Page Setup...Go to PaperSize, choose LandScape and press OK. The report will adjust. If this doesnt fit all the fields the yoy will either have to - 1. Go back to the DataReport and make some adjustments, or 2. Go back to your DataBase and shorten the Field Names.
If the Report looks like you want it too and to avoid seeing the message that we encountered when we first opened MS Word to view the report - Go to file and choose Save As...Go to Save As Type and choose Word Document.
Private Sub Command1_Click()