To the main website of James Tam 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.

Caution and tip for the assignment

Similar to the previous workbook exercise this assignment will be challenging. In fact it will be significantly more challenging and take more time than the exercise. So you should start working on it as soon as possible. You won't be given extra time if you find yourself "in over your head" and you cannot complete the work before the deadline. On the other hand previous students have been able to complete a good portion of the requirements in previous semesters. Most of them did not have any programming experience so this assignment is one that beginners do have the ability to complete if they apply themselves properly and if they are prepared to put in the time. (Reminder: it involves more than just trying to write the code for this assignment and the exercises, you should try to practice as much as possible beforehand: writing programs and tracing programs in order to develop your skills at both).

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) If you implement Feature #6 this word will be the same search Word that the program will try to find in each document at the specified location.
  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 times or more then the text (from Feature #4a) to be written (Feature 4a) is colored red. Credit will be awarded if the text is colored under the correct condition. (0.4 GPA)
    5. If the search word appears three or more times then the text to be written (from Feature #4a) is also bolded. Credit will be awarded if the text is bolded under the correct condition. (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. Keep in mind that some methods will automatically retain the proportion when just one of the dimensions is changed. (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 as provided to the university (make sure it matches, don't include 'nicknames'), student identification number, tutorial number. (0.045 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, complete and of course correct. [Example] (0.155 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 number of instances of the search Word 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.

Marking and grading

Collaboration:

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].

Submitting your work:

D2L configuration for this course

Late submissions for assignments  when there is no extension granted: Make sure you give yourself enough time to complete the submission process so you don't get cut off by D2L's deadline (or your submission will be automatically flagged as late by D2L and it will be graded appropriately)..

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)