Problem solving using applications by James Tam

Return to the course web page

CPSC 203: Assignment 4 (VBA programming)

Due Tuesday December 8 at 4 PM

Programming MS-Word using VBA (Visual Basic for Applications):

For this assignment you are to write VBA macros that will be included in a Word macro-enabled document (ends in the suffix ".docm") document. In order to get credit for the assignment you must write VBA macro code and you will get no credit, for instance, you just take an existing Word document and manually make the changes using Word instead of writing a program. There are a number of sample documents that the TA's may use when marking your work. None of those documents will include things beyond the requirements of the features listed below, they will just evaluate how well you implemented your program. To get you started here is an [example document]. Use this document as a starting point and embed your VBA instructions here (make sure you don't save the VBA macros in "all word documents" but in the sample document. If you are unsure of what this entails then you should refer to your in class lecture notes (the first set of VBA lecture notes) but here is a reminder image of how you should save your macros:

Because your macros will be included in the document, when it comes time to submit your work you can just upload the macro-enabled document. Double check that there really are macros in the Word document that you upload! It is up to you to do the check, you shouldn't rely on us to do it for you.  If you want to be extra safe (and I highly recommend that you do this) you can copy and paste your VBA program into a regular Word document and submit that extra Word document as well the macro-enabled document.

Similar to the previous assignment everyone starts out with a grade point of zero for this assignment. As you implement the features or documentation requirements listed below your grade will increase. Violating style requirements will result in a decreased assignment grade.

Features that your VBA macros should include:

  1. Word search and replace: The user is prompted for the word to search for and the word to replace it with. The prompts will occur via the InputBox. The program will then search for and replace all instances. If the search word does not exist in the document then nothing will happen. (+0.2 GPA)
  1. Style search and replace: The program will search and replace for all instances of the style "Heading 1" with "Normal".  (+0.2 GPA)
  1. Enlarge all the images in the document  by 25% (alternatively images are 125% of their previous size). There are two versions of this feature, one is a superior version of the other so you will only receive credit for one version. (Max of 0.6 GPA)
 
  • Basic version: Your program is written so it can only enlarge a fixed number of images when the program runs. (+0.3 GPA)
 
  • Advanced version:  Using a loop your program deal with a document containing any number of images. If there are no images in a document then the program should display some sort of meaningful error message to the user otherwise each image will be enlarged by the specified amount. (+0.6 GPA)
  1. Count the number of spelling mistakes in the document. (+0.2 GPA)
  1. Count the number of occurrences of a word in the document. The word will be entered by the user as the program runs via an InputBox. No credit will be awarded if the program counts the number of occurrences of the same word each time that it runs. (+0.3 GPA)
  1. Evaluate the quality of the document. (Max of 1.3 GPA) If there are any spelling mistakes in document then the document is automatically rejected (text = "REJECTED") (+0.3 GPA). If there are no spelling mistakes then the quality of the document is evaluated as follows. When the program runs the user can enter in a search word via a MsgBox and the program counts the number of (exact) occurrences of that word. If there are zero occurrences of that word then the document is neutral (text = "NEUTRAL"). (+0.3 GPA) If there are one or more occurrences of the that word then the document is superior (text = "SUPERIOR"). (+0.3 GPA). You will only get credit for searching the number of occurrences if the search word can be entered as the program runs (nothing if the same search word is used each time that the program runs). Also to get credit for evaluating neutral or superior documents your program first has to be able to evaluate if a document falls under the rejected category (because neutral and superior are documents that are not rejected). The quality of the document ('rejected', 'neutral' or 'superior') must be added to the very top of the document that is being evaluated (+0.1 GPA) and high-lighted as follows: bolded font (+0.1 GPA), font size of 14 points (+0.1 GPA), font color of red (+0.1 GPA). Credit for this feature obviously requires other features (such as #4 & 5) to be fully working beforehand.
  1. Print the document (you won't actually be able to print anything in the 203 labs because there are no printers connected to the computers) but your program should be able to invoke the print command using VBA. (+0.2 GPA)
  1. Close the document and automatically save changes (no choice given to the user). (+0.2 GPA)
  1. Instead of applying Features 1 - 8 on the document in which the macro is contained it will instead it will prompt the user for location to find the documents to be evaluated (e.g., "C:\temp") via a MsgBox and apply Features 1 - 8 to every Word document in that location. When you write the program you can assume that the folder only contains Word documents. You must employ nested loops in order to get credit for this feature, an outer loop successively opens each document in the specified location and will apply Features 1 - 8 inside its body. (+0.6 GPA)

Documentation requirements (see Part I of the VBA notes for a detailed explanation for #1 & 2 summary is provided below): documentation marks may be awarded independently of program functionality

  1. Contact information: Name, student ID and tutorial section (+0.1 GPA for including all pieces of information, no credit for partial information)
  2. A list of features (#1 - 9 above) as well as a clear indication if the feature was or was not fully completed and (if applicable) which version of the feature that was completed (0.1 grade point for every three features listed x 9 features = 0.3 GPA max). You only get documentation marks for each grouping of three features e.g., listing 0 - 2 features provides no documentation marks, listing 3 - 5 features yield 0.1 documentation marks etc. so be sure to document every feature if you don't complete them all.
  3. Use of a versioning system that ties into a list of features completed e.g., "Version (Nov 12) has Feature 8 completed", "Version (Nov 13) Has Features 1, 2, 8 completed" (+0.1 GPA)

Style requirements (see Part I of the VBA notes for a detailed explanation):

Good naming conventions: all program identifiers (variables, named constants, macro and file names must exhibit properties good naming conventions). A penalty of -0.1 GPA will applied for the first instance of a poor naming convention, -0.2 GPA for the second instance down to a maximum penalty of -0.3 GPA for naming conventions.

Appropriate use of named constants. If there is one or two instances where named constants could have been employed but you didn't define them then no penalty is applied. If there were three or more instances where named constants could have been employed then a maximum penalty of -0.1 GPA will be applied. (To get you started the cut-off used to determine if the number of spelling mistakes is unacceptable is a prime candidate for a named constant e.g. "Const SPELLING_CUTOFF = 0").

Submitting your work:

  1. Assignments (the macro-enabled ".docm" file) must be electronically submitted according to the assignment submission requirements using D2L. If you are submitting your code inside a regular Word document (".docx") then include them both inside of zip file and submit the compressed document.
  2. You are not allowed to work in groups for this class. Copying the work of another student will be regarded as academic misconduct (cheating).  For additional details about what is and is not okay for this class please refer to the [notes on misconduct for this course].