19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

390 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

9. Click on the form to exit Excel.<br />

The values you created in Excel are now displayed in the OLE rectangle without<br />

any Excel embellishments. If needed, you can resize the OLE rectangle to<br />

show any hidden material.<br />

10. Run the program.<br />

11. Double-click on the OLE rectangle to reinvoke Excel.<br />

12. Change the value in one of the cells <strong>and</strong> then click on any other cell.<br />

Notice that the change is reflected in the bar chart.<br />

13. Click the End icon to end the program.<br />

Notice that the changes to the data <strong>and</strong> graph have been lost. They are permanently<br />

gone.<br />

14. Add a comm<strong>and</strong> button to the form <strong>and</strong> caption it Save Sheet.<br />

15. Double-click on the comm<strong>and</strong> button to open the Comm<strong>and</strong>1_Click event procedure<br />

code window.<br />

16. With Excel 97, enter the following program lines into this code window. (With<br />

Excel 95, delete “worksheet(“sheet1”)”.)<br />

Dim objExcel As Object<br />

Set objExcel = OLE1.Object<br />

objExcel.worksheet(“sheet1”).SaveAs “MySheet.xls”<br />

Set objExcel = Nothing<br />

The first two lines of this code make the contents of OLE1 into an OLE<br />

Automation object.<br />

17. Run the program, double-click on the OLE rectangle to invoke Excel, change<br />

the contents of one of the cells, <strong>and</strong> click on another cell.<br />

18. Click on the Save Sheet comm<strong>and</strong> button.<br />

19. Click the End icon to end the program.<br />

Again the changes to the data <strong>and</strong> graph have been lost. However, the saved file<br />

now can be used to recover the changes if you so desire. To do so, go to the<br />

SourceDoc property of OLE1, click on the ellipsis, type MYSHEET.XLS into<br />

the text box, click on the OK button, <strong>and</strong> click on Yes in the “Delete Current<br />

Embedded Object?” message box.<br />

■ A LINKING WALKTHROUGH USING WORD<br />

1. Before starting <strong>Visual</strong> <strong>Basic</strong>, invoke Word, type in a few sentences, <strong>and</strong> save the<br />

document. In this walkthrough we assume that the document you have created<br />

now resides on drive A <strong>and</strong> is named MYWORK.DOC.<br />

2. Invoke <strong>Visual</strong> <strong>Basic</strong>.<br />

3. Click the OLE icon in the Toolbox <strong>and</strong> use the single-click-draw technique to<br />

create a large rectangle on the form.<br />

4. Click on the “Create from File” option button, type A:\MYWORK.DOC into<br />

the text box, click the Link check box, <strong>and</strong> click on the OK button.<br />

The document saved in MYWORK. DOC is displayed in the OLE rectangle.<br />

5. Run the program <strong>and</strong> double-click the OLE rectangle. The complete Word program<br />

is invoked <strong>and</strong> the document in MYWORK.DOC is displayed.<br />

6. Make some changes to the document, <strong>and</strong> then press Alt/File/Save <strong>and</strong><br />

Alt/File/Exit to save your changes <strong>and</strong> exit Word. The modified document is<br />

displayed in the OLE rectangle on the <strong>Visual</strong> <strong>Basic</strong> form.<br />

7. End the program <strong>and</strong> display the form if it is hidden.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!