Results 1 to 2 of 2
  1. #1
    New Lounger
    Join Date
    Jul 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Access to Word in VBA (2000)

    Edited by HansV to preserve indentation (by putting <!t>[pre]<!/t> and <!t>[/pre]<!/t> tags around the code), and to correct some text messed up by the Lounge software/Browser.

    I am trying to send data from an access data table to a microsoft word document. I am having mixed results. I have been able to send fields but I have not been able to format the text properly. I want to create the first line ("Test Results") to be bold and centered. Everyother line I want to be not bold and left aligned. I know there is probable some easy way to do this multiple formatting but I have been unable to figure it out. Here is what I have so far:
    <pre>Private Sub Command0_Click()
    On Error GoTo errorHandler
    Dim wdApp As Word.Application
    Dim myDoc As Word.Document
    Dim mywdRange As Word.Range
    Set wdApp = New Word.Application
    With wdApp
    .Visible = True
    .WindowState = wdWindowStateMaximize
    End With
    Set myDoc = wdApp.Documents.Add
    Set mywdRange = myDoc.Words(1)

    With mywdRange
    .Text = "Test Results"
    .FormattedText.Bold = True
    .Text = .Text & "1. Trial 1 Results: " & Me.<!t>[Trial_1]<!/t>

    .GoToNext wdGoToLine
    .Text = .Text & "2. Trial Location and Date Tested: " & _
    Me.<!t>[Trial Location]<!/t> & " Date:" & Me.[Date]
    .GoToNext wdGoToLine
    .Text = .Text & "3. Success Rate: " & Me.[Success Rate]
    .GoToNext wdGoToLine

    .AutoFormat
    .ParagraphFormat.LineSpacing = 24
    .Font.Name = "Times New Roman"
    .Font.Size = 12
    .Font.ColorIndex = wdBlack
    .Bold = False
    End With

    errorHandler:
    Set wdApp = Nothing
    Set myDoc = Nothing
    Set mywdRange = Nothing
    End Sub
    </pre>


  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Access to Word in VBA (2000)

    Try this; it sets the format for the first line (paragraph) afterwards instead of when entering the text.

    Set mywdRange = myDoc.Words(1)
    With mywdRange
    .Text = "Test Results"
    .Text = .Text & "1. Trial 1 Results: " & Me.<!t>[Trial_1]<!/t>
    .Text = .Text & "2. Trial Location and Date Tested: " & _
    Me.<!t>[Trial Location]<!/t> & " Date:" & Me.[Date]
    .Text = .Text & "3. Success Rate: " & Me.[Success Rate]

    .ParagraphFormat.LineSpacing = 24
    .Font.Name = "Times New Roman"
    .Font.Size = 12
    .Font.ColorIndex = wdBlack
    End With

    Set mywdRange = myDoc.Paragraphs(1).Range
    With mywdRange
    .Bold = True
    .ParagraphFormat.Alignment = wdAlignParagraphCenter
    End With

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •