2010-07-30 1 views
2

액세스에서 문서를 열고 편지 병합을 실행 한 다음 VBA를 사용하여 병합 된 문서 출력을 저장하려고합니다.메일 병합 액세스 - 병합 된 문서 저장

여기 내 현재 시도 : 나는 병합 된 문서를 얻을 수

Dim templateName as String, tempRoot as String 
tempRoot = "C:\report\" 
templateName = tempRoot & "template.doc" 

Dim objDoc As Word.Document 
Dim objWord As New Word.Application 
Set objDoc = objWord.Documents.Open(templateName) 

objWord.Visible = True 

exportData "AnnualData", tempRoot & "annualData.txt" 'Outputs query to txt file for merge 

objDoc.MailMerge.OpenDataSource NAME:= _ 
    tempRoot & "annualData.txt", ConfirmConversions:=False, ReadOnly _ 
    :=False, LinkToSource:=True, AddToRecentFiles:=False, PasswordDocument:= _ 
    "", PasswordTemplate:="", WritePasswordDocument:="", _ 
    WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _ 
    Connection:="", SQLStatement:="", SQLStatement1:="", SubType:= _ 
    wdMergeSubTypeOther 

objDoc.MailMerge.Execute 
objDoc.Close False  'Ideally after closing, the new document becomes the active document? 

ActiveDocument.SaveAs tempRoot & "testReport.doc" 'And then save? 

Set objWord = Nothing 
Set objDoc = Nothing 

그러나, 나는 그것을 저장할 수 없습니다입니다. 문서가 열려 있지 않을 때 명령을 수행 할 수 없다는 오류가 표시됩니다.

누구나 의견을 제시 할 수 있다면 감사하겠습니다.

+1

분명히 objWord.ActiveDocument.SaveAs? – Fionnuala

+0

저는 도구이며, ActiveDocument가 objWord의 메서드가 될 것이라고 생각합니다 ... 다른 임의의 개체가 아닙니다. 감사합니다 – Mervyn

+2

아마 Word에서 코드를 복사했기 때문에 그렇게 가정했습니다. 단어에서 Application은 .ActiveDocument의 기본 부모입니다. 동일한 코드를 실행할 때 objWord 객체 변수는 부모이며 Word 응용 프로그램을 나타냅니다. 따라서 접두어가 붙지 않은 Word의 모든 명령은 코드를 Access로 변환 할 때 응용 프로그램 개체의 암시 적 가능성이 높기 때문에 Word 응용 프로그램 개체를 나타내는 개체 변수로 모든 것을 선행합니다. –

답변

1

변경된 ActiveDocument가 objWord.ActiveDocument로 변경되었습니다. 원하는 결과를 얻었습니다.

감사합니다.

+0

당신은 환영합니다 :) – Fionnuala

0

나는 이것을 막 보았습니다. 여기 내가하고있는 일이 있으며 잘 작동합니다. oDocument는 열려있는 대화 상자를 통해 사용자가 선택하는 병합 양식입니다. Excel 파일은 이전에 내 보낸 사용자 temp 폴더에 저장 한 쿼리입니다. Access 쿼리와 임시 테이블을 사용하여이 기법을 시도했지만 Excel을 사용하면 훨씬 문제가없는 것으로 나타났습니다.

Sleep 명령은 가져온 시스템 DLL 함수 (Public Declare Sub Sleep Lib "kernel32"(ByVal dwMS As Long))에서 가져온 것으로 병합을 실행할 Word 시간을 제공합니다. 실제로, 그것은 당신이 필요로하는 전부일지도 모른다. Office 2007을 사용 중입니다.

If Not oDocument Is Nothing Then 
        ' get merge source file 
       Set oFSO = New FileSystemObject 
       Set oFolder = oFSO.GetSpecialFolder(TemporaryFolder) 
       strTempFile = oFolder.Path & "\PTDMergeSource.xls" 

        ' run merge 
       With oDocument.MailMerge 
        .MainDocumentType = wdFormLetters 
        .Destination = wdSendToNewDocument 
        .OpenDataSource strTempFile, WdOpenFormat.wdOpenFormatDocument, False, False, False, False, , , , , , , "SELECT * FROM `tblMerge$`", , False, WdMergeSubType.wdMergeSubTypeAccess 
        .Execute True 
       End With 
       Sleep 2 
       oDocument.Close False 
      Else 
      MsgBox "Action was cancelled, or there was an error opening that document. Please try again, then try opening that document in Word. It may be someone else has locked that document (they are editing it). If the problem persists, email the document to the support contractor." 
      End If