title: VBA Copy Paste Data into Excel from Project
body: <p>I'm running the code below and getting spurious results. </p>
  
  <p>For some reason it copies five lines of code into the desired worksheet instead of the specified MS Project data. </p>
  
  <p>Can Anyone help out a newbie?</p>
  
  <p>Five lines of code incorrectly copied into Excel worksheet:</p>
  
  <pre><code>'Open MS Project file
  projApp.Application.FileOpenEx "C:File.mpp"
  
  Set projApp = projApp.ActiveProject
  
  'Final set up of code
  Set projApp = Nothing
  </code></pre>
  
  <p><img src="https://imgur.com/gallery/POkTc" alt="Error Image"></p>
  
  <pre><code>Sub OpenProjectCopyPasteData()
  
  Dim appProj As MSProject.Application
  Dim aProg As MSProject.Project
  Dim sel As MSProject.Selection
  Dim ts As Tasks
  Dim t As Task
  Dim rng As Range
  Dim ws As Worksheet
  
  Application.DisplayAlerts = False
  
  'Clear current contents
  
  Set ws = Worksheets("Project Data")
  Set rng = ws.Range("A:J")
  rng.ClearContents
  
  On Error Resume Next
  Set appProj = GetObject(, "MSProject.Application")
  If appProj Is Nothing Then
      Set appProj = New MSProject.Application
  End If
  appProj.Visible = True
  
  'Open MS Project file
  projApp.Application.FileOpenEx "C:File.mpp"
  Set projApp = projApp.ActiveProject
  
  'Final set up of code
  Set projApp = Nothing
  
  appProj.Visible = True
  
  WindowActivate WindowName:=aProg
  
  'Copy the project columns and paste into Excel
  Set ts = aProg.Tasks
  
  SelectTaskColumn Column:="Task Name"
  OutlineShowAllTasks
  OutlineShowAllTasks
  EditCopy
  Set ws = Worksheets("Project Data")
  Set rng = ws.Range("A:A")
  ActiveSheet.Paste Destination:=rng
  
  SelectTaskColumn Column:="Task Name"
  EditCopy
  Set rng = ws.Range("B:B")
  ActiveSheet.Paste Destination:=rng
  
  SelectTaskColumn Column:="Resource Names"
  EditCopy
  Set rng = ws.Range("C:C")
  ActiveSheet.Paste Destination:=rng
  
  SelectTaskColumn Column:="Finish"
  EditCopy
  Set rng = ws.Range("D:D")
  ActiveSheet.Paste Destination:=rng
  
  Application.DisplayAlerts = True
  appProj.DisplayAlerts = True
  
  End Sub
  </code></pre>