Edit a Word doc from Access using VBA

D

Dennis

In Access2003, I need to programmatically open a Word doc, and do a single
Find and replace, then print the document and close it without saving the
change. Can someone please point me in the right direction? I'm very
experienced with VBA code, but this is a new task, and I don't know where to
start.

Thanks!
 
D

Doug Robbins - Word MVP

While the code in the following response does something completely
different, it will show you how to use Word from Access. Then you can find
information on the replace part from the article "Find & ReplaceAll on a
batch of documents in the same folder" at:

http://www.word.mvps.org/FAQs/MacrosVBA/BatchFR.htm

Doug Robbins - Word MVP said:
Margaret,

If you create the following folders

C:\WordData
C:\WordData\Processed

and you move all of the documents from which you want to extract the data
into the folder C:\WordData

The if you create a database named WordData in the C:\ folder and in it
you
create a table named tblWordData containing the following fields

Descriptor
Column1
Column2

and you create a Form in the database that contains a command button and
if
you have the following code in that command button Click event, it will
import all of the data from all of the tables in all of the documents in
the
C:\WordData folder into the table tblWordData

Dim dbsWordData As Database
Dim rstWordData As Recordset
Dim wordApp As Object
Dim vDescriptor As String
Dim vColumn1 As String
Dim vColumn2 As String
Dim FldrPath As String
Dim RecordDoc As String
Dim Source As Object
Dim SourceTable As Table
Dim i As Long, j As Long, k As Long
Dim tblrng As Range
Dim tblname As Range
Dim datarng As Range

Set dbsWordData = OpenDatabase("c:\WordData.mdb")
Set rstWordData = dbsWordData.OpenRecordset("tblWordData", dbOpenDynaset)

On Error GoTo CreateWordApp
Set wordApp = GetObject(, "Word.Application")
wordApp.Visible = False
On Error Resume Next

FldrPath = "C:\WordData\"

RecordDoc = Dir$(FldrPath & "*.doc")
k = 0
While RecordDoc <> ""
Set Source = wordApp.Documents.Open(FldrPath & RecordDoc)
With Source
For i = 1 To .Tables.Count
Set tblrng = .Tables(i).Range
Set tblname = tblrng.Duplicate
tblname.MoveStart wdParagraph, -1
tblname.End = tblname.Paragraphs(1).Range.End - 1
vDescriptor = tblname.Text
Set datarng = .Tables(i).Cell(1, 1).Range
datarng.End = datarng.End - 1
vColumn1 = datarng.Text
Set datarng = .tables(i).Cell(1, 2).Range
datarng.End = datarng.End - 1
vColumn2 = datarng.Text
With rstWordData
.AddNew
!Descriptor = vDescriptor
!Column1 = vColumn1
!Column2 = vColumn2
.Update
For j = 2 To Source.Tables(i).Rows.Count
Set datarng = Source.Tables(i).Cell(j, 1).Range
datarng.End = datarng.End - 1
vColumn1 = datarng.Text
Set datarng = Source.tables(i).Cell(j, 2).Range
datarng.End = datarng.End - 1
vColumn2 = datarng.Text
.AddNew
!Column1 = vColumn1
!Column2 = vColumn2
.Update
Next j
End With
Next i
End With
k = k + 1
Source.SaveAs "c:\WordData\Processed\" & Source.Name
Kill "c:\WordData\" & Source
Source.Close wdDoNotSaveChanges
RecordDoc = Dir
Wend
MsgBox "Data Imported from " & k & " documents."
wordApp.Quit

Set wordApp = Nothing
Set WordDoc = Nothing
Set rstWordData = Nothing
Set dbsWordData = Nothing

CreateWordApp:
Set wordApp = CreateObject("Word.Application")
Resume Next

After each document is processed, it is moved into the folder
C:\WordData\Processed and when all of the documents have been processed, a
message box will be displayed with the message "Data imported from #
documents."

Testing the above with two documents, each containing two tables with
Document#Table# in a paragraph before each table, it imported the data
into
the tblWordData as follows:

Descriptor Column1 Column2
Document1Table 1 D1T1R1C1 D1T1R1C2
D1T1R2C1 D1T1R2C2
D1T1R3C1 D1T1R3C2
Document1Table 2 D1T2R1C1 D1T2R1C2
D1T2R2C1 D1T2R2C2
D1T2R3C1 D1T2R3C2
Document2Table 1 D2T1R1C1 D2T1R1C2
D2T1R2C1 D2T1R2C2
D2T1R3C1 D2T1R3C2
Document2Table 2 D2T2R1C1 D2T2R1C2
D2T2R2C1 D2T2R2C2
D2T2R3C1 D2T2R3C2

Whether or not it will work for you will depend upon the meaning of your
word "generally".

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

in
message news:[email protected]...
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top