MS 项目 VBA:循环全部打开.MPP,打印 PDF



目标: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"

循环遍历所有打开的项目并导出为 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   

我们让它工作(请注意对需要修改的用户特定字段的引用。

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:UsersUSERNAMEDesktopCommunity Care Transformation1. 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:UsersUSERNAMEDesktopCommunity Care Transformation1. 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:UsersUSERNAMEDesktopCommunity Care Transformation1. 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

最新更新