1

I'm having problem opening project file from Project Online (server). I want to save all online files locally. If I open project first through Desktop client and try to open that file using function FileOpenEx it works (maybe because it opens draft?) Also I've tried full URL

 $Project.FileOpenEx(".../pwa/_api/projectdata/Projects/$name",$true)

But it started import wizard This is the code:

$csv = Import-Csv '...\ProjectNames.csv'
$Project = New-Object -ComObject msproject.application 
$csv | ForEach-Object {

$name=$_.ProjectName
Write-Output $name

    $Project.FileOpenEx("<>\$name",$true)

    $Save=$Project.FileSaveAs("...\Desktop\ProjectData\$name.mpp")

    Get-Process | where{$_.ProcessName -like "*winproj*"} | Stop-Process
}

I get this error when I try to open Project Online projects.

ForEach-Object : Exception calling "FileOpenEx" with "2" argument(s): "The remote procedure call failed. (Exception from HRESULT: 0x800706BE)"
    At ...\MigrateProjects.ps1:26 char:8
    + $csv | ForEach-Object {
    +        ~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [ForEach-Object], MethodInvocationException
        + FullyQualifiedErrorId : COMException,Microsoft.PowerShell.Commands.ForEachObjectCommand

EDIT: I've changed code to use shell like pointed in comments and it opens now. But when I try to save file sometimes it doesn't return boolean value and just stops. It happens on different projects, but it did save all my listed projects once. But when I want to repeat saving process it just exits at FileSaveAs. And I can't figure out why.

$csv = Import-Csv '...\ProjectData\ProjectNames.csv'

$ProjServer=".../sites/pwa/"

$ProjShell = new-object -comobject wscript.shell 
$ProjShell.Run("winproj /s $ProjServer", 1,$false)

Start-Sleep -s 15
$Project =[System.Runtime.InteropServices.Marshal]::GetActiveObject("msproject.application")

Try{
$csv | ForEach-Object {
    
    $name=$_.ProjectName
    Write-Output $name
    $NameWithoutSpaces= $name -replace '\s','_'

    $Project.FileOpenEx("<>\$name",$true)
    
   $Save= $Project.FileSaveAs("...\ProjectData\$NameWithoutSpaces.mpp")
   Write-Output $Save
}
}
catch{
    Get-Process | where{$_.ProcessName -like "*winproj*"} | Stop-Process
    $error[0].Exception.GetBaseException().LoaderExceptions
    }


Get-Process | where{$_.ProcessName -like "*winproj*"} | Stop-Process
  • In order to automate MS Project and have it open to a project server, you need to launch winproj.exe using a command-line switch. See [Connect to a project server when opening MS Project](https://stackoverflow.com/questions/24121106/connect-to-a-project-server-when-opening-ms-project/24540820) for how this is done with VBScript; modify as necessary to work with PowerShell. – Rachel Hettinger Oct 12 '20 at 20:02
  • @RachelHettinger Thanks it worked. But now I have another problem with saving. I've edited my question. the error is: Could not get dispatch ID for FileSaveAs (error: 0x80010001)). – Saulius Juškevičius Oct 13 '20 at 10:02
  • Before repeating the save as process, consider deleting the file before trying to save again to the path and name of an existing file. – Rachel Hettinger Oct 13 '20 at 19:06
  • Before every try I delete files manually. So that's not the problem. What worked is putting Start-Sleep before FileSaveAs because I think that it doesn't open fully before starting to save. Maybe there is more correct way to do this? – Saulius Juškevičius Oct 14 '20 at 04:12
  • Yes, it is quite possible that the files are not fully opened before the save as command runs so a pause (sleep) is a good way to handle this. – Rachel Hettinger Oct 14 '20 at 15:37

0 Answers0