PDA

View Full Version : Text Alignment



cw75cdn
08-20-2008, 08:41 AM
Hello All,

This is my first post here and I'm sure this is a simple process but I'm just not hitting the nail on the head:

I'm new to Word VBA (and VBA in general) and I've been asked to generate an auto-signature for my companys' Outlook users, I've managed to find code on the Net and from trial and error and a LOT of googling, I've managed to throw a decent auto-signature piece of code together that runs during logon, however, there's only one problem I haven't been able to lick, text alignment. Basically, I want to align everything else in the signature to the right. Everything works properly except the alignment, it is based on code I found on Sue Moxer's Outlook site.

I've searched the forums here and nothing really stands out to me so I'm going to post the code here and hopefully someone can help:

The code:


On Error Resume Next
Set objSysInfo = CreateObject("ADSystemInfo")
strUser = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUser)
strName = objUser.FullName
strTitle = objUser.Description
strCompany = "Company Name"
strAddress = "Company Address"
strCity = "Company City"
strPhone = "Company Phone"
strTollFree = "Company Toll Free Phone"
strFax = "Company Fax"
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection
Set objEmailOptions = objWord.EmailOptions
Set objSignatureObject = objEmailOptions.EmailSignature
Set objSignatureEntries = objSignatureObject.EmailSignatureEntries
objSelection.ParagraphFormat.Alignment = wdAlignParagraphRight
objSelection.TypeParagraph()
objSelection.Font.Name = "Verdana"
objSelection.Font.Size = 10
objSelection.Font.Bold = True
objSelection.TypeText strName & chr(11)
objSelection.Font.Bold = False
objSelection.Font.Italic = True
objSelection.TypeText strTitle & chr(11)
objSelection.Font.Italic = False
objSelection.TypeText chr(11)
objSelection.InlineShapes.AddPicture "C:\Public\Clipart\email-sig-mall.jpg",
true, true
objSelection.TypeText & chr(11)
objSelection.Font.Size = 8
objSelection.TypeText strAddress & chr(11)
objSelection.TypeText strCity & chr(11)
objSelection.TypeText strPhone & chr(11)
objSelection.TypeText strTollFree & chr(11)
objSelection.TypeText strFax & chr(11)
Set objSelection = objDoc.Range()
objSignatureEntries.Add "AD Signature", objSelection
objSignatureObject.NewMessageSignature = "AD Signature"
objSignatureObject.ReplyMessageSignature = "AD Signature"
objDoc.Saved = True
objWord.Quit

cw75cdn
08-20-2008, 10:22 AM
bump. anyone?

cw75cdn
08-20-2008, 03:09 PM
Ok,

So I'm trying a different approach after MUCH reading on the 'Net:

a) create a table
b) insert text/image content into table
c) align cell of table to the right

This seems to be working rather well, but once again, I'm still running into problems getting the alignment to work right. I can create the table, with one row and insert the data into it without issue. However, I still cannot get the alignment of the cell data to work.

Here's some basic code on how I started this plan of attack:


Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection
Set objRange = objDoc.Range()
objDoc.Tables.Add objRange, 1, 1
set objTable = objDoc.Tables(1)


This creates my table with 1 row. However, I've tried several methods to get the alignment of the table content to align to the right without success:


Set objSelection = objTable.Row(1).Range.Select
objSelection.ParagraphFormat = wdAlignParagraphRight


I get the idea I have to select the right object and work with it, I've even run the Macro recorder to make sure I'm not missing anything but when I run my script (saved as foo.vbs) it creates the table, injects the content but does not align the content to the right.

What am I missing? :(

macropod
08-21-2008, 12:45 AM
Hi cw75cdn,

You say
Basically, I want to align everything else in the signature to the right everything else but what? At the moment it looks to me as is you're trying to right-align everything you're adding to the document.

As a general principle, you should base your document on a template with suitable Styles set up to manage the document layout you want, rather than overriding whatever Style format applies to the paragraph you're working with. Amongst other things, that means you don't have to worry about fonts, alignments etc in the code - you simply insert a paragraph with the required Style and start 'typing'.

macropod
08-21-2008, 12:49 AM
Hi cw75cdn,

FWIW, your code would be easier to maintain and understand with a bit of worrk on the structure. For example:
On Error Resume Next
Set objSysInfo = CreateObject("ADSystemInfo")
strUser = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUser)
With objUser
strName = .FullName
strTitle = .Description
End With
strCompany = "Company Name"
strAddress = "Company Address"
strCity = "Company City"
strPhone = "Company Phone"
strTollFree = "Company Toll Free Phone"
strFax = "Company Fax"
Set objword = CreateObject("Word.Application")
With objword
Set objDoc = .Documents.Add()
Set objSelection = .Selection
Set objEmailOptions = .EmailOptions
End With
Set objSignatureObject = objEmailOptions.EmailSignature
Set objSignatureEntries = objSignatureObject.EmailSignatureEntries
With objSelection
.ParagraphFormat.Alignment = wdAlignParagraphRight
.TypeParagraph
With .Font
.Name = "Verdana"
.Size = 10
.Bold = True
End With
.TypeText strName & Chr(11)
With .Font
.Bold = False
.Italic = True
End With
.TypeText strTitle & Chr(11)
.Font.Italic = False
.TypeText Chr(11)
.InlineShapes.AddPicture "C:\Public\Clipart\email-sig-mall.jpg", True, True
.TypeText Chr(11)
.Font.Size = 8
.TypeText strAddress & Chr(11) & strCity & Chr(11) & strPhone & Chr(11) & strTollFree & Chr(11) & strFax & Chr(11)
End With
Set objSelection = objDoc.Range()
objSignatureEntries.Add "AD Signature", objSelection
objSignatureObject.NewMessageSignature = "AD Signature"
objSignatureObject.ReplyMessageSignature = "AD Signature"
objDoc.Saved = True
objword.Quit