【问题标题】:Updating formfield before saving a pdf through vba在通过 vba 保存 pdf 之前更新表单域
【发布时间】:2017-12-21 20:02:07
【问题描述】:

我是 VBA 和一般编码的初学者,但我的 VBA 代码存在问题。这就是我想做的事情:

我的 Access 数据库中有两个可填充字段(f_autpar_nom 和 f_autpar_fiche),它们需要在我的 Word 文件中的两个表单字段(eleves_nom 和 eleves_numfiche)中使用 command_click()。然后,我的 Word 文档打开并提示我“是否要保存此文件”,然后 Word 文档另存为 PDF 并通过电子邮件发送。

除了一件事之外,一切都在工作:当我打印 PDF 并返回我设置的默认消息(即“erreur”)时,表单域没有更新。

我需要在消息框提示我发送电子邮件之前找到更新表单域的方法。

这是我使用 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

【问题讨论】:

  • 您是否考虑将两个子例程合并为一个(而)应该从 Access 触发的子例程?在我看来,它将解决这个问题。因此,没有 Document_Open event 操作,而 Access 中只有一个宏
  • On Error Resume Next 命令清除所有现有的错误情况。因此,Err.Clear 后面不需要(或前面)。
  • 您的 Access 例程打开文档,在 doc 中填写表单域的内容,然后设置 Doc = NothingDoc 的保存版本永远不会改变。显然,如果您希望在保存的文档中进行更改,则必须保存它们。
  • @KazimierzJawor 我尝试合并两个宏,但无法通过具有访问权限的 vba 显示带有 word 的 msgbox。
  • @Variatus 我尝试将“doc = nothing”取出,但它仍然不起作用。

标签: ms-access vba ms-word


【解决方案1】:

我并不是要删除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 中做所有事情就是结论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多