OFF: Using CreateObject with Microsoft Excel and Microsoft Project (163099)



The information in this article applies to:

  • Microsoft Project for Windows 4.0
  • Microsoft Project for Windows 95 4.1
  • Microsoft Project for Windows 95 4.1a
  • Microsoft Project for the Macintosh 4.0
  • Microsoft Excel for Windows 5.0
  • Microsoft Excel for Windows 5.0a
  • Microsoft Excel for Windows 5.0c
  • Microsoft Excel for Windows 95
  • Microsoft Excel for Windows 95 7.0a
  • Microsoft Excel for the Macintosh 5.0
  • Microsoft Excel for the Macintosh 5.0a

This article was previously published under Q163099

SUMMARY

This article provides example code using the Visual Basic CreateObject command to transfer information between Microsoft Excel and Microsoft Project.

MORE INFORMATION

Microsoft provides examples of Visual Basic procedures for illustration only, without warranty either expressed Or implied including but not limited to the implied warranties of merchantability And / Or fitness For a particular purpose. This Visual Basic procedure Is provided 'as is' and Microsoft does not guarantee that it can be used in all situations. Microsoft does not support modifications of this procedure to suit customer requirements For a particular purpose. Note that a Line that Is preceded by an apostrophe introduces a comment in the code - - comments are provided To explain what the code Is doing at a particular point in the procedure. Note also that an underscore character(_) indicates that code continues from one Line To the Next . You can type lines that contain this character As one logical Line Or you can divide the lines of code And include the Line - continuation character.

To create macros in Microsoft Project, use the following steps:

  1. On the Tools menu, click Macros.
  2. Click New.
  3. In the Name box, type a one word descriptive name for the macro, and then click OK.

Using Microsoft Project to Pull Information from Microsoft Excel

The following macro creates an OLE object which represents the Microsoft Excel environment and allows Microsoft Project to control and retrieve information from Microsoft Excel.

   Sub From_Excel_To_Project()

      'Sets the object variable to contain a Microsoft Excel Object.
      Dim xlObj As Object
      'Creates a string variable to temporarily store information.
      Dim strVar As String
      'Clears the xlObj object.
      Set xlObj = Nothing
      'Sets the xlObj object as a Microsoft Excel Application object.
      Set xlObj = CreateObject("Excel.Application")
      'Opens the file called Book1.xls.
      xlObj.Workbooks.Open FileName:="c:\book1.xls"
      'The variable strVar holds the contents of cell A1 on Sheet1 in
      'the Book1.xls file.
      strVar = xlObj.ActiveWorkbook.Sheets("Sheet1").Range("A1").Value
      'Places the contents of strVar in the Task Name field of the
      'first task in the open project file.
      ActiveProject.Tasks(1).Name = strVar

   End Sub
				

Using Microsoft Project to Copy Information from Microsoft Excel

The following macro copies the contents of cell A1 on Sheet1 of the file called C:\Book1.xls and then pastes the contents from the clipboard into the Task Name field of the first task in a Microsoft Project file.
   Sub Copy_From_Excel_To_Project()

      'Sets the Object variable to contain Microsoft Excel Object.
      Dim xlObj As Object
      'Clears the xlObj object
      Set xlObj = Nothing
      'Sets the xlObj object as a Microsoft Excel Application object.
      Set xlObj = CreateObject("Excel.Application")
      'Opens the file Book1.xls.
      xlObj.Workbooks.Open FileName:="c:\book1.xls"
      'Selects cell A1 on Sheet1. Note that you can specify a range, such
      'as .range("A1:D100").
      xlObj.activeworkbook.Sheets("Sheet1").range("A1").Select
      'Copies the contents of the selected cell to the Clipboard.
      xlObj.Selection.Copy
      'Selects the first row in Microsoft Project.
      SelectBeginning
      'Selects the name field for Task1.
      SelectTaskField Row:=0, Column:="Name"
      'Pastes the contents from the clipboard.
      EditPaste
      xlObj.CutCopyMode = False

   End Sub
				

Modification Type:MinorLast Reviewed:9/13/2006
Keywords:kbbug kbdtacode kbpending KB163099