Problems readin a Word document in Access VBA (1 Viewer)

sja13

Professional Idiot
Local time
Today, 06:39
Joined
May 3, 2017
Messages
63
I have the following code which I hoped would read an entire (short) document, but it doesn't- the problem is commented in the code. Can ayone help?
Code:
Sub ReadWordDoc()
Dim wdApp                       As Word.Application
Dim wdDoc                       As Word.Document
Dim strWordFile                 As String
Dim varRes                      As Variant

  strWordFile = "Z:\A file which exists.docx"
'*
'** Ensure only one instance of Word.
'*
  On Error Resume Next
  Set wdApp = GetObject(, "Word.Application")
  If Err.Number <> 0 Then 'Word isn't already running
    Set wdApp = CreateObject("Word.Application")
  End If
  On Error GoTo 0
'*
'** Open the Word document.
'*
  Set wdDoc = wdApp.Documents.Open(strWordFile)

  wdApp.Visible = True

'*
'** Select all the text (by inspection this
'** seems to happen)
'*
  wdDoc.Select
'*
'** Next bit just gives part of the document.
'** From inspection of Word, I get
'** 40 words,
'** 210 characters (243 inc spaces),
'** 6 paragraphs and
'** 8 lines.
'** The part I get splits mid word.
'** None of the above stats seem to be
'** 'magic numbers;.
'*
  varRes = wdDoc.Range
'*
'** Tidy up.
'*
  wdDoc.Close
  wdApp.Quit
End Sub
 

NauticalGent

Ignore List Poster Boy
Local time
Today, 01:39
Joined
Apr 27, 2015
Messages
6,341
Isn’t Range a collection that has to have a variable assigned and a Set statement to initialize? You seem to be missing your Dim and Set statements...
 

sja13

Professional Idiot
Local time
Today, 06:39
Joined
May 3, 2017
Messages
63
In case anyone is also struggling, here's some code to achieve the objective of reading a Word document in Access.

It's a different approach to my original.

It also works (at least for me!)

Code:
Option Compare Database
Option Explicit
Sub ReadWordDoc()
'#####################################
'# Needs Tools/References to         #
'# Microsoft Word n.n Object Library #
'#####################################
Dim parLine                     As Paragraph
Dim strLine                     As String
Dim strWordFile                 As String
Dim wdApp                       As Word.Application
Dim wdDoc                       As Word.Document
'*
'** Get basic details.
'*
  strWordFile = "C:\Your Word file.docx"
'*
'** Address the Word document.
'** To do this we need an instance of Word,
'** so create one if we don't already have
'** Word running.
'*
  On Error Resume Next
  Set wdApp = GetObject(, "Word.Application")
  If Err.Number <> 0 Then
    Set wdApp = CreateObject("Word.Application")
  End If
  On Error GoTo 0
'*
'** During testing, it's nice to see
'** what's going on.
'** Remove next line when happy.
'*
  wdApp.Visible = True
'*
'** Read the Word document into
'** individual text lines.
'** We also drop the trailing line feed
'** at the end of each paragraph.
'*
  Set wdDoc = wdApp.Documents.Open(strWordFile)
'*
'** Select the Word text, line by line
'** (actually, paragraph by paragraph!)
'*
  For Each parLine In wdDoc.Paragraphs
    strLine = Trim(parLine.Range.Text)
'*
'** Strip trailing Carriage Return.
'*
    Do Until Right(strLine, 1) <> vbCr
      strLine = Left(strLine, Len(strLine) - 1)
    Loop
'*
'** Optionally, skip blank lines.
'** (And PLEASE don't get all antsy
'** about using a GoTo. Used minimally
'** and locally they can, in my view,
'** aid legibility.)
'*
    If strLine = "" Then GoTo ReLoop
'*
'** OK, now process the line in whatever
'** way you want to here.
'*
ReLoop:
  Next parLine
  wdDoc.Close
  wdApp.Quit
End Sub 'ReadWordDoc
 

Users who are viewing this thread

Top Bottom