在通过vba保存pdf之前更新表单域

问题描述 投票:2回答:1

我是VBA和编码的初学者,我遇到了我的VBA代码问题。这就是我想要做的事情:

我有两个可填写的字段(f_autpar_nom和f_autpar_fiche),我的Access数据库需要使用command_click()在两个formfield(eleves_nom和eleves_numfiche)上的Word文件中。然后,我的Word文档打开并提示我“你想保存这个”,然后Word文档保存为PDF并通过电子邮件发送。

一切都在工作,除了一件事:当我打印PDF并返回我设置的默认消息(这是“erreur”)时,表单字段不会更新。

我需要的是找到一种方法来更新formfield之前我的消息框提示我发送电子邮件。

这是我使用Access的代码

Function fillwordform()
    Dim appword As Word.Application
    Dim doc As Word.Document
    Dim Path As String

    On Error Resume Next
    Error.Clear
    Path = "P:\Commun\SECTEUR DU TRANSPORT SCOLAIRE\Harnais\Autorisations Parentales\Autorisation parentale vierge envoyée\Autorisation_blank.docm"
    Set appword = GetObject(, "word.application")

    If Err.Number <> 0 Then
        Set appword = New Word.Application
        appword.Visible = True
    End If
    Set doc = appword.Documents.Open(Path, , False)
    With doc
        .FormFields("eleves_nom").Result = Me.f_autpar_nom
        .FormFields("eleves_numfiche").Result = Me.f_autpar_fiche
        appword.Visible = True
        appword.Activate
    End With

    Set doc = Nothing
    Set appword = Nothing    
End Function

Private Sub Commande47_Click()
    Dim mydoc As String
    mydoc = "P:\Commun\SECTEUR DU TRANSPORT SCOLAIRE\Harnais\Autorisations Parentales\Autorisation_blank.docm"
    Call fillwordform
End Sub

和Word

Private Sub document_open()
    Dim outl As Object
    Dim Mail As Object
    Dim Msg, Style, Title, Help, Ctxt, Response, MyString
    Dim PDFname As String

    Msg = "L'autorisation sera sauvegardée et envoyée par email.  Continuer?"
    Style = vbOKCancel + vbQuestion + vbDefaultButton2
    Title = "Document"
    Ctxt = 1000
    Response = MsgBox(Msg, Style, Title, Help, Ctxt)
    If Response = vbOK Then
        ActiveDocument.Save
        PDFname = ActiveDocument.Path & "\" & "Autorisation Parentale " & FormFields("eleves_nom").Result & ".pdf"
       ActiveDocument.SaveAs2 FileName:=PDFname, FileFormat:=wdFormatPDF
        Set outl = CreateObject("Outlook.Application")
        Set Mail = outl.CreateItem(0)
        Mail.Subject = "Autorisation parentale " & FormFields("eleves_nom").Result & " " & FormFields("eleves_numfiche")
        Mail.To = ""
        Mail.Attachments.Add PDFname
        Mail.Display
        Application.Quit SaveChanges:=wdDoNotSaveChanges

    Else
        MsgBox "Le fichier ne sera pas envoyé."
        Cancel = True
    End If
End Sub
vba ms-access ms-word access-vba word-vba
1个回答
0
投票

我不是故意要删除Set Doc = Nothing。我的目的是要指出,在该命令之前所做的任何改变都必须丢失,因为它们没有被保存。在下面的代码中,文档已关闭并保存。

Private Sub Commande47_Click()
    Dim mydoc As String
    mydoc = "P:\Commun\SECTEUR DU TRANSPORT SCOLAIRE\Harnais\Autorisations Parentales\Autorisation_blank.docm"
    Call FillWordForm
End Sub

Function FillWordForm(Ffn As String)

    Dim appWord As Word.Application
    Dim Doc As Word.Document

    On Error Resume Next
    Set appWord = GetObject(, "word.application")
    If Err.Number Then Set appWord = New Word.Application
    appWord.Visible = True
    On Error GoTo 0

    Set Doc = appWord.Documents.Open(Ffn, , False)
    ' the newly opened document is activated by default
    With Doc
        .FormFields("eleves_nom").Result = Me.f_autpar_nom
        .FormFields("eleves_numfiche").Result = Me.f_autpar_fiche
        .Close True             ' close the file and save the changes made
    End With

    Set appWord = Nothing
End Function

但是,我也同意@Kazimierz Jawor你的构造是不幸的。基本上,当您从Access打开文档时,应该运行文档的On_Open过程。因此,您甚至可以在设置表单字段之前发送电子邮件。我保存更改的建议可能仅在您第二次运行代码时生效。

更好的方法应该是从Access或Word发送邮件。我的偏好是后者。使用Word从Access表中提取两个值应该很容易,将它们添加到Word文档并邮寄出整个文件。但是,我没有看到为什么你应该使用Open事件来做到这一点。如果这个选择更合乎逻辑,那么从Access中做所有事情就是结论。

© www.soinside.com 2019 - 2024. All rights reserved.