Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

BN1190 IT in Business

Tutorial – Programming

Introduction

Understanding how software is developed is important since most managers will eventually be involved in development projects of one kind or another.

This tutorial takes you through the process of creating various simple computer programs. As you work, you will also learn about some basic programming concepts.

Important: Much of what is covered here has been greatly simplified. Keep in mind that some of the methods shown may not be the “right” or best way to do things.

Task 1: Extending Software Packages (1)

Most major software applications contain a built-in programming language. One of the best-known languages is Visual Basic for Applications, or VBA for short. VBA features in all major Microsoft packages, including Word, Excel, Access, PowerPoint and others. It also features in many packages produced by other software companies.

In this first task, we are going to add two buttons to Word. One button will allow us to mark a place in a document with a bookmark. The second button will allow us to jump to that bookmark from wherever we are in the document. This ability is very handy when working with long documents because it makes it easy to return to a particular place if have to look something up in another part of the document.

Before we can create the buttons, we will need to produce two macros. Macros were originally created as a way of automating long or repetitive sets of keystrokes. They were created by recording a series of actions and then replaying them whenever needed. Instead of typing “Yours sincerely,” at the end of a letter, for instance, you might record all of the actions needed and store them so that pressing Ctrl-Y might cause the macro to run. Running the macro would make the word processor behave exactly as if the keys needed to type the phrase had been pressed. As time went on, macros have become more complex and more powerful until the distinction between a macro and a computer program has become blurred.

Before you can create a macro, you need to display some “hidden” options in Word. Note that the same procedure applies to other Microsoft Office 2010 packages, such as Excel.

1.1 Open Word and load any reasonably long document you have to hand. If the document is important, make a copy of it – never work directly on data you can’t afford to lose! If in doubt, just work on a copy of this tutorial handout since you can always download a fresh copy if you need to.

1.2 Click on the File tab. Now click on the Options item then choose Customize Ribbon in the dialog box that appears.



1.3 In the dialog, make sure that the Developer item is checked, then click on OK to return to Word. You should now see a new Developer item on the toolbar. This provides access to a variety of tools, including a macro recorder. As the name suggests, a macro recorder provides the ability to create, edit and run macros.

We are going to create two simple macros, then edit them to suit our needs.


1.4 Position the cursor anywhere in the Word document currently open. This is where we will insert a bookmark and will let us test the macros created.

1.5 Switch to the Developer toolbar and then click on the Record Macro button to display a simple dialog.



Make sure that the information in the Record Macro dialog looks like the illustration. The macro should be called AddBookmark and you should include a description of “Insert a bookmark at the current position in the document.”

Click on the OK button to begin recording.

1.5 The steps we want to include in the macro are very simple:

· Go to the Insert tab and click on the Bookmark item.

· Add a new bookmark called MyBookmark.

1.6 Now switch back to the Developer tab and click the Stop Recording button.


1.7 We need to create a second macro to jump to the bookmark we created. Record a new macro called GoToMyBookmark and with a Description of “Move to MyBookmark”.

The steps to include in the macro are these:

· Go to the Insert tab and click on the Bookmark item.

· Make sure MyBookmark is highlighted, then click on the Go To button.

Remember to close the dialog box after moving to the bookmark. Remember also to stop recording.

Just before we add the macros to the toolbar so that we can use/test them, let’s take a look at the VBA code that was automatically created when we recorded the macro.

1.8 Click on the Macros item on the Developer toolbar to display the Macros dialog. The dialog displays all of the macros available to you, including those created by other users and programs. Notice that the macros we created display the description we added earlier. Choose one of the macros we created and then click on the Edit button.


The new display that appears is called the Visual Basic Editor or VBE for short. This is where you can develop full-blown applications that make use of any of Word’s features. Take a little time to look at the editor – we will be using it in a little while. When you have finished, close the editor as you would any other program and you will return to Word.


To add the macros to Word’s toolbar, right-click anywhere on the toolbar and choose Customize Quick Access Toolbar from the popup menu that appears.

In the dialog that appears, drop down the list marked Choose commands from and choose Macros.

Find the macros you created and use the Add button to include them on the Quick Access Toolbar. Use the Modify button to choose icons for your new buttons and change the names to something more friendly.

When you have finished, your new buttons will look similar to the illustration. Test the buttons to make sure they work correctly.


The macros and buttons will be available in every Word document you work on. If you want to remove them, use the Macros dialog to delete them. You may also need to modify the Quick Access Toolbar again to remove the buttons.

Task 2: Extending Software Packages (2)

As well as adding new features to Word, VBA can also be used to add new abilities to Excel. In this task, we are going to add two new functions to Excel. Remember the difference between a function and a formula. A formula is a calculation of some kind and can make use of one or more functions to produce a result. A function is a ready-made piece of code that calculates a specific value e.g. SUM adds up a range of numbers.

Excel does not contain functions to convert temperatures. If we want to convert Centigrade to Fahrenheit, for instance, we need to use one or more calculations using a formula like this:

Tf = (9/5)*Tc+32

(Tc = temperature in degrees Celsius, Tf = temperature in degrees Fahrenheit)

2.1 Copy the example below to see how the calculation works. Cell E2 contains this formula:

=(9/5)*B2+32

2.2 If the Developer toolbar is not visible, use the instructions given earlier to display it.

2.3 Enter the VBE by clicking Visual Basic in the Developer toolbar or by pressing ALT+F11.

2.4 On the left side of the VBE screen you should see the Project Explorer window. If you can’t see it, go to the View menu and click on Project Explorer to display it.

2.5 Use the Insert menu to add new Module. The module will be titled something like Book1 - Module1 (Code)

2.6 Enter the code shown below. Indent the line starting with “CtoF” using the Tab key – do not use spaces.

2.7 Close the VBE and return to Excel. You can now use the new function to replace the calculation used earlier. In E2 you might now enter this:

=ctof(B2)

2.8 The formula used to convert Fahrenheit to Centigrade is given below.

Tc = (5/9)*(Tf-32)

2.9 Can you write a new function to perform the calculation? Use the CtoF function as a model and call the new function FtoC.