Эта команда недоступна, так как ни один документ не открыт - PullRequest
0 голосов
/ 09 октября 2019

Я относительно новичок в mailmerge. Я пытался реализовать этот код для кнопки действия, чтобы реализовать слияние почты, но я продолжаю получать сообщение об ошибке, как указано в теме, с этой строкой With ActiveDocument.MailMerge, выделенной желтым

Я посмотрел на свой шаблон слова икажется, все в порядке.

Private Sub Command30_Click()
   'set variables
    Dim oWord As Word.Application
    Dim oWdoc As Word.Document
    Dim wdInputName As String
    Dim wdOutputName As String
    Dim outFileName As String

' Set Template Path
    '------------------------------------------------
    wdInputName = CurrentProject.Path & "X:\PM\PM-O\PM-OQ\PM-OQRA\PROCESSES\03_Pigments\98_ Database\05_Inventory\02_Bulk_letter\word_template.docx"

 ' Create unique save filename with minutes
    ' and seconds to prevent overwrite
    '------------------------------------------------
    outFileName = "Merck_InvStatements_" & Format(Now(), "yyyymmddmms")

 ' Output File Path w/outFileName
    '------------------------------------------------
    wdOutputName = CurrentProject.Path & "\desktop\completed\" & outFileName

     Set oWord = New Word.Application
    Set oWdoc = oWord.Documents.Open(wdInputName)

    ' Start mail merge
    '------------------------------------------------
    With oWord

    oWord.Visible = True
    With ActiveDocument.MailMerge
        .MainDocumentType = wdFormLetters
        .OpenDataSource _
            Name:=CurrentProject.FullName, _
            AddToRecentFiles:=False, _
            LinkToSource:=True, _
            Connection:="Q_SERIAL_INVENTORY_A02", _
            SQLStatement:="SELECT * FROM [Q_SERIAL_INVENTORY_A02]"
        .Destination = wdSendToNewDocument
        .Execute Pause:=False
    End With
   End With
 ' Hide Word During Merge
    '------------------------------------------------
    oWord.Visible = False

    ' Save file as PDF
    ' Uncomment the line below and comment out
    ' the line below "Save file as Word Document"
    '------------------------------------------------
    'oWord.ActiveDocument.SaveAs2 wdOutputName & ".pdf", 17

    ' Save file as Word Document
    '------------------------------------------------
    oWord.ActiveDocument.SaveAs2 wdOutputName & ".docx", 16

    ' Quit Word to Save Memory
    '------------------------------------------------
    oWord.Quit savechanges:=False

    ' Clean up memory
    '------------------------------------------------
    Set oWord = Nothing
    Set oWdoc = Nothing
     End Sub`
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...