EXCEL VBA:循环正在工作,但在打印到PDF文件之前没有刷新

时间:2013-07-04 09:18:40

标签: excel vba loops pdf-generation refresh

大家早上好!

我的VBA代码存在问题。我实际上只是想创建一个循环来打印基于相同背景模板的PDF(在名为AFFIDAVIT CREATOR的工作表中),从INPUT Sheet中替换一些4个框(标签和图像)。

到目前为止,循环工作正常。唯一的问题: 它根据给定的名称(变量r)生成PDF文件,但在导出为PDF后刷新工作表。结果:具有不同名称的多个文件但它们都显示相同:(

有什么想法吗?

这是我的代码:

Private Sub TryMe()
Dim r As Long
Dim strCap As String
Dim strCap2 As String
r = 4

    Do Until Sheets("INPUT").Cells(r, 3).Value = ""

    strCap = Sheets("INPUT").Cells(r, 3).Value
    Sheets("AFFIDAVIT CREATOR").Label1.Caption = strCap

strCap2 = Sheets("INPUT").Cells(r, 5).Value
Sheets("AFFIDAVIT CREATOR").Label2.Caption = strCap2

If Sheets("INPUT").Cells(r, 4) = "OE" Then
    Sheets("AFFIDAVIT CREATOR").Image1.Picture = LoadPicture(ActiveWorkbook.Path & "\OE_Logo.jpg")
Else
    Sheets("AFFIDAVIT CREATOR").Image1.Picture = LoadPicture(ActiveWorkbook.Path & "\SF_Logo.jpg")
End If

If Sheets("INPUT").Cells(r, 6) = "OE" Then
    Sheets("AFFIDAVIT CREATOR").Image2.Picture = LoadPicture(ActiveWorkbook.Path & "\OE_Logo.jpg")
Else
    Sheets("AFFIDAVIT CREATOR").Image2.Picture = LoadPicture(ActiveWorkbook.Path & "\SF_Logo.jpg")
End If


ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, From:=1, To:=1, FileName:=ThisWorkbook.Path & "\" & Sheets("INPUT").Cells(r, 3) & ".pdf" _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=False

Sheets("AFFIDAVIT CREATOR").Calculate

r = r + 1

Loop

End Sub

1 个答案:

答案 0 :(得分:4)

根据评论,如果Stop以您需要的方式工作。为了让您的照片加载3秒,您可以尝试使用wait workaround,如下所示:

Dim Start As Single
Start = Timer
'wait 5 sec...
Do While Start + 5 > Timer
    DoEvents
Loop

.ExportAsFixedFormat之前添加代码。我将等待时间设置为5秒。你可以在一些测试后改变它。