Import Word doc into memo field

  • Thread starter Thread starter Amber
  • Start date Start date
A

Amber

I have about 600 documents that I would like to copy and
paste the contents into the corresponding memo fields in
my database.

In my code, I'm using Word to open the docs and then using
record("Description") = objWord.Selection.Text to put the
text in the field. I've got it to work, but it's not
picking up the carriage returns (although it is when I do
it manually). What I want to do is somehow use the
PasteAndFormat method, but I can't figure out how...

Is there a more slick way to do this? The raw files are
actually documents I FTP'd from a UNIX box, and I'm not
even sure what format they are...

Help!
 
Hi Amber,

Memo fields normally just store plain text, and the textbox that Access
uses to display them doesn't understand anything else (not even tabs,
which show up as little boxes).

If the documents are plain text (you can tell they are if they look OK
when you view them in Notepad) you can use the function below, which
reads the contents of a file into a variable as a string; it's slicker
than using Word.

The Unix origin may complicate matters a bit. Unix text files use LF -
Chr(10) - between lines, while Windows text files (and Access textboxes)
use CRLF - Chr(13)&Chr(10). If the files look good in Notepad but have
little boxes instead of linebreaks in Access, this may be the cause. If
so, replace all the LFs with CRLFs (in VBA:
strFileContents = Replace(strFileContents, vbLF, vbCRLF)
).

Just to keep things simple <g> Word uses just CR - Chr(13) - for its
paragraph marks and Chr(11) for manual line breaks. So when you do
strText = objWord.Selection.Text
you need to follow it with
strText = Replace(strText, vbCR, vbCRLF)
strText = Replace(strText, Chr(11), vbCRLF)
to make it like a normal Windows text file. (This is done automatically
when you save a Word document as plain text.)

If the documents aren't plain text and you want to preserve the
formatting, it's possible but a good deal more complicated. Post back if
this is what you need.



'CODE STARTS********************************
Function FileContents(FileSpec As Variant, _
Optional ReturnErrors As Boolean = False, _
Optional ByRef ErrCode As Long) As Variant
'Retrieves contents of file as a string
'Silently returns Null on error unless
' ReturnErrors is true, in which case
' uses CVErr() to return an error value.
' Optionally, you can retrieve the error
' code in the ErrCode argument

Dim lngFN As Long

On Error GoTo Err_FileContents
If IsNull(FileSpec) Then
FileContents = Null
Else
lngFN = FreeFile()
Open FileSpec For Input As #lngFN
FileContents = Input(LOF(lngFN), #lngFN)
End If
ErrCode = 0
GoTo Exit_FileContents

Err_FileContents:
ErrCode = Err.Number
If ReturnErrors Then
FileContents = CVErr(Err.Number)
Else
FileContents = Null
End If
Err.Clear
Exit_FileContents:
Close #lngFN
End Function
'END OF CODE***************************
 
Thanks, John, that's exactly what I needed to know. My
program works like a charm now, and I'm glad I didn't need
Word.

I'm relatively new to programming, and it's nice to know
there is help out there!

Thanks again,
Amber
-----Original Message-----
Hi Amber,

Memo fields normally just store plain text, and the textbox that Access
uses to display them doesn't understand anything else (not even tabs,
which show up as little boxes).

If the documents are plain text (you can tell they are if they look OK
when you view them in Notepad) you can use the function below, which
reads the contents of a file into a variable as a string; it's slicker
than using Word.

The Unix origin may complicate matters a bit. Unix text files use LF -
Chr(10) - between lines, while Windows text files (and Access textboxes)
use CRLF - Chr(13)&Chr(10). If the files look good in Notepad but have
little boxes instead of linebreaks in Access, this may be the cause. If
so, replace all the LFs with CRLFs (in VBA:
strFileContents = Replace(strFileContents, vbLF, vbCRLF)
).

Just to keep things simple <g> Word uses just CR - Chr (13) - for its
paragraph marks and Chr(11) for manual line breaks. So when you do
strText = objWord.Selection.Text
you need to follow it with
strText = Replace(strText, vbCR, vbCRLF)
strText = Replace(strText, Chr(11), vbCRLF)
to make it like a normal Windows text file. (This is done automatically
when you save a Word document as plain text.)

If the documents aren't plain text and you want to preserve the
formatting, it's possible but a good deal more complicated. Post back if
this is what you need.



'CODE STARTS********************************
Function FileContents(FileSpec As Variant, _
Optional ReturnErrors As Boolean = False, _
Optional ByRef ErrCode As Long) As Variant
'Retrieves contents of file as a string
'Silently returns Null on error unless
' ReturnErrors is true, in which case
' uses CVErr() to return an error value.
' Optionally, you can retrieve the error
' code in the ErrCode argument

Dim lngFN As Long

On Error GoTo Err_FileContents
If IsNull(FileSpec) Then
FileContents = Null
Else
lngFN = FreeFile()
Open FileSpec For Input As #lngFN
FileContents = Input(LOF(lngFN), #lngFN)
End If
ErrCode = 0
GoTo Exit_FileContents

Err_FileContents:
ErrCode = Err.Number
If ReturnErrors Then
FileContents = CVErr(Err.Number)
Else
FileContents = Null
End If
Err.Clear
Exit_FileContents:
Close #lngFN
End Function
'END OF CODE***************************


I have about 600 documents that I would like to copy and
paste the contents into the corresponding memo fields in
my database.

In my code, I'm using Word to open the docs and then using
record("Description") = objWord.Selection.Text to put the
text in the field. I've got it to work, but it's not
picking up the carriage returns (although it is when I do
it manually). What I want to do is somehow use the
PasteAndFormat method, but I can't figure out how...

Is there a more slick way to do this? The raw files are
actually documents I FTP'd from a UNIX box, and I'm not
even sure what format they are...

Help!

--
John Nurick [Microsoft Access MVP]

Please respond in the newgroup and not by email.
.
 
Back
Top