Jaa


Solution - Novice Challenge 8: It’s Not My Fault

We all make mistakes, right? Like maybe we say something we really shouldn’t have said, or even worse, we write down something we really shouldn’t have written down. Or sometimes we say or write something down that’s perfectly okay, unless someone thinks a different person was responsible for saying it or writing it down. Or unless someone thinks you’ve promised something that you didn’t mean to promise. Or…well, the possibilities for getting yourself or – even worse, your company (that’s worse?) – into trouble are virtually endless.

There may not be much you can do to keep yourself out of trouble, but chances are your company (and everyone in it at a higher level than you) is going to protect itself wherever it can. That’s what disclaimers are all about. (Note: The opinions of the OfficePalooza organizers are most definitely not the opinions of Microsoft or the Microsoft Office Marketing Team.)

So, in keeping with the spirit of making sure everyone stays happy and out of court, Challenge 8 was all about opening a Word document, copying the contents of the file, then pasting those contents at the end of another Word document (a document that happens to already be open). You may have accomplished this differently, but here’s what we did:

Sub AddDisclaimer()

 

Dim d As Document

Dim s As Selection

Dim str As String

 

'Open disclaimer doc

Set d = Documents.Open(ThisDocument.Path & "\disclaimer.docx")

'Select all the contents of the disclaimer doc

d.Content.Select

'Copy the selected contents

Selection.Copy

 

'Close the disclaimer doc

d.Close

 

'Set this document at the active document

ThisDocument.Activate

'Move the cursor to the end of the doc

Selection.EndKey wdStory, wdMove

'Start a new paragraph

Selection.TypeParagraph

 

'Paste the copied content

Selection.Paste

 

End Sub

 

As usual, we start by declaring some variables. Next comes this line of code:

Set d = Documents.Open(ThisDocument.Path & "\disclaimer.docx")

Here all we’re doing is calling the Open method of the Document object to open a Word document, in this case the document containing our disclaimer (disclaimer.docx). One of the stipulations of this challenge was that you couldn’t hard-code the full path into your code. (Can you imagine testing hundreds of entries, all with a different path? No thank you.) We told you to assume that the document was in the current folder. So why couldn’t we just do this:

Set d = Documents.Open("disclaimer.docx")

Well, we could, but most of the time that won’t work – Word actually wants a full path. So how do we supply a full path without hard-coding a path? Easy: we just look in the Path property of the current document to find out where we are, then add that path to the file name. You know, like this:

ThisDocument.Path & "\disclaimer.docx"

Now that we have the document open, we want to select all the contents of that document and copy it to the Clipboard. Amazingly enough, we select and copy by using the Select and Copy methods:

d.Content.Select

Selection.Copy

Now that we have the contents safely tucked away on the Clipboard we can close the document:

d.Close

Next we set the document we’re working from (that is, the document we want to copy the selection to) as the active document, just to make sure we copy to the right place:

ThisDocument.Activate

If we were to paste the selection into the document now, the data would be pasted in wherever the cursor happened to be within the document. That would be okay, provided that our cursor is at the end of the document. We can’t really guarantee that, so we need to add some code to move the cursor:

Selection.EndKey wdStory, wdMove

The EndKey method moves the selection (in this case, since we haven’t selected anything, that’s the cursor) to the end of the document. Actually, that’s not completely true. EndKey moves to the end of something, but we have to tell is what. It could move the cursor to the end of a line, the end of a paragraph, or the end of various other places. We want to move to the end of the whole document, so we specify wdStory to move to the end of the whole story. We also pass in the wdMove parameter, which means we’re only moving the cursor and not selecting everything between where the cursor had been and where we’re moving it to.

We now have our cursor at the end of our document. But what if the end of the document isn’t a blank line? For example, the end of the document might immediately follow the last word in the document. We don’t want to paste our disclaimer if it’s going to merge into another paragraph. Therefore, to be on the safe side, we call the TypeParagraph method to insert a new paragraph:

Selection.TypeParagraph

We now have our cursor positioned on a new line at the end of our document, so we can paste our selection (remember, the disclaimer we copied?) into our document.

And we’re now fully protected against anyone suing us over anything we said inside that document. Or so we think….