Project中ExportAsFixedFormat方法调用报错,寻求解决方案
使用VBA的ExportAsFixedFormat方法导出Project视图为PDF时出错
我在运行用于从完整项目拆分生成独立项目包的VBA子过程CreatePackages时遇到错误,核心问题是无法使用ExportAsFixedFormat方法将Job Packet的多个视图导出为PDF。报错信息如下:

我的代码如下:
Sub CreatePackages() Dim folderPath As String Dim matrix As Project Dim template As Project Dim templatePath As String Dim selectedMatrix As Variant On Error GoTo ErrorHandler 'Assign active project to matrix object Set matrix = Application.ActiveProject 'Check if a matrix file is selected If matrix Is Nothing Then MsgBox "Failed to open the Matrix file.", vbCritical Exit Sub End If 'Select the folder where the packages will be saved folderPath = SelectFolder() 'Activate the current project 'Check if a valid folder is selected If folderPath = "" Then MsgBox "No valid folder selected.", vbExclamation Exit Sub End If 'Loop through all the tasks in the matrix Dim ot As Task Dim otIdentifier As Integer Dim otNumber As String Dim packagePath As String Dim completePackagePath As String Dim packageName As String Dim planOfWorkName As String Dim task As Task Dim viewPTName As String Dim newPackage As Project Dim templateOpened As Boolean Dim waitTime As Date 'Get the complete path of the template file templatePath = folderPath & "\Template_PQ_2023.mpp" For Each ot In matrix.Tasks If ot.Summary And ot.OutlineLevel = 2 Then 'Open the template file templateOpened = Application.FileOpenEx(Name:=templatePath) 'Initialize the template Set template = Application.ActiveProject 'Check if the template opened successfully If template Is Nothing Then MsgBox "Failed to open the template file.", vbCritical matrix.Close Exit Sub End If 'Activate the matrix file matrix.Activate 'Select the task otIdentifier = ot.ID SelectRow Row:=otIdentifier, RowRelative:=False 'Copy the task to the matrix file Application.EditCopy 'Activate the template file template.Activate 'Select the first row in the file SelectRow Row:=1, RowRelative:=False 'Paste the task into the template Application.EditPaste 'Get the task number otNumber = ot.Text4 'Create a folder for the package packagePath = CreateFolder(otNumber, folderPath) 'Create the package name *.mpp packageName = "PQ-" & otNumber 'Create the complete package path for the *.mpp file completePackagePath = packagePath & Application.PathSeparator & packageName & ".mpp" 'Save the package with the name "PQ-otNumber" template.SaveAs Name:=completePackagePath Set newPackage = Application.ActiveProject 'Create the Plan of Work name planOfWorkName = packageName & "-PT" 'Create the complete name for the Plan of Work including the full path viewPTName = packagePath & Application.PathSeparator & planOfWorkName & ".pdf" Debug.Print (viewPTName) 'Export the Plan of Work view to PDF newPackage.ExportAsFixedFormat FileType:=pjPDF, FileName:=viewPTName, ArchiveFormat:=True 'Create the Personnel Required name personnelRequiredName = packageName & "-PR" 'Create the complete name for the Personnel Required including the full path viewPRName = packagePath & Application.PathSeparator & personnelRequiredName & ".pdf" Debug.Print (viewPRName) 'Switch to the Personnel Required view SendKeys "{F10}", True SendKeys "{o}", True SendKeys "{w}", True SendKeys "{DOWN}", True SendKeys "{ENTER}", True 'Export the Personnel Required view to PDF newPackage.ExportAsFixedFormat FileType:=pjPDF, FileName:=viewPRName, ArchiveFormat:=True Application.Quit 'Close the new file 'Application.FileCloseEx pjSave End If Next ot Exit Sub ErrorHandler: MsgBox "An error occurred: " & Err.Description, vbCritical Exit Sub End Sub
希望有人能帮助我解决这个问题。
内容的提问来源于stack exchange,提问作者Damian Brown
相关产品推荐
相关产品推荐

