刷新word文档



我在网上找到了一个VBA脚本,可以将多个Worddatasets保存为PDF格式的"Serienbrief"(我认为它在英语中被称为bulkletter(。word文档由各种Mergefields以及多个Excel条形图组成,这些条形图仍然连接到相应的Excel文件。

问题:

当我将datasets保存为pdf时,当dataset更改时,mergefields会更改。

然而,这些图总是具有第一个dataset的值。我可以在更改dataset后手动刷新Word文档,将图形更新为正确的值,但我需要自动化这个过程。

我要查找的内容:

我需要扩展下面的Script,它每次在保存新的pdf(基本上是cntrl+A,然后是F9(之前都会刷新Document(因此也会刷新条形图(。

我尝试过的:

我在这里发现了一个类似的问题Word VBA来刷新嵌入式Excel图表,但答案对我没有帮助,也是因为我不确定在脚本中的哪里实现它。

下面是我迄今为止使用的脚本,它运行得很好,除了缺少刷新部分。

感谢您的帮助!

Sub Serienbrief()
' set variables
Dim iBrief As Integer, sBrief As String
Dim AppShell As Object
Dim BrowseDir As Variant
Dim Path As String
' catch any errors
On Error GoTo ErrorHandling
' determine path
Set AppShell = CreateObject("Shell.Application")
Set BrowseDir = AppShell.BrowseForFolder(0, "Speicherort für Serienbriefe auswählen", 0, 16)
If BrowseDir = "Desktop" Then
Path = CreateObject("WScript.Shell").SpecialFolders("Desktop")
Else
Path = BrowseDir.items().Item().Path
End If
If Path = "" Then GoTo ErrorHandling
Path = Path & "Serienbrief-" & Format(Now, "dd.mm.yyyy-hh.mm.ss") & ""
MkDir Path
On Error GoTo ErrorHandling
' hide application for better performance
MsgBox "Serienbriefe werden exportiert. Dieser Vorganag kann einige Minuten dauern - Microsoft Word wird während dieser Zeit ausgeblendet", vbOKOnly + vbInformation
Application.Visible = False
' create bulkletter and export as pdf
With ActiveDocument.MailMerge
.DataSource.ActiveRecord = 1
Do
.Destination = wdSendToNewDocument
.SuppressBlankLines = True

With .DataSource
.FirstRecord = .ActiveRecord
.LastRecord = .ActiveRecord
sBrief = Path & .DataFields("IDSCHOOL").Value & ".pdf"
End With
.Execute Pause:=False
ThisDocument.InlineShapes(1).LinkFormat.BreakLink
If .DataSource.DataFields("IDSCHOOL").Value > "" Then
ActiveDocument.SaveAs FileName:=sBrief, FileFormat:=wdFormatPDF
End If
ActiveDocument.Close False
If .DataSource.ActiveRecord < .DataSource.RecordCount Then
.DataSource.ActiveRecord = wdNextRecord
Else
Exit Do
End If
Loop
End With
' error handling
ErrorHandling:
Application.Visible = True
If Err.Number = 76 Then
MsgBox "Der ausgewählte Speicherort ist ungültig", vbOKOnly + vbCritical
ElseIf Err.Number = 5852 Then
MsgBox "Das Dokument ist kein Serienbrief"
ElseIf Err.Number = 4198 Then
MsgBox "Der ausgewählte Speicherort ist ungültig", vbOKOnly + vbCritical
ElseIf Err.Number = 91 Then
MsgBox "Exportieren von Serienbriefen abgebrochen", vbOKOnly + vbExclamation
ElseIf Err.Number > 0 Then
MsgBox "Unbekannter Fehler: " & Err.Number & " - Bitte Makro erneut ausführen.", vbOKOnly + vbCritical
Else
MsgBox "Serienbriefe erfolgreich exportiert", vbOKOnly + vbInformation
End If
End Sub

更改:

ThisDocument.InlineShapes(1).LinkFormat.BreakLink

至:

ActiveDocument.InlineShapes(1).LinkFormat.Update
ActiveDocument.InlineShapes(1).LinkFormat.BreakLink

最新更新