Hi
You probably don't want to hear all this, but....
A "line" is a very hazy concept in Word. Word wraps lines within paragraphs
on the fly according to the active printer, the paper size, the margins,
indents, font and so on.
The chr(10) in your string will put a new line feed, *not a paragraph mark*
into Word. It's a rare case that needs a new line feed. They're awkward on a
good day and difficult to manipulate on a bad one.
You would almost certainly be better off putting each part of the text into
a separate paragraph.
To tell Word to centre a paragraph, you can do two things:
- either format the paragraph in question as centred, or
- apply to the paragraph a style that is formatted as centred.
If you have more than one centred paragraph in the document, then choose the
Style method. Let's say that your CourtCounty is a major heading and the
CourtBranch is a minor heading. So you'd use the built-in Heading 1 style
for the Court County and Heading 2 for the CourtBranch. Here's some code to
play with:
Sub PlayWithWord()
Dim appWord As Word.Application
Dim oDoc As Word.Document
Dim CourtCounty2 As String
Dim CourtCounty As String
Dim CourtBranch As String
Dim rng As Word.Range
CourtCounty = "Somewhere"
CourtBranch = "Branch one"
'Set appWord in some appropriate way in Access
Set appWord = Word.Application
'If you're not using the Active Document, then
'set oDoc in Access in some appropriate way
Set oDoc = appWord.ActiveDocument
'Once you've set the styles, you don't
'need to do this every time.
'Better is to create a template with your styles
'defined as you want them, and create the new document
'from the template.
'But modifying the styles here on the fly is better
'than nothing.
oDoc.Styles(wdStyleHeading1).ParagraphFormat.Alignment =
wdAlignParagraphCenter
oDoc.Styles(wdStyleHeading2).ParagraphFormat.Alignment =
wdAlignParagraphCenter
'Work out where to insert the text at the end of the document
Set rng = oDoc.Range
rng.Collapse wdCollapseEnd
'Insert an empty paragraph in case the last text in the document
'is at the end of an existig paragraph of text
rng.InsertAfter vbCrLf
rng.Collapse wdCollapseEnd
'Insert the text
rng.InsertAfter CourtCounty & vbCrLf
rng.Style = wdStyleHeading1
rng.Collapse wdCollapseEnd
rng.InsertAfter CourtBranch & vbCrLf
rng.Style = wdStyleHeading2
End Sub
Resources:
Why use Microsoft Word's built-in heading styles?
http://www.ShaunaKelly.com/word/numbering/UseBuiltInHeadingStyles.html
How to modify styles in Microsoft Word
http://www.ShaunaKelly.com/word/styles/ModifyAStyle.html
Creating a macro with no programming experience using the recorder
http://www.word.mvps.org/FAQs/MacrosVBA/UsingRecorder.htm
How to modify a recorded macro
http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm
What is the relationship between a Word document and its template?
http://www.ShaunaKelly.com/word/templaterelations/index.html
Hope this helps.
Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word