using Word in Access - insert file

J

John Piwko

I have a very large report - 300+ pieces of data to insert
into it. So, I cannot use just one table or query with
which to create the merge document. I have created the two
merge documents and they work well. What I want to do is
to have the two merge documents combine, print out, be
saved as one large Word document and then delete the two
smaller documents....all from within access..........any
ideas? I will post the code that creates and saves the two
smaller documents.

Dim HRName As String
Dim HUDa As String
Dim HUDb As String
HRName = strRPID (This is defined above this section of
code)
Dim objWord As Word.Application, o_doc As Word.Document
strFilePath1 = "c:\db\HUDANNUAL1.doc" (This is a word
merge document)
Set objWord = New Word.Application
Set o_doc = objWord.Documents.Add(strFilePath1)
objWord.Visible = False
o_doc.MailMerge.MainDocumentType = wdNotAMergeDocument
objWord.ChangeFileOpenDirectory "c:\db\HR"
HUDa = CStr(HRName) & "a.doc"
o_doc.SaveAs FileName:=CStr(HRName) & "a.doc"
o_doc.Close SaveChanges:=wdDoNotSaveChanges
strFilePath2 = "c:\db\HUDANNUAL2.doc" (This is a Word
merge document)
Set objWord = New Word.Application
Set o_doc = objWord.Documents.Add(strFilePath2)
objWord.Visible = False
o_doc.MailMerge.MainDocumentType = wdNotAMergeDocument
objWord.ChangeFileOpenDirectory "c:\db\HR"
HUDb = CStr(HRName) & "B.doc"
o_doc.SaveAs FileName:=CStr(HRName) & "b.doc"
o_doc.Close SaveChanges:=wdDoNotSaveChanges
'Here I would like to open HUDa, insert HUDb into it and
'then save that combined document as CStr(HRName) & ".doc"
'and then delete HUDa & HUDb because I no longer need them
objWord.ChangeFileOpenDirectory "c:\Documents and
Settings\Administrator\My Documents"
objWord.Quit
Set o_doc = Nothing
Set objWord = Nothing

Any and all ideas appreciated
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Hi John,

This is untested some may need a tweak

Dim Adoc As Document, BDoc As Document, i As Long, arange As Range
Set Adoc = ODoc.Documents.Open("a.doc")
Set BDoc = ODoc.Documents.Open("b.doc")
For i = 1 To Adoc.Sections.Count
Set arange = Adoc.Sections(i).Range
arange.End = arange.End - 1 'eliminate the section break from arange
BDoc.Sections(i).Range.InsertBefore arange
Next i

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
 
J

John P. Piwko

I see what you are doing, but I don't need to do that. I am doing a mailmaerge on a set of data for a single client. I just need to insert HUDb into HUDa. This will be the complete data for one client. Then I would like to delete the HUDa & HUDb files. Thanks for your help.
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?Sm9obiBQLiBQaXdrbw==?=,
am doing a mailmaerge on a set of data for a single client. I just need to
insert HUDb into HUDa. This will be the complete data for one client. Then I
would like to delete the HUDa & HUDb files.For the first part, does InsertFile work for you? If not, why not?

Deleting the files is a bit more problematic, as Word has a tendency to keep
files "locked". But in theory you can use the KILL method to delete any file
from disk. In practice, you may find you can't use it immediately, during
your macro. Some programmers often do file deletion at the beginning of the
next "run" for this reason.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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