MS Project VBA:循环所有打开的.MPP,打印PDF

时间:2018-08-13 17:17:43

标签: vba loops pdf ms-project

目标:VBA宏将遍历所有打开的项目,应用3种不同的视图,并将它们PDF格式。当前,下面的代码应用了正确的视图(过滤器等),并另存为,但是我必须单击确定两次以确认位置并覆盖现有位置。其次,我考虑过使用Set SecondProject = ActiveProject,但是团队中只有三个人,而且我们并不拥有相同数量的项目。 MS Project似乎与众不同,以至于我为其他应用程序找到的示例不起作用。为了简化起见,我将在下面提供其中一种报告视图,而不是全部3种。

主要问题: 1)如何遍历所有打开的项目 2)如何使其跳过“另存为”中的确定步骤(还有另一种打印到PDF的方法)

当前代码如下:

Dim FirstProject As Project
Dim SecondProject As Project

Dim targetFolder As String

targetFolder =“ C:\ Users \ 522842 \ Desktop \ Community Care Transformation \ 1。可交付成果”

Set FirstProject = ActiveProject

ViewApply Name:="VA Status"
FilterApply Name:="&All Tasks"
FilterApply Name:="Active Tasks"
FilePrint FromPage:=1
FileSaveAs "C:[path here].pdf"

2 个答案:

答案 0 :(得分:0)

示例遍历所有打开的项目并导出为PDF格式,保留项目名称并附加“ .PDF”扩展名:

Option Explicit
Sub test()
    Dim P As Project
    Dim ProjectFullName As String
    Dim PDFname As String
    Dim DotPos As Integer
    For Each P In Projects
        P.Activate
        'Add code here to apply views and filters as required
        ProjectFullName = P.FullName
        DotPos = InStr(ProjectFullName, ".")
        If DotPos > 0 Then ProjectFullName = Left(ProjectFullName, DotPos - 1)
        PDFname = ProjectFullName & ".PDF"
        DocumentExport FileName:=PDFname
    Next P
End Sub   

答案 1 :(得分:0)

我们使它起作用(请注意对用户特定字段的引用,需要对其进行修改。

Sub PrintThisFile()

Dim Names As String

Names = ActiveProject.Name                                                                                                                                                 'Read in file name
Names = Replace(Names, ".mpp", "")                                                                                                                                                                      'Get rid of .mpp extension
FilePageSetupPage PaperSize:=pjPaperTabloid
ViewApply Name:="VA Status"                                                                                                                                                                             'Set VA Status View
OutlineShowAllTasks
FilterApply Name:="&All Tasks"                                                                                                                                                                          'Clears existing filter
FilterApply Name:="Active Tasks"                                                                                                                                                                        'Set the Active Filter to prepare the full schedule for printing
DocumentExport FileName:="C:\Users\USERNAME\Desktop\Community Care Transformation\1. Deliverables\" & Names & ".pdf"                                             'Saves the document as a pdf *MUST CHANGE DESTINATION FOLDER ACCORDINGLY
FilterApply Name:="VA 2 Week"                                                                                                                                                                           'Apply the 2 week look ahead view
DocumentExport FileName:="C:\Users\USERNAME\Desktop\Community Care Transformation\1. Deliverables\" & Names & "_Two Week Look Ahead.pdf"       'Saves the 2 week look ahead document as a pdf *MUST CHANGE DESTINATION FOLDER ACCORDINGLY
FilterApply Name:="VA Only Status Due"                                                                                                                                                      'Apply the Only Status Due view
DocumentExport FileName:="C:\Users\USERNAME\Desktop\Community Care Transformation\1. Deliverables\" & Names & "_Due Next Week.pdf"                   'Saves the document as a pdf
ViewApply Name:="VA Status"                                                                                                                                                                             'Reset to VA Status View
FilterApply Name:="&All Tasks"
FilterApply Name:="Active Tasks"
PaneClose           


MsgBox ("Documents have been saved")

End Sub