| >GRSoftware >VBA Tutor >Newsletter Tutorials >Tutorial 6 >Tutorial 8 |
VBA TUTOR NEWSLETTER ~ TUTORIAL SEVEN: Transfering data from Forms into WORD |
Due to several e-mails requesting help over the last month, this tutorial will look at how to transfer data from a form into a WORD document. The first task is to create a location in the WORD document or template for the data to be transfered to. The most common methods are to use tables and bookmarks. Tables are most commonly used, but bookmarks offer greater flexibility, especially when you want to create text strings. Look at the example WORD document form_demo.doc, which demonstrates the transfer of data from a userform into a 'table' cell, a 'bookmark' location and a 'Text FormField' location. The code used in form_demo.doc is shown below. Private Sub CommandButton1_Click() Dim myTable As Object 'select the bookmark object Selection.GoTo What:=wdGoToBookmark, Name:="bm_test" 'insert the value into bookmark location Selection = formdemo.TextBox1.Value 'select the text formfield bookmark object Selection.GoTo What:=wdGoToBookmark, Name:="FormText1" 'insert the value into text formfield bookmark location Selection = formdemo.TextBox1.Value 'select the first cell in the table object and insert the value Set myTable = ActiveDocument.Tables(1) myTable.Cell(1, 1) = formdemo.TextBox1.Value 'hide the form formdemo.Hide 'unload the form Unload formdemo 'release the resources set aside for MyTable Set myTable = Nothing End Sub This is a very simple tutorial, but I thought many beginner VBA users would like to have this issue clarified. OTHER RESOURCES for USING FORMS: [1] Documentation that guides you through creating a form from start to finish @ the MSDN Library. [2] A chapter on 'Using Forms ...' @ the MSDN Library. |
HOPEFULLY, THESE CODE EXAMPLES WILL ADD TO YOUR ABILITY TO BECOME A VBA POWER USER! |
| © 2000 Gary Radley |