1. Home
  2. Computing & Technology
  3. Visual Basic

VBA - Accessing Access

By , About.com Guide

8 of 10

Page 2 of the Code

~~~~~~~~~~~~~~~~~~~~~~~~~
Private Sub Command1_Click()
  Dim oApp As Word.Application
  Dim oWord As Word.Document

  Set oApp = _
    CreateObject("Word.Application")
  Set oWord = _
    oApp.Documents.Open( _
    FileName:="{file path}\aboutacw.doc")
  oApp.Visible = True
~~~~~~~~~~~~~~~~~~~~~~~~~

Unlike the previous Word examples, in this one, the entire Word application opens and the document is visible using the statement:

~~~~~~~~~~~~~~~~~~~~~~~~~
oApp.Visible = True
~~~~~~~~~~~~~~~~~~~~~~~~~

By sizing the Word window and single stepping through the VBA code in Excel, you can see the effect of the next statements while you run the program. Here's the code to copy the lines of the verse into rows in the Access database:

~~~~~~~~~~~~~~~~~~~~~~~~~
oApp.Selection.HomeKey _
    Unit:=wdStory
    oApp.Selection.EndKey _
      Unit:=wdLine, Extend:=wdExtend
    DoCmd.GoToRecord , , acNewRec
  Do Until oApp.Selection.End + 1 >= _
    oApp.ActiveDocument.Content.StoryLength
    Me.AboutText = oApp.Selection.Text
    DoCmd.GoToRecord , , acNewRec
    oApp.Selection.MoveDown Unit:=wdLine, _
      Count:=1
    oApp.Selection.HomeKey _
      Unit:=wdLine
    oApp.Selection.EndKey _
      Unit:=wdLine, Extend:=wdExtend
  Loop

Exit_WordDoc:
  Me.AboutText = oApp.Selection.Text
  DoCmd.GoToRecord , , acNewRec
~~~~~~~~~~~~~~~~~~~~~~~~~

Explore Visual Basic
By Category
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB 6
  5. Learn VBA
  6. VBA - Accessing Access

©2009 About.com, a part of The New York Times Company.

All rights reserved.