Problems with reading from & writing to a Word document (1 Viewer)

catacaustic

Wannabe Access Hacker
Local time
Yesterday, 23:49
Joined
Jan 3, 2007
Messages
14
Hi all, I seem to have had a very small "breakthrough" with another problem that has led me here. What I'm trying to do is somehow get a 12-page Word document to be almost "mail merged". There is only going to be one done at a time, so it's not a real lerge job.

What I'm trying to do is read the file into my Access form, do a search-and-replace on the correct character strings that I've put into the Word file, and save the file again under a new file name. The problem is that as this is a Word document, there is something going wrong with it when I save it. Word won't open the file, saying "Word has experienced an error" and asks me to open the file in recovery mode. When I've tried this it still doesn't work.

The code that I've got so far for this is (and yes... I don't think it's great code, but I've been doing Access and VB for about 2 days now, so I have little idea about it all yet):

Code:
Dim FileName As String
    Dim OutputFile As String
    Dim billOfSale As String
    
    FileName = "C:\Bill_Of_Sale.doc"
    OutputFile = "C:\BillOfSale_Complete.doc"
    
    ' Read in the Word file for the Bill of Sale
    Const ForReading = 1
    Const ForAppending = 8
    
    ' Create the file system object for reading and writing
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    ' Check if the template file exists
    If objFSO.FileExists(FileName) Then
        ' The file exists, so read in the file
        Set objTextFile = objFSO.OpenTextFile(FileName, ForReading)
        billOfSale = objTextFile.ReadAll
        
        ' Replace the correct areas of the file
        ' billOfSale = Replace(billOfSale, "%first_name%", "Michael")
        
        ' Save the file as a new file
        Set objTextFile = objFSO.OpenTextFile(OutputFile, ForAppending, True)
        objTextFile.Write (billOfSale)
        
        ' Tell the user that the process has been completed!
        MsgBox "Bill Of Sale Written!!", vbOKOnly
    Else
        ' There is no template file in the specified location
        MsgBox "The Bill Of Sale document does not exist", vbOKOnly
    End If

I get the feeling that opening and saving the file using the text file functions could be where I'm going wrong, but I have no idea of what else to use. :confused: As I said, I'm pretty new to VB.

Any suggestions as to what might work? (I'm using Access 2003)
 

Moniker

VBA Pro
Local time
Today, 01:49
Joined
Dec 21, 2006
Messages
1,567
I explained how to open and edit a Word document from Access in a previous post. The info there should point you in the right direction. (Hint: You were right to question using the text objects to edit a Word document.) Also note that the post I'm referring you to is titled, " How to open a Word document from within Access". I think the Search function of this forum would've worked for you here. :)
 

catacaustic

Wannabe Access Hacker
Local time
Yesterday, 23:49
Joined
Jan 3, 2007
Messages
14
Not sure if anyone else has got any ideas, but I "seem" to have almost solved my own problem. So far at least.

What I've done is ssaved the Word document as a Rich Text File which still retains the formatting (so far at least), there's no hassles with the binary way that Word saves it's files, and I can actually write to a new RTF file keeping all of the formatting and the info that needed to be inserted.

Hopefully this might help someone else out there.

Anyone else got any other ideas that can work?
 

catacaustic

Wannabe Access Hacker
Local time
Yesterday, 23:49
Joined
Jan 3, 2007
Messages
14
Looks very good Moniker. I did try searching for this thread, but I'm thinking that my search terms weren't as good as yours (I never even knew that existed).

I'll give that a go too and see how that works!
 

Users who are viewing this thread

Top Bottom