VBA Excel - Sub正在打印旧数据

时间:2016-08-25 16:50:15

标签: excel vba excel-vba

我在尝试打印Excel工作表时遇到问题。我有一个调用另外两个潜艇的潜艇,见下文:

    Sub AutoGen_NOW()

    Application.Calculation = xlCalculationAutomatic

    autoGen_refresh

    autoGen_invoiceStageGen

    End Sub

调用的潜艇是

 1. autoGen_refresh
 2. autoGen_invoiceStageGen

第一个子命令刷新工作簿连接,而第二个子命令打印引用连接的工作表。我的问题是,即使打印子发生在刷新子后,打印子打印工作表,因为它在刷新之前出现,而不是新数据。当我手动查看工作表时,在打印后,它包含新数据。

非常感谢任何帮助。我在下面添加了两个潜艇:

Sub autoGen_refresh()

Dim LastAnnual As WorkbookConnection
Dim LastMonthly As WorkbookConnection
Dim oledbCn As OLEDBConnection

Set LastAnnual = ThisWorkbook.Connections("Staging_AnnualLastEntry")
Set LastMonthly = ThisWorkbook.Connections("Staging_MonthlyLastEntry")

'Refresh Annual Connection if Annual is selected - Refresh Monthly Connection if Monthly is selected
If MonthForm.OptionButton16.Value = True Then
    LastAnnual.Refresh
Else
    LastMonthly.Refresh
    MonthlyTemp
    MonthlyHide
End If

End Sub

Sub autoGen_invoiceStageGen()

Dim FolderPath As String
Dim sourceSheet As Worksheet    'This is the Worksheet where the data connection is
Dim i As Long


'Dim template worksheets
Dim annualRateWS    As Worksheet
Dim annualPSFWS     As Worksheet
Dim annualFlatWS    As Worksheet
Dim monthlyRateWS   As Worksheet

'Set template Worksheets
Set annualRateWS = Worksheets("00_ComRate_Template")
Set annualPSFWS = Worksheets("00_PSF_Template")
Set annualFlatWS = Worksheets("00_FlatAmount_Template")
Set monthlyRateWS = Worksheets("00_MONTHLYTEMPLATE")

'Set sourceSheet to Annual or Monthly table based on user input
Select Case MonthForm.OptionButton16.Value
    Case "True"
        Set sourceSheet = ThisWorkbook.Worksheets("Annual_InvoiceGen")
    Case "False"
        Set sourceSheet = ThisWorkbook.Worksheets("Monthly_InvoiceGen")
End Select



'Set path of directory all invoices are saved too
FolderPath = ("P:\Regis Profiles\00_MerchantDB\XX_TempDump\")




'Code used to stop screen flashing during macro operation -- This is set back to true after loop
Application.ScreenUpdating = False


'ANNUAL COMMISSION RATE - PDF GENERATION
If MonthForm.OptionButton16.Value = "True" And MonthForm.OptionButton7.Value = "True" Then
    annualRateWS.Visible = True
    annualRateWS.Activate
    ThisWorkbook.Connections("Staging_AnnualLastEntry").Refresh
    annualRateWS.Calculate
    Workbook_BeforePrint (False)
    'Application.Wait (Now + TimeValue("0:00:03"))

         For i = 32 To 43 'This Code Hides All Calculation Years that are out of range*****
                    ActiveSheet.Rows(i).Hidden = (ActiveSheet.Cells(i, 5).Value = 0)
         Next i



     annualRateWS.Activate
     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FolderPath & ThisWorkbook.Worksheets("REF_InvoiceGen").Range("$B$2").Value & Format(Now(), "yyyymmddhhmmss"), Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
             :=False, OpenAfterPublish:=True
         annualRateWS.Visible = False
 End If


'ANNUAL COMMISSION PSF - PDF GENERATION
If MonthForm.OptionButton16.Value = True And MonthForm.OptionButton8.Value = "True" Then
    annualPSFWS.Visible = True
    annualPSFWS.Activate
    ThisWorkbook.Connections("Staging_AnnualLastEntry").Refresh
    annualPSFWS.Calculate
    'Application.Wait (Now + TimeValue("0:00:03"))


    annualPSFWS.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FolderPath & ThisWorkbook.Worksheets("REF_InvoiceGen").Range("$B$2").Value & Format(Now(), "yyyymmddhhmmss"), Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
             :=False, OpenAfterPublish:=True
         annualPSFWS.Visible = False
End If


'ANNUAL FLAT AMOUNT = PDF GENERATION
If MonthForm.OptionButton16.Value = True And MonthForm.OptionButton9.Value = "True" Then
    annualFlatWS.Visible = True
    annualFlatWS.Activate
    ThisWorkbook.Connections("Staging_AnnualLastEntry").Refresh
    annualFlatWS.Calculate
    'Application.Wait (Now + TimeValue("0:00:03"))


    annualFlatWS.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FolderPath & ThisWorkbook.Worksheets("REF_InvoiceGen").Range("$B$2").Value & Format(Now(), "yyyymmddhhmmss"), Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
             :=False, OpenAfterPublish:=True
         annualFlatWS.Visible = False
End If

'MONTHLY COMMISSION RATE - PDF GENERATION
If MonthForm.OptionButton16.Value = "False" And MonthForm.OptionButton7.Value = "True" Then
    monthlyRateWS.Visible = True
    monthlyRateWS.Activate
    monthlyRateWS.Calculate



    MonthlyTemp
    MonthlyHide

    'Application.Wait (Now + TimeValue("0:00:03"))

    monthlyRateWS.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FolderPath & ThisWorkbook.Worksheets("REF_InvoiceGen").Range("$B$2").Value & Format(Now(), "yyyymmddhhmmss"), Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
             :=False, OpenAfterPublish:=True
         monthlyRateWS.Visible = False
End If


sourceSheet.Visible = False


'Code used to set screen updating BACK TO True
Application.ScreenUpdating = True


End Sub

1 个答案:

答案 0 :(得分:0)

蒂姆·威廉姆斯解决了这个问题。已在连接上启用后台刷新。禁用此功能后,一切正常。