任务计划程序未运行Excel VBA代码以将PDF作为电子邮件附件发送

任务计划程序未运行Excel VBA代码以将PDF作为电子邮件附件发送,vba,excel,outlook,taskscheduler,Vba,Excel,Outlook,Taskscheduler,以下是我正在使用的软件/系统: 微软办公软件2010; 任务调度器; Windows Server 2008 R2标准 我正在Excel文件中运行一些VBA代码,该文件执行以下操作: 1。通过SQL/ODBC连接从数据库检索数据 二,。使用now函数将数据上载到工作簿中的原始数据表,并在单元格中为工作簿添加时间戳 三,。刷新工作簿中的每个数据透视表并设置其格式 四,。将指定的图纸导出并保存为PDF文档,并使用步骤2中的时间戳保存文档名称 五,。保存工作簿 六,。电子邮件,即刚刚在Excel中创

以下是我正在使用的软件/系统:
微软办公软件2010;
任务调度器;
Windows Server 2008 R2标准

我正在Excel文件中运行一些VBA代码,该文件执行以下操作:


1。通过SQL/ODBC连接从数据库检索数据
二,。使用now函数将数据上载到工作簿中的原始数据表,并在单元格中为工作簿添加时间戳
三,。刷新工作簿中的每个数据透视表并设置其格式
四,。将指定的图纸导出并保存为PDF文档,并使用步骤2中的时间戳保存文档名称
五,。保存工作簿
六,。电子邮件,即刚刚在Excel中创建为电子邮件附件的特定PDF文档。
七,。关闭Excel应用程序

我在一个名为Workbook_Open的私有子系统中运行整个系列,该子系统检查当前时间是否与指定的运行时匹配。如果是这样,它将运行步骤1-7,如果是一小时后,它将关闭工作簿(这样我就可以处理它,而不是两小时的窗口)

下面是正在使用的代码: *注意,下面的代码在“ThisWorkbook”Excel对象中运行

'This Macro will use check to see if you opened the workbook at a certain time, if you did, then it will run the Report Automation Macros below.

Private Sub Workbook_Open()

HourRightNow = Hour(Now())

If HourRightNow = 13 Then

Call RefreshDataTables
Call RefreshPivotTables
Call SaveWorkbook
Call ExportToPDFFile
Call EmailPDFAsAttachment
Call CloseWorkbook

ElseIf HourRightNow = 14 Then

Call CloseWorkbook

End If

End Sub


Sub RefreshDataTables()
'
' RefreshDataTables Macro
' This Macro is used to refresh the data from the Dentrix Tables.
'
'This selects the table and refreshes it.

Sheets("raw").Select
Range("D4").Select
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
Worksheets("NomenclatureVBA").Range("A2").Formula = "=now()"

End Sub


Sub RefreshPivotTables()
'
' RefreshPivotTables Macro
' This Macro refreshes each Pivot Table in the document.
'

'This goes through each sheet and refreshes each pivot table.
    Sheets("D0150 VS D0330 BY BIZLINE").PivotTables("D0150 vs D0330 by BIZLINE").PivotCache.Refresh

   Columns("B:DD").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With


    Sheets("D0150 VS D0330").PivotTables("D0150 COMP EXAM vs D0330 PANO").PivotCache.Refresh

    Columns("B:DD").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
'Formnats to the specific date format below.






End Sub

'--------------------------------------------------------------------------------------------------------------

Sub SaveWorkbook()

' Saves Active (Open) Workbook

    ActiveWorkbook.Save

End Sub


'**********************READY************************
'More simplified and tested version of the Export To PDF format
'Make sure to update the filePaths, worksheets,

Sub ExportToPDFFile()
Dim strFilename As String


'Considering Sheet1 to be where you need to pick file name
strFilename = Worksheets("NomenclatureVBA").Range("C2")


Sheets(Array("D0150 VS D0330", "D0150 VS D0330 BY BIZLINE")).Select
Sheets("D0150 VS D0330").Activate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "\\****(ServerNameGoesHere)****\UserFolders\_Common\DentrixEntrpriseCustomReports\Public\Owner Reports\DataAnalystAutomatedReports\Reports\D0150 COMP EXAM vs D0330 PANO\" & strFilename & ".pdf" _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=False

Sheets("NomenclatureVBA").Select

'This is where the exporting ends, now we will proceed to email the file.
'-----------------------------------------------------------------------------

'The emailing begins here
'This says that if there is a file name stored in the strFileName variable, then....
End Sub



'This Macro Closes the workbook... Note that it closes the very specific workbook you choose.

Sub CloseWorkbook()

'Workbooks("Automated D0150 COMP EXAM vs D0330 PANO.xlsm").Close SaveChanges:=False
Application.DisplayAlerts = False
Application.Quit

End Sub
然后,我还有一个宏,它在VBA的模块部分向PDF文件发送电子邮件。看起来是这样的:

Sub EmailPDFAsAttachment()
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, Outlook 2010.
' This example sends the last saved version of the Activeworkbook object .
    Dim OutApp As Object
    Dim OutMail As Object
    Dim FilePath As String

    'This part is setting the strings and objects to be things. (e.g. FilePath is setting itself equal to the text where we plan to set up each report)

    FilePath = "\\***(ServerGoesHere)***\UserFolders\_Common\DentrixEntrpriseCustomReports\Public\Owner Reports\DataAnalystAutomatedReports\Reports\D0150 COMP EXAM vs D0330 PANO\" _
    & Worksheets("NomenclatureVBA").Range("C2") & ".pdf"

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
   ' Change the mail address and subject in the macro before you run it.
   '

    With OutMail
        .To = "email@example.com"
        .CC = ""
        .BCC = ""
        .Subject = Worksheets("NomenclatureVBA").Range("C2")

        .HTMLBody = "Hello all!" & "<br>" & _
        "Here is this week's report for the Comp Exam vs. Pano." & "<br>" & _
         "Let me know what you think or any comments or questions you have!" & "<br>" & _
         vbNewLine & Signature & .HTMLBody

        .Attachments.Add FilePath
        ' In place of the following statement, you can use ".Display" to
        ' display the mail.
        .Send
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub
Sub-EmailPDFAsAttachment()
'在Excel 2000、Excel 2002、Excel 2003、Excel 2007、Excel 2010、Outlook 2000、Outlook 2002、Outlook 2003、Outlook 2007、Outlook 2010中工作。
'此示例发送Activeworkbook对象的上次保存版本。
Dim OutApp作为对象
将邮件变暗为对象
将文件路径设置为字符串
'此部分将字符串和对象设置为对象。(例如,FilePath将自身设置为与我们计划设置每个报告的文本相同)
FilePath=“\\***(ServerGoeserre)***\UserFolders\\通用\DentrixEntrpriseCustomReports\Public\Owner Reports\dataanalysistatAutomatedreports\Reports\D0150 COMP-EXAM vs D0330 PANO\”_
&工作表(“命名法”).Range(“C2”)和.pdf
Set-OutApp=CreateObject(“Outlook.Application”)
Set-OutMail=OutApp.CreateItem(0)
出错时继续下一步
'在运行宏之前,请更改宏中的邮件地址和主题。
'
发邮件
.To=”email@example.com"
.CC=“”
.BCC=“”
.Subject=工作表(“术语表”)范围(“C2”)
.HTMLBody=“大家好!”&“
”和_ “这是本周Comp考试与Pano的对比报告。”&“
”&_ “让我知道你的想法或任何评论或问题!”&“
”&_ vbNewLine&签名&HTMLBody .Attachments.Add文件路径 '代替下面的语句,您可以使用“.Display”来 '显示邮件。 .发送 以 错误转到0 发送邮件=无 设置应用程序=无 端接头
因此,当我在13小时(下午1点)打开工作簿时,这一切都可以正常运行,但是,当我在13小时内尝试在任务计划程序中运行此程序时,它会一直运行到EmailPDFAsAttachment宏/sub,它挂起宏中的某个位置并停止运行

我还应说明,我在Outlook和Excel中都具有以下信任中心设置:

有人知道是什么原因导致宏在我亲自打开文件时运行良好,然后当我尝试通过任务调度器打开文件时,宏会在同一位置暂停? 有人知道如何通过任务调度器使其正确运行吗


谢谢

我们意识到服务器限制了我在任务计划程序中的权限。当我去的时候,我的IT主管将我的权限切换到Admin,它完美地运行了任务调度程序


抱歉误报了。。。我本来不会发布这个问题的,但我上周花了整整一周的时间来研究它。谢谢大家的关注

那是我的猜测。您必须确保密码输入正确。如果您不正确地输入了密码,任务计划程序将接受它,即使它不应该接受。在我看来,它应该提示用户并将错误通知他/她。也许微软会在不久的将来改变这一点。

试着在下一次错误恢复时注释掉
。也许你会知道发生了什么错误。