Lesson 5: Copy and Paste

Lesson Objectives

Upon completion of this lesson, the participant will be able to:

Some Topics to be introduced in this lesson include:

Copy, Paste, and Related Commands

This section discusses the use of copy/paste related macro commands to exchange data with other applications. Data can be exchanged in several basic formats - text, picture, and object - with options to link to the source. The available options depend on both the source and target applications.

OUTPUT

The following MSProject macro commands can be used to copy data from the active selection in the active project:

EditCopy

EditCopyPicture

EditCopy fromDate, toDate, includeTable, includeTimescale

Used to copy data from the active project.

The optional arguments are only used when copying data from the Resource Usage view. If the Resource Usage view is active and EditCopy is used without any arguments, then the Copy Resource Usage dialog is opened. When the fromDate and toDate arguments are specified, then it copies timescale data from the minimum number of periods that include the specified dates. If no dates are specified, then it defaults to roughly the periods visible on screen. If an application supports OLE Automation, it can also get Resource Usage data from MSProject by using MSProject's TimescaledData method and summing over all the tasks that a resource is assigned to.

Depending on the active selection in the active project, EditCopy can provide the data in the form of text, a picture, or an object, with the option to link to the source project. Here are some of the Paste Special formats that might be available in Word when data is copied from Project 4.0:

Unformatted text

Picture

Microsoft Project 4.0 Project Object

The rest of this lesson refers to the formats generically as text, picture, and object. Note that the object format without linking creates an embedded object, whereas a linked object is the same as a linked picture.

Here are some of the formats EditCopy can provide to other applications, depending on the application and the active selection in MSProject:

 

Text

Picture1

Object1

Paste Link

Task/Resource Sheet

X

X

X

X

Gantt Chart

X

X

X

X

Resource Usage

X

X

X

X2

Resource Graph  

X

X

X

Pert Chart  

X

X

X

Calendar view  

X

X

 
Task/Resource Form

Selected text in field

X

   

X

Task/Resource Form

Details Notes

X

   

X

Task/Resource Form

Details Objects

X

   

X

1 The picture and object formats are available in the target application only if the source project file is still open.

2 Resource Usage can include text from a table and/or timescale data - you can't paste link Resource Usage text into another application if the copy includes timescale data. Also, column titles and timescale labels are included only if Tools Options "Copy Resource Usage Header" is checked.

When text is copied, fields are tab separated, and rows are separated by newline strings (chr(13) & chr(10)). Text from a locked first column in a table cannot be copied.

NOTE: If you select a whole row in a table, the Edit menu changes Copy(Cell) to Copy(Task/Resource). This only makes a difference if you paste text back into Project, not if you paste text into another application. If you paste into Project, Copy(Tasks/Resource) copies all fields, even those not in the active table. You can also see the difference if you look in the clipboard at View, Display Text, for example, copying cells might show Display Text as "Copy 2Rx4C", but copying whole tasks might show as "2 tasks".

EditCopyPicture object, forPrinter

Used to copy the active selection in the active project as a picture or an object, but not as text. This works with all non-form task/resource views.

The arguments are optional.

object

If this is False, then only the picture format is provided to other applications. If True, then both the picture and object formats are provided. Paste Link is provided only if this argument is True. The default is False.

forPrinter

If True, then the copied image is formatted for the screen, otherwise for the printer. If not specified, then the Copy Picture dialog is opened.

The camera button on the default standard toolbar does the same thing as using the EditCopyPicture command with object:=False. It prompts for the forPrinter argument.

Here are some of the formats EditCopyPicture can provide depending on the object argument (see exceptions below the table):

object argument

Picture

Object

Paste Link

True

X

X

X

False

X

   

Exceptions:

Gantt Chart: If ForPrinter:=True, then the Object format is not provided.

Calendar view: No paste link format is provided.

Try This

The following MSProject pastes a picture of rows 3 and 4 of the active Gantt Chart into the start of the active Word document.

1. Open or create a test document in Word.

2. Leave Word open and switch to MSProject. Open/create a test project that has tasks in at least the first four rows.

3. Enter the following MSProject macro and run it. After it finishes, you should see that Gantt picture at the top of the Word document.

Sub copaste2()

Dim w As Object

ViewApply "Gantt chart"

SelectRow Row:=3, Height:=1, RowRelative:=False

EditCopyPicture

Set w = CreateObject("Word.Basic")

w.StartOfDocument

w.EditPaste

AppActivate "Microsoft Word"

End Sub

INPUT

The following MSProject macro commands can be used to paste data into the active project:

EditPasteSpecial

EditPaste

DDEPasteLink

EditPasteSpecial link, type, displayAsIcon

Note: Online Help only shows the Link argument, but Object Browser shows all three arguments.

The arguments are optional.

link

If True, then creates an OLE paste link.

type

Determines the paste format. Possible values are:

0 Object

1 Picture

2 Text

displayAsIcon

Determines whether an object should show as an icon. Use this argument only when pasting an object.

Whether or not the EditPasteSpecial command opens the Paste Special dialog depends on what arguments are included and their values:

These always bring up the Paste Special dialog:

EditPasteSpecial Link:=False, Type:=0, DisplayAsIcon:=True

EditPasteSpecial Link:=False, Type:=0

EditPasteSpecial Link:=False, Type:=1

These don't bring up the Paste Special dialog:

EditPasteSpecial Link:=False, Type:=0, DisplayAsIcon:=False

EditPasteSpecial Link:=False, Type:=1, DisplayAsIcon:=False

EditPasteSpecial Link:=False, Type:=2

Here are some of the formats available to EditPasteSpecial when a range of cells is copied from Excel, depending on the active selection in the active project:

 

Text

Picture

Object

Paste Link

Task/Resource Sheet

X

   

X

Gantt Chart

X

X

X

X

Text Box on Gantt Chart

X*

     
Resource Usage

X

   

X

Resource Graph        
Pert Chart        
Calendar view        
Task/Resource Form

Selected text in field

X*

     
Task/Resource Form

Details Notes

X*

     
Task/Resource Form

Details Objects

 

X

X

X

* Text Box on Gantt and selected text and notes in task/resource forms can use EditPaste to paste text, but can't use EditPasteSpecial at all.

Note: The following MSProject macro command can be used to activate the source, if an OLE object is selected in MSProject (it's like double clicking on the currently selected object):

ObjectVerb verb:=0

Try This

This macro selects and copies the range A1:B2 from the active Excel worksheet and pastes it as a picture onto the Gantt Chart of the active project.

1. Create a test worksheet in Excel with something in the range A1:B2.

2. Leave Excel open. Switch to MSProject and create a test project.

3. Enter and run the following MSProject macro. When it's done you should see a picture of the Excel cells on the Gantt chart.

Sub copaste3()

Dim x As Object

Set x = GetObject(, "Excel.Application")

x.Range("A1:B2").Select

x.Selection.Copy

ViewApply Name:="Gantt Chart"

EditPasteSpecial Link:=False, Type:=1, DisplayAsIcon:=False

End Sub

Is there any way to have the MSProject macro resize the picture?

EditPaste

This never paste links. Whether it pastes as text, picture, or object depends on the active selection in MSProject and what was copied by the source application.

DDEPasteLink

Used to paste link data into MSProject using the older DDE paste link format, instead of the newer OLE 2.0 format. For OLE 2.0 compliant applications, OLE links are used by default when paste link is selected. The DDE links may perform faster and be less stressful on resources than OLE links, however, they may lack some of the newer OLE functionality. For example, when you double click a cell in an MSProject task table that contains an OLE link, the source application will be activated, whereas, if it contains a DDE link, then the task information dialog opens.

Try This

The following MSProject macro copies the contents of the active cell in Excel and DDEPastelinks it into the start field of the task in row 2.

1. Select a cell in an Excel worksheet that contains something.

2. Switch to MSProject and enter the following MSProject macro.

Sub copaste3()

Dim x As Object

Set x = GetObject(, "Excel.Application")

x.ActiveCell.Copy

ViewApply "Task Sheet"

SelectTaskField Row:=2, Column:="Start", RowRelative:=False

DDEPasteLink

End Sub

3. Test the macro in the following cases:

If there is a task with id=2 and the table is sorted on id.

If there is no task in row 2 before running the macro.

If there is a task in row 2, but no task with id=2 because of the of the way the table is sorted.

Lesson 5 Exercises

  1. Select a cell in the Calendar view in MSProject and choose Edit Copy. Switch to Word and see what formats are available in its Edit Paste Special dialog. Is Paste Link available?


  2. Write an MSProject macro that uses OLE Automation to copy the range A1:D3 from the active Excel worksheet. The macro then pastes it into the object area of the Task Form formatted for Objects, for task 3. Assume there are at least 3 tasks in the active project, and that there is a workbook open in Excel.


  3. There is a "Try This" example in this lesson that shows how an MSProject macro can use OLE Automation to copy an Excel range. To find out how to paste it correctly into MSProject, create a test project with at least 3 tasks and then record an MSProject macro that applies the Task Form view, uses the Edit Goto menu command to go to task 3, formats the Details for Objects, and then uses the Edit Paste menu command.

    To test the completed MSProject macro, create a test Excel worksheet with some test data in A1:D3, then switch back to MSProject and run the macro. You should see the Excel range in the Object area of the Task Form for task 3.

  4. Write an MSProject macro that copies the Gantt chart of the active project and then uses OLE Automation to paste it into the active Word document as an object. The Word command, EditPaste, uses the object format by default to paste the Gantt as an embedded object. Assume that there is at least one task in the active project, and that there is a document open in Word.


  5. Walk through this manually first. Does the camera tool or Edit Copy in MSProject allow you to paste as an object into Word? Does it matter what tasks are selected in the Gantt chart? What happens if you select a blank task and choose Edit Copy and then do Edit Paste in Word?

    Test run the MSProject macro and switch to Word. You should see the embedded Gantt object.