To the main website of James Tam | Return to the CPSC 203 web page |
Due dates for all assignments can be found on the [main grid] of the course webpage.
Note: The computer lab for CPSC 203 is located in SS018 (the "Tri-Labs"). If you find that your particular version of Word (e.g. Office 365, older versions of Office, MAC users) does not include everything needed for a feature then you should to complete that feature in SS018. Alternatively for the remote learning version of the course you can login to a lab computer from your own. [Information link created by UC-IT with modifications by James Tam, for questions: www.ucalgary.ca/it]
Location of some starting documents (not mandatory): https://pages.cpsc.ucalgary.ca/~tamj/books/chop_suey_chop_chop/examples/VBA/step_by_step/starting/
Whether you use the starting macro enabled document (a docm file) or create your own you are to complete the steps below in order to have a working program and you are to submit the document containing your macro. It's recommended that you also copy-paste your program into the body of a Word document (the document containing the macro - if you paste it into another Word document then make sure you submit this document as well so it's available to your marker).
Writing a VBA macro program for Word:
You will be writing a program using the VBA programming language that will prompt the user for a path (location) and a name of a Word document. The program will then try to open document at the specified location.
Step 1:
Open Word and create a new blank document or open the starting document. Close any other Word documents that you may have opened.Step 2:
Save the document under a different name. Under the File tab select the Save As menu option.Step 3:
Select a location that you can remember.Step 4:
In the file name field rename the document as 'Typing a program, opening a specified document - solution.docm'Step 5
: To create a new macro in Word look at the View tab (Macros group) and click on the 'down' arrow triangle when you can then select the "View Macros" option in the menu that appears.Step 6: Look for the input field under the label "
Macro Name" and enter the following 'openingSpecifiedDocument' but without the quotes.Step 7
: Look for the label "Macros in" and select under the pull-down menu the name of the starting document Typing a program, opening a specified document - solution.Step 8
: Under the text Macro name type in 'openingSpecifiedDocument'Step 9
: Click on the 'Create' button.Step 10
: In the VBA code editor, [after line of red colored documentation and before the line containing "End Sub"] enter the following instructions for your VBA program. As a reminder: before typing in each line containing an instruction you should first hit tab so the instruction is indented.: Dim documentName As StringStep 10A
Step 10B
: Dim path As StringStep 10C
(long instructions such as the following should be entered all on a single line in the VBA editor): documentName = InputBox("Type in the name of the document to open (e.g. Example document1): ")Step 10D
: Documents.Open (documentName)Step 11
: Run the program, click on the 'Play' icon. An error message appears. The specific content of the message will vary. The gist of the message indicates that the file with the name you just typed in could not be found. The error message should also specify the path or location where it tried to open this file. You should be able to verify that this path (or at least the part that you can see in the popup if the path is a long one) is the location of the Word document containing this program.Step 12
: Go back to the code editor and type in the following after what you entered in Step 10C documentName = InputBox(...) and before what you entered in Step 10D Documents.Open(...): path = ActiveDocument.pathStep 13:
Delete the instruction that you previously entered in Step 10D Documents.Open (documentName). Retype on this line the following new instruction: Documents.Open (path & documentName)Step 14
: Run the program, click on the 'Play' icon. An error message appears. Look carefully at the information specifying the path and file name. You should see that the name of the last folder and the file name you entered are shown as one word. For instance if program tried to open a file at the location: C:\203\Ex1 and you typed in Example document1 for the name of the file then the path and the file name would appear as C:\203\Ex1Example document1. In this example it's trying to open a file called Ex1Example document1 in the location C:\203. A slash is needed to separate the name of the last folder Ex1 from the name of the file that the program is trying to open Example document1.Step 15: Delete the instruction from Step 13
Documents.Open (path & documentName). Retype on this line the following new instruction: Documents.Open (path & "\" & documentName)Step 16
: Run the program, click on the 'Play' icon. Another error message appears. This one will likely more challenging for most to decipher because it requires knowledge of file name suffixes used in Windows. (File name suffix = last few characters at the end of a file name after the period e.g. for image file me.jpg the file name suffix is jpg). By default the file name suffix is hidden from the user in Windows. The file name suffix used for Word 2007+ documents is 'docx'. The complete filename and the suffix is needed to open a Word document via Documents.OpenStep 17:
Delete the instruction from Step 15: Documents.Open (path & "\" & documentName). Retype on this line the following new instruction (that includes the filename suffix): Documents.Open (path & "\" & documentName & ".docx")Step 18
: Run the program, click on the 'Play' icon. If you completed the steps correctly and what you entered for the document name exactly matches a document residing in the location where your VBA program is located then you should not see any error message. Instead you should note that Word has opened the specified document.Although you won't be graded on documentation for exercises it still would be a good idea to include it so you don't miss it in the assignment. What's needed for assignments include: your full name as provided to the university (make sure it matches, don't include 'nicknames'), student identification number, tutorial number. Also you should clearly specify what features of the assignment that you completed or didn't complete. In order to get credit the documentation has to be clear and complete.
Assignments must reflect individual work; group work is not allowed in this class nor can you copy the work of others. Some "do nots" for your solution: don't publically post it, don't email it out, don't show it to other students, don't get help with your assignment from a tutor (if you have hired one). For more detailed information as to what constitutes academic misconduct (i.e., cheating) for this course please read the following [link].
Submission received: |
On time |
Hours late : >0 and <=24 |
Hours late: >24 and <=48 |
Hours late: >48 and <=72 |
Hours late: >72 and <=96 |
Hours late: >96 |
Penalty: |
None |
-1 GPA |
-2 GPA |
-3 GPA |
-4 GPA |
No credit (not accepted) |