Microsoft KB Archive/163099

= OFF: Using CreateObject with Microsoft Excel and Microsoft Project =

Article ID: 163099

Article Last Modified on 9/13/2006

-

APPLIES TO


 * Microsoft Project 4.0 Standard Edition
 * Microsoft Project 4.1 Standard Edition
 * Microsoft Project 4.1a
 * Microsoft Project 4.0 for Macintosh
 * Microsoft Excel 5.0 Standard Edition
 * Microsoft Excel 5.0a
 * Microsoft Excel 5.0c
 * Microsoft Excel 95 Standard Edition
 * Microsoft Excel 95a
 * Microsoft Excel 5.0 for Macintosh
 * Microsoft Excel 5.0a for Macintosh

-



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

Additional query words: createobject create object vb vba

Keywords: kbbug kbdtacode kbpending KB163099

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.