Export (0) Print
Expand All
Expand Minimize

Office Space: Tips and Tricks for Scripting Microsoft Office Applications

Office Space

Welcome to Office Space, the new column that offers tips and tricks for scripting Microsoft® Office applications. We’ll post new tips every Tuesday and Thursday; to see an archive of previous tips, visit the Office Space Archive. And if you have particular questions about Microsoft Office scripting, feel free to send them to scripter@microsoft.com (in English, if possible). We can’t promise to answer all the questions we receive, but we’ll do our best.

Adding a Drop Cap to a Paragraph

A lot of people write to us and ask, “How can I become a Scripting Guy?” Well, to be honest, it isn’t easy; in fact, the process of becoming a Scripting Guy is very similar to the process of becoming a doctor … well, other than the fact that doctors have to have extensive education, have to pass a rigorous certification exam, and actually have to know what they’re doing. But, other than that ….

But if you do harbor dreams of becoming a Scripting Guy, we’ll let you in on a secret: once you decide to do something, you need to be willing to go all the way. (In fact, you need to be willing to go over the top.) If all you want to do is save data to a text file, the Scripting Guys would be the first to discourage you from using Microsoft Word; after all, it’s a lot easier to use the FileSystemObject to create a plain text file. But if you do want to use Microsoft Word, then the Scripting Guys would also be the first to tell you that you need to be willing to tap into all the formatting capabilities of Word: you need to use different fonts, you need to sprinkle color here and there, you need to toss in a graphic or two. And, to add a touch of class and elegance, consider using a drop cap every now and then.

Yes, a drop cap. If you aren’t sure what a drop cap is, take a look at the big letter H in the screenshot below:

Microsoft Word


It’s a completely meaningless document, but it sure looks cool, doesn’t it? And, best of all, you can easily use a script to add a drop cap to a document, and we’re here to show you how. (And, along the way, we’ll also show you how to change the document view in Word.)

Let’s take a look at the script:



Const wdPrintView = 3

Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add()
Set objView = objDoc.ActiveWindow.View
objView.Type = wdPrintView
objWord.Visible = True

Set objSelection = objWord.Selection

strText = "Here is the first sentence in my paragraph. "
strText = strText & "Here is the second sentence in my paragraph. "
strText = strText & "Here is the third sentence in my paragraph. "
strText = strText & "Here is the fourth sentence in my paragraph. "
strText = strText & "Here is the fifth sentence in my paragraph. "
strText = strText & "Here is the sixth sentence in my paragraph. "
strText = strText & "Here is the seventh sentence in my paragraph. "

objSelection.TypeText strText
objSelection.TypeParagraph()
objSelection.TypeParagraph()
objSelection.TypeText strText

objDoc.Paragraphs(1).DropCap.Enable
objDoc.Paragraphs(1).DropCap.LinesToDrop = 3



We begin by defining a constant – wdPrintView – that we’ll use to set the view for our document to Print Layout. Why? Well, that way we can ensure that you’ll actually be able to see the drop cap. In Normal view the drop cap will be there, but it won’t look like a drop cap. Instead, it’ll look like this:

Microsoft Word


By the way, here are constants (and values) for other view settings:

View Type

Constant

Value

Normal

wdNormalView

1

Web Layout

wdWebView

6

Print Layout

wdPrintView

3

Reading Layout

wdReadingView

7

Outline

wdOutlineView

2


Next we create an instance of the Word.Application object and add a new document. Note that before we set the Visible property to True (which will actually allow us to see our new document) we create an instance of the View object and set the view Type to Print Layout (using the constant wdPrintView we defined a moment ago). Here’s the code that does all that:



Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add()
Set objView = objDoc.ActiveWindow.View
objView.Type = wdPrintView
objWord.Visible = True



Again, what if we wanted to view the document in Reading Layout? Then we’d define a constant named wdReadingView (with a value of 7) and substitute this line of code:



objView.Type = wdReadingView



With our document up and running we create an instance of the Word Selection object and then assign a bunch of sentences to a variable named strText; we do that simply so we can have a paragraph that looks like a real paragraph (though, admittedly, it might not sound like a paragraph). Finally, we use the TypeText method to type our paragraph, use a pair of TyeParagraph methods to enter a blank line (each call to TypeParagrah is equivalent to pressing ENTER on the keyboard), and then – just for the heck of it – use TypeText to type our paragraph a second time. That gives is a document with two paragraphs, neither of which features a drop cap.

So let’s do something about that; let’s add a drop cap to paragraph one. That takes just two lines of code:



objDoc.Paragraphs(1).DropCap.Enable
objDoc.Paragraphs(1).DropCap.LinesToDrop = 3



In the first line we call the Enable method to configure the first character in paragraph one as a drop cap. We then specify the size of our drop cap by setting the value of the LinesToDrop property; a value of 3 means our drop cap will be three lines high. That’s all we have to do.

Of course, if we want to get a little fancier we can change the font for the drop cap by assigning a different font to the FontName property. For example:



objDoc.Paragraphs(1).DropCap.FontName = "Script MT Bold"



That gives us a document that looks like this:

Microsoft Word


You can even modify the position of the drop cap, including the distance between the character and the text. For more information, take a peek at the Microsoft Word VBA Language Reference found on MSDN.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft