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):
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. As I said, I'm pretty new to VB.
Any suggestions as to what might work? (I'm using Access 2003)
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. As I said, I'm pretty new to VB.
Any suggestions as to what might work? (I'm using Access 2003)