EXCEL VBA:在打印到PDF文件之前,循环正在工作,但没有刷新 [英] EXCEL VBA : Loop is working but not refreshing before printing to PDF file

查看:109
本文介绍了EXCEL VBA:在打印到PDF文件之前,循环正在工作,但没有刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家早上好!

我的VBA代码存在问题.实际上,我实际上只想创建一个循环,以打印基于相同背景模板(位于名为AFFIDAVIT CREATOR的工作表中)的PDF,从而替换输入"工作表中的约4个框(标签和图像).

I have an issue concerning my VBA Code. I actualy just want to create a loop that prints PDFs that are based on the same background template (which is in the sheet called AFFIDAVIT CREATOR) replacing some 4 boxes (labels and images) from the INPUT Sheet.

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

So far, the loop is working properly. Only problem: It generates the PDF files according to the given name (variable r) but refreshes the sheet AFTER exporting to PDF. Result: Mutliple files with different names but they all display the same :(

有什么想法吗?

那是我的代码:

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

推荐答案

根据注释,如果Stop以您需要的方式工作. 3秒钟让您的图片加载完毕,您可以尝试使用如下所示的wait workaround:

According to comments, if Stop worked in the way that you need approx. 3 sec to get your picture loaded you could try to use a wait workaround which looks as follow:

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

.ExportAsFixedFormat之前添加代码.在上面,我将等待时间设置为5秒.您可以在进行一些测试后进行更改.

Add the code just before .ExportAsFixedFormat. Above I set waiting time to 5 sec. which you could change after some tests.

这篇关于EXCEL VBA:在打印到PDF文件之前,循环正在工作,但没有刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆