Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 12th, 2004, 07:00 AM
Authorized User
 
Join Date: Jun 2004
Location: Boness, Falkirk, United Kingdom.
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default OLE Automation Access - Word

Hi - I was wondering if someone could help me? I'll start at the begining: I need a user to open a word document (from Access) which has some static text(mail merge) and I need to append data from a form to it. This isn't a problem I've used the basic code shown in the automation chapter 15 OF ACCESS 2003 VBA with some slight modifications - API call to CMDLG32.dll assigning the file name to a string and passing the string to the getobject function as a parameter. Now the problem starts I get 2 word documents opened, the merge template document and another "Form document" both with the same info? Is it possible only to open the new document with all the staic and dynamically assigned text?

Code snippit below. (Be kind to me I'm new to this game ;o))

Private Sub CmdGetCmnDlg_Click()
On Error GoTo Err_CmdGetCmnDlg_Click

Dim strPath As String
Dim WordDoc As Word.Document

strPath = GetFile // Function call to API common dialog returning selected document path.

If (strPath = "") Then
  Exit Sub
Else

Set WordDoc = GetObject(strPath)
WordDoc.Application.Visible = True

WordDoc.MailMerge.OpenDataSource *****...................etc
WordDoc.MailMerge.Execute
Cheers Wullie


William
__________________
William
  #2 (permalink)  
Old July 12th, 2004, 12:02 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Hi William,

If you work with a reference to a word application object instead of a document object, you can close the template file with:

objWord.Documents("Document1").Close wdDoNotSaveChanges

objWord is you application object varialbe. Replace "Document1" with the name of the merge template document that opens for you if its different.

For example:

Public Sub WordMailMerge()

    Dim objWord As Word.Application

    Set objWord = New Word.Application

    ' Get path to template file.
    strPath = GetFile

    objWord.Documents.Add (strPath)

    objWord.ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
    objWord.ActiveDocument.MailMerge.OpenDataSource ......etc

    With objWord.ActiveDocument.MailMerge
        .Destination = wdSendToNewDocument
        .Execute
    End With

    ' Close the template file
    objWord.Documents("Document1").Close wdDoNotSaveChanges

    objWord.Visible = True
    Set objWord = Nothing

 End Sub

HTH,

Bob

  #3 (permalink)  
Old July 13th, 2004, 07:23 AM
Authorized User
 
Join Date: Jun 2004
Location: Boness, Falkirk, United Kingdom.
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Bob I'll give it a go tonight as the project I'm working on is on my home machine. The solution looks so simple now.

Cheers Wm :o)

William


Similar Threads
Thread Thread Starter Forum Replies Last Post
Internal OLE Automation error sandythegenius Classic ASP Databases 2 May 11th, 2007 01:06 AM
Automation error when using Access to start Word jrnorth Access VBA 1 August 8th, 2005 03:07 AM
Access to Word Mail Merge Automation kareltje Access VBA 6 June 9th, 2005 04:54 AM
Access OLE Merge to Word GregoryHu Access VBA 5 May 7th, 2004 10:18 AM
Access OLE to Word GregoryHu Access VBA 0 March 1st, 2004 08:50 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.