Creating a word document from the current record in an Access Form 
Author Message
 Creating a word document from the current record in an Access Form

I am attempting to create a single word document (from a word template) with
several fields (Mailmerge fields) populated with fields from the current
form/record.
There will be only one document created but with the relevant fields filled.
I have been attempting to use:

Dim appWord As New Word.Application
Dim worddoc As Word.Document
Dim lstrTemplate As String, lstrPath As String
lstrTemplate = "some path\Transmittal.dot"
Set worddoc = appWord.Documents.Add(lstrTemplate, , , False)
  worddoc.MailMerge.Fields.Item("Address") = Me.Address
lstrPath = "Another Path\"
worddoc.SaveAs lstrPath & Me.ReferenceNo & ".doc", wdFormatDocument, True
Set worddoc = Nothing
appWord.Quit
Set appWord = Nothing

with little success, without the worddoc.mailmerge line the document is
created but I need to populate the fields (there are only 3) in the template
before saving it.

I am very familiar with Access and VB but none too familiar with cross
office automation etc.
Can someone please point me in the right direction or provide me with the
obvious simple answer!?

Jeff



Mon, 21 Jun 2004 17:40:03 GMT  
 Creating a word document from the current record in an Access Form
Hi Jeff,

Mmmm, mail merge isn't meant to be used quite like this. MUST you use a
template with merge fields? Or are you free to set up your own template?

Quote:
> There will be only one document created but with the relevant fields filled.
> I have been attempting to use:

> Dim appWord As New Word.Application
> Dim worddoc As Word.Document
> Dim lstrTemplate As String, lstrPath As String
> lstrTemplate = "some path\Transmittal.dot"
> Set worddoc = appWord.Documents.Add(lstrTemplate, , , False)
>   worddoc.MailMerge.Fields.Item("Address") = Me.Address
> lstrPath = "Another Path\"
> worddoc.SaveAs lstrPath & Me.ReferenceNo & ".doc", wdFormatDocument, True
> Set worddoc = Nothing
> appWord.Quit
> Set appWord = Nothing

> with little success, without the worddoc.mailmerge line the document is
> created but I need to populate the fields (there are only 3) in the template
> before saving it.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister
http://www.mvps.org/word
http://go.compuserve.com/MSOfficeForum

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



Mon, 21 Jun 2004 20:53:19 GMT  
 Creating a word document from the current record in an Access Form
Cindy,
Yes, I have created the Template myself, so can do what I like with it. It
sounds like you might have a simpler solution!?
Regards
Jeff



Quote:
> Hi Jeff,

> Mmmm, mail merge isn't meant to be used quite like this. MUST you use a
> template with merge fields? Or are you free to set up your own template?

> > There will be only one document created but with the relevant fields
filled.
> > I have been attempting to use:

> > Dim appWord As New Word.Application
> > Dim worddoc As Word.Document
> > Dim lstrTemplate As String, lstrPath As String
> > lstrTemplate = "some path\Transmittal.dot"
> > Set worddoc = appWord.Documents.Add(lstrTemplate, , , False)
> >   worddoc.MailMerge.Fields.Item("Address") = Me.Address
> > lstrPath = "Another Path\"
> > worddoc.SaveAs lstrPath & Me.ReferenceNo & ".doc", wdFormatDocument,
True
> > Set worddoc = Nothing
> > appWord.Quit
> > Set appWord = Nothing

> > with little success, without the worddoc.mailmerge line the document is
> > created but I need to populate the fields (there are only 3) in the
template
> > before saving it.

> Cindy Meister
> INTER-Solutions, Switzerland
> http://homepage.swissonline.ch/cindymeister
> http://www.mvps.org/word
> http://go.compuserve.com/MSOfficeForum

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



Tue, 22 Jun 2004 09:30:07 GMT  
 Creating a word document from the current record in an Access Form
Cindy
I took Schmeros's suggestion of using bookmarks and played around a bit as
posted below.
The only problem I see with this is that if the client edits the word
template (and the bookmarked locations are not visible) there is a risk that
the bookmarks will not end up being in the correct positions - but I see
this as the cleints problem :)
If there is still a better way to do this please let me know.
Regards
Jeff

  SysCmd acSysCmdSetStatus, "Creating Transmittal - Please wait..."
  DoCmd.Hourglass True

  ' Open new document instance using the configured template
  lstrTemplate = GetConfigItem("TransmittalTemplate")
  Set worddoc = appWord.Documents.Add(lstrTemplate, , , True)

    ' Locate Bookmarks and Insert text from current record
    worddoc.Bookmarks("TranmittalNo").Select
    Selection.TypeText Text:=Me.ReferenceNo
    worddoc.Bookmarks("Name").Select
    Selection.TypeText Text:=Me.NameID.Column(1)
    worddoc.Bookmarks("Company").Select
    Selection.TypeText Text:=Me.NameID.Column(2)
    worddoc.Bookmarks("Address").Select
    Selection.TypeText Text:=Me.NameID.Column(3)

  ' Save instance as new document on disk
  lstrPath = GetConfigItem("DocumentsLocation")
  worddoc.SaveAs lstrPath & "\" & Me.ReferenceNo & ".doc", wdFormatDocument,
True

  ' Set Created flag and cleanup
  Me.TransmittalCreated = True
  Set worddoc = Nothing
  appWord.Quit
  Set appWord = Nothing

  SysCmd acSysCmdClearStatus
  DoCmd.Hourglass False

=================================================================



Quote:
> Hi Jeff,

> Mmmm, mail merge isn't meant to be used quite like this. MUST you use a
> template with merge fields? Or are you free to set up your own template?

> > There will be only one document created but with the relevant fields
filled.
> > I have been attempting to use:

> > Dim appWord As New Word.Application
> > Dim worddoc As Word.Document
> > Dim lstrTemplate As String, lstrPath As String
> > lstrTemplate = "some path\Transmittal.dot"
> > Set worddoc = appWord.Documents.Add(lstrTemplate, , , False)
> >   worddoc.MailMerge.Fields.Item("Address") = Me.Address
> > lstrPath = "Another Path\"
> > worddoc.SaveAs lstrPath & Me.ReferenceNo & ".doc", wdFormatDocument,
True
> > Set worddoc = Nothing
> > appWord.Quit
> > Set appWord = Nothing

> > with little success, without the worddoc.mailmerge line the document is
> > created but I need to populate the fields (there are only 3) in the
template
> > before saving it.

> Cindy Meister
> INTER-Solutions, Switzerland
> http://homepage.swissonline.ch/cindymeister
> http://www.mvps.org/word
> http://go.compuserve.com/MSOfficeForum

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



Tue, 22 Jun 2004 11:09:42 GMT  
 Creating a word document from the current record in an Access Form
Hi Jeff,

Quote:
> I took Schmeros's suggestion of using bookmarks

Bookmarks is a good approach, but I don't like the code
Schmero gave you :-) A bit inefficient.

For other approaches (and more efficient code), this
article could be useful:

http://www.msofficepro.com/features/2000/12/vba200012cm_f/v
ba200012cm_f.asp

You'll need to sign up for a (free, one-month trial)
subscription in order to access the archives.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister
http://www.mvps.org/word
http://go.compuserve.com/MSOfficeForum

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



Tue, 22 Jun 2004 17:56:26 GMT  
 Creating a word document from the current record in an Access Form
Thx for that Cindy...I will follow you suggestion up...
regards
Jeff



Quote:
> Hi Jeff,

> > I took Schmeros's suggestion of using bookmarks

> Bookmarks is a good approach, but I don't like the code
> Schmero gave you :-) A bit inefficient.

> For other approaches (and more efficient code), this
> article could be useful:

> http://www.msofficepro.com/features/2000/12/vba200012cm_f/v
> ba200012cm_f.asp

> You'll need to sign up for a (free, one-month trial)
> subscription in order to access the archives.

> Cindy Meister
> INTER-Solutions, Switzerland
> http://homepage.swissonline.ch/cindymeister
> http://www.mvps.org/word
> http://go.compuserve.com/MSOfficeForum

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



Wed, 23 Jun 2004 10:14:35 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Accessing current record in MS Access with DDE from MS Word

2. Opening specified Word documents per record in a form

3. Accessing the current record from one form to another

4. Updating current record used by a Form?- VBA in Access 97

5. Linking Access records to Word Documents

6. Creating Word Document from Form Entry on Web Pages

7. Access crashes after creating Outlook/Word document

8. Creating Word Documents from Access

9. Creating Word Documents in Access VBA

10. A97: Create Word document in Access

11. Creating Embedded word documents from within Access using VBA

12. Function to seek record and make current record on form

 

 
Powered by phpBB® Forum Software