Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am working on a script that creates a Word document, but I cannot get the text to be center aligned. Here is an example of the code I'm using. (VBScript)

set objword = createobject("word.application")
set doc = objword.documents.add
set selection = objword.selection
selection.Font.name = "arial" 
selection.Font.size = "20"
selection.Font.Bold = true
selection.Paragraphs.Alignment = wdAlignParagraphCenter
selection.typetext "This is the title that should be centered"
selection.typeparagraph
doc.saveas("testdoc.doc")
objword.Quit
set objword = nothing

I have tried a few different things that I found through Google, but I have not found anything that works.

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

UPDATE: turns out that constants like wdAlignParagraphCenter may not actually be defined, so check that in your code.

The easiest way to get an example of how to do formatting in Word (or Excel) is to actually start recording a macro, do what you want, then stop recording and take a look at the macro content.

In your example code, I don't see any sign of you attempting to center the text. Try something like selection.ParagraphFormat.Alignment = wdAlignParagraphCenter, though that's pulled from the documentation not actually from trying it.

After a bit more experimentation, the value of wdAlignParagraphCenter should be 1 but from the vbs it isn't. Digging into the VBScript documentation to figure out exactly where this is defined, etc. is left as an exercise for the reader.

Also, be willing to read through the documentation - the Visual Basic Reference reachable from Word's code editor has information that will probably be of use to you.

Be aware that (at least in my years-old experience) there may be things you can do in Word that don't have a correctly-functioning code equivalent, though perhaps they've improved things since the days of WordBasic.

share|improve this answer
    
Recording a macro gives the code you provided here, but that does not work. Guess I should have listed the different things I have already tried. –  Tester101 Dec 16 '09 at 14:54
    
Did you put that before or after you inserted the text? I believe that what you're doing in the sample you provided is the equivalent of opening a document, changing the font, then typing. If you change the centering prior to inserting the text it may make a difference - after you insert the text it would be changing the setting for the next paragraph. –  fencepost Dec 16 '09 at 15:49
    
That I would be happy with, at least then I would see a result. I have tried moving it around in the code, but nothing is ever centered. –  Tester101 Dec 16 '09 at 15:56
    
This discussion including the info on those constants being late bound might be of interest: excelforum.com/excel-programming/… –  fencepost Dec 16 '09 at 16:14
    
2 comments deleted/consolidated: The problem is that wdAlignParagraphCenter isn't actually defined outside Word so I suspect it's either throwing an (ignored) error or being treated as 0 which is likely a legitimate value. I found one example that explicitly defined that value to use 1 and tried tossing that value into the command; that did the trick. –  fencepost Dec 16 '09 at 16:23
show 1 more comment
Set objWord = CreateObject("word.application")
Set objDoc = objWord.Documents.Add
Set objSelection = objWord.Selection
objSelection.Font.Name = "arial"
objSelection.Font.Size = "20"
objSelection.Font.Bold = True
objSelection.Paragraphs.Alignment = wdAlignParagraphCenter
objSelection.typetext "This is the title that should be centered"
objSelection.typeparagraph
objDoc.saveas ("testdoc.doc")
objWord.Quit
Set objWord = Nothing
share|improve this answer
    
This did not work for me –  Tester101 Dec 16 '09 at 15:35
    
what did you get? I tried it with the rest of your code and had a nice centered title. –  Beaner Dec 16 '09 at 15:46
    
I edited my code above to include this line of code, but my title is still left justified. –  Tester101 Dec 16 '09 at 15:57
    
I believe the difference here is probably in where you're running the code. If you're running it within Word the constant has the correct value; if you're running it from a .vbs then it does not. –  fencepost Dec 16 '09 at 16:12
    
If fencepost's idea about wdAlignParagraphCenter not being defined is on target you can add it. (wdAlignParagraphCenter = 1) –  Beaner Dec 16 '09 at 16:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.