Return to the CPSC 203 web page

A3: VBA Programming For Word

Due dates for all assignments can be found on the [graded components] section of the course webpage. Most students will find this assignment more challenging than any of the previous assignments so you should start working on it as soon as possible. Don't wait until all the VBA lectures are finished, once we're covered any material related to the assignment you should immediately implement the appropriate part of the assignment. You won't be granted extra time if you find the assignment more challenging than you expected.

Here is a [starting document] to ensure that you at least start with the type of Word document that can contain a macro (i.e. a "Macro enabled Word document). (It doesn't contain any VBA instructions as you need to do this yourself). The document is provided merely to get you started, whether or not you use it is entirely up to you. If you are unsure of what is required in the creation of a VBA program then you should refer to your in class lecture notes (the first set of VBA lecture notes, VBA Part I) 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 your VBA macro really is 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. Or if you copy paste the program into Word document that contains your macro then you don't need to submit an additional document. As was the case with other assignments do not use other compression utilities (such as zip), otherwise your submission may not be marked.

Write a VBA macro that includes the following features (note that failing to follow style requirements can affect your grade)

Implementing Features #1 - 5 without implementing the last feature will have these features affect the currently active Word document.  Implementing Feature #6 will have Features 1 - 5 apply to all the Word documents in a location specified by the user.

  1. Prompt the user for a word using an InputBox. (0.1 GPA)
  2. Count the number of instances of the word (from Feature #1) using the appropriate parts of the ActiveDocument object. The count should include partial matches e.g. if the input word was 'jam' then 'James' would be counted as match. As you can see in the example, the counting process is not case sensitive. If Feature #6 is implemented (multiple documents are searched) then the same search word should be used for all documents. You will only be awarded 0.2 out of 0.4 GPA if your program requires new prompt for each document opened. (0.4 GPA)
  3. Display the count of the instances via a MsgBox in the following form. "Number occurrences:  <actual number of occurrences  of the search word>" e.g. Number occurrences: 2 (0.1 GPA)
  4. Write information about the number of instances into the currently active Word document using the appropriate parts of the Selection object. (If Feature #6 is implemented then this information would be written into the respective document in which the count was being conducted). Example of a document (counting number of instances of 'silent') [before program counts] [after count]. Note: Features 4d & 4e only affect the text that your program writes to the document, the formatting of the rest of the text in the document is unchanged.
    1. The text written should take the form: "Number occurrences:  <actual number of occurrences  of the search word>" e.g. Number occurrences: 2 (0.3 GPA)
    2. This text should appear on its own line i.e. it should be preceded by "a hard return" or a VBA carriage return "vbCr". (0.3 GPA)
    3. The text appears at the end of the document ("end of story"). (0.3 GPA)
    4. If the search word (entered with Feature #1) appears two or more times then the text to be written (Feature 4a) is colored red. (0.4 GPA)
    5. If the search word appears three or more times then the text to be written is also bolded. (0.4 GPA)
  5. Increase the 'accessibility' of the document: If there are any images in the document then they will be modified using the appropriate parts of the ActiveDocument object.
    1. Double the size (both the height and width change so the proportions of height to size remains the same) of each image (in VBA "InLineShapes" in the document. (0.3 GPA)
    2. If the document doesn't contain any images then a MsgBox will appear with the message "No images to modify". (0.2 GPA)
  6. Modify multiple documents (automating a repetitive task): Program runs as above but it will run Features 2 - 5 on all the Word documents at a location specified by the user. That is, the above 4 features (2-5) are contained in the body of a loop defined for Feature #6. Again: Feature #1 runs only once, the program searches for the same word for all Word documents.
    1. Prompt the user for the location of the Word documents using an InputBox e.g. e.g. "C:\203". The program should not require that the user enters a slash after the name of the last folder (in this example the containing folder is '203') because your VBA program will automatically add it to the end before trying to open any documents. (0.1 GPA)
    2. If the user enters an empty location i.e. presses "OK" without specifying a path then the program will display a MsgBox that contains the message "No location entered, ending program" and then the program will end. Hint: you don't need to use a special instruction to end the program. Reaching the end of the subroutine (or part of the subroutine) will automatically end the program. (0.2 GPA)
    3. If the location is not empty then the program will successively open  each Word document the  at that location using the appropriate parts of the Documents collection. To get credit for this feature and the next feature the program must not only open just Word documents but only open certain types of Word documents: older Word 97-2003 (.doc) documents, newer Word 2007+ (.docx) documents and even macro enabled documents (.docm). If the folder contains any other type then your program must be able to 'ignore' them (not open them) and only open these 3 types of Word documents. (0.2 out of 0.5 GPA if the program tries to open all files in the specified location even if they aren't Word documents). (0.5 GPA)
    4. After opening each Word document the program will apply Features 2 - 5 on each document. (0.5 GPA)

Documentation requirements

  1. Contact information: your full name, student identification number. (0.05 GPA)

  2. Specifying clearly 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. [Example] (0.15 GPA)

Style requirements

  1. Each level of code indenting is consistently 1 tab. Instructions in the sub-routine (between the 'sub' and 'end-sub' is 1 level, the body of IF or WHILE structures counts as another level of indenting. An IF within an IF or WHILE within a WHILE (or even  an IF within a WHILE or a WHILE within an IF) each count as another level. Penalty: -0.2 GPA applied to any case (and not each case) where the required indenting is not applied and applied consistently.
  2. Good naming conventions (e.g. variables, sub-routines, the name of Word document containing the VBA program and constants if applicable) are followed. Some examples of naming conventions are provided in [the VBA Part I notes]. Penalty: -0.2 GPA applied to any case (and not each case) where poor naming conventions have been used.

JT's cautions: these are oddities that I encountered when I was making up my solution.

Number 1If your images get too large (i.e. there's a maximum size allowed) then when you run your program and it tries to enlarge an image you will get a "runtime error".

If then select 'Debug' you may see VBA flag the instruction that tries to enlarge an image as the source of the problem.

Number 2: Order is important. When writing text to the Word document or documents (Feature #4) put the instruction that tells VBA to write the text to the end of the document before any instructions that format the text (bold and red font). Reversing the order may result in no formatting effects for the font even if the word count is sufficiently high. Look at this [example] that I created which illustrates the effect changing the order. The text inserted at the end is not colored red when the person writing the program wanted it be colored. On the other hand the text inserted at the start of the document is colored red. Note the difference in the ordering of the statement between the two insertions.

Submitting your work:

Important points to keep in mind:

  1. Extensions may be granted for reasonable cases by the course instructor with the receipt of the appropriate documentation (e.g., a completed Statutory declaration form that has been signed by appropriate Deponent, you can get an appointment via the Office of the Registrar). Typical examples of reasonable cases for an extension include: illness or a death in the family. Cases where extensions will NOT be granted include situations that are typical of student life: having multiple due dates, work commitments etc. Teaching Assistants (the people working in the 203 lab room) will not be able to provide extension on their own and must receive written permission from the course instructor first. (Note: Forgetting to submit/not properly submitting your assignment or a component of your assignment in does not constitute a sufficient reason for special grading considerations).
  2. CollaborationAssignments must reflect individual work, group work is not allowed in this class nor can you copy the work of others. To avoid problems students should not see each others assignment solution.

  3. Execution: programs must work on the machines on campus Windows computers. (For the remote learning semester since access to the lab computers is more challenging: so the requirement is slightly relaxed and your file simple has to work on any Windows computer). It's up to you to test and check this is the case. Non-functional submissions will receive only partial credit (if any at all).

  4. Late submissions:

    Submission received:

    On time

    Hours late : >0 and <=24

    Hours late: >24 and <=48

    Hours late: >48 hours

    Penalty:

    None

    -1.5 GPA

    -3.0 GPA

    No credit

Marking: