View Single Post
 
Old 03-22-2016, 08:18 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,521
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The code I posted for the 'QueryA!ConditionCategory' processing is sufficient for output that consists of a single paragraph. If not, you'd need to define a range object and use that.

The .Range object as used in the snippet you quoted explicitly applies to the third-last paragraph in the document. Since .Alignment is a .Paragraphs property and not a .Range property, it uses the same .Paragraphs reference as .Range.

As most of my Word knowledge & programming skills are self taught, developed whilst helping other users solve their Word productivity issues, I'm not really in a position to recommend something specific to your needs. That said, a book you might find useful is The Secret Life of Word: A Professional Writer's Guide to Microsoft Word Automation, by R Delwood, published by XML Press in 2011(http://xmlpress.net/publications/word-secrets/). I contributed content for and did much of the technical review of this book. This isn't a programming book as such (though it does have some programming in it) and doesn't profess to teach you how to program. Rather, it shows how to combine Word's various tools to achieve whatever the desired result might be. Another that I contributed to (and has much more programming in it) is Word Hacks, by A Savikas, published by O'Reilly Media in 2005 (http://shop.oreilly.com/product/9780596004934.do). I contributed content for this book also. Although it pre-dates Office 2007, much of the content is still relevant.

On a side note, if your code was properly structured, its logic would be easier to follow and you would need fewer comments. For example:
Code:
Private Sub but_Unsatisfactory_Click()
Dim QueryA As DAO.Recordset
Dim QueryB As DAO.Recordset
Dim dbs As DAO.Database
Dim strSQLA As String, strSQLB As String
Dim DA As String, DAX As String
Dim Variable As String
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wdRng As Word.Range
Dim wdTbl As Word.Table
 
If Forms!frm_Assess!lst_Referrals.Column(5) <> "" Then
  Set dbs = CurrentDb
  DA = Forms!frm_Assess!txt_DA
  DAX = """" & DA & """"
 
  'Query SQL String for Checks
  strSQLA = "SELECT tbl_DA.DAID, tbl_DA.[DA No], tbl_DAConCheck.CheckTitle, tbl_DAConCheck.CheckOutcome, tbl_DAConCheck.CheckComments, tbl_DAConCheck.ConditionCategory, tbl_DAConCheck.Order " & _
    "FROM tbl_DA INNER JOIN tbl_DAConCheck ON tbl_DA.DAID = tbl_DAConCheck.DAID " & _
    "WHERE (((tbl_DA.[DA No])=" & DAX & ") AND ((tbl_DAConCheck.CheckOutcome)<> ""Invisible"")) " & _
    "ORDER BY tbl_DAConCheck.ConditionCategory, tbl_DAConCheck.Order;"
 
  'Query SQL String for RAI
  strSQLB = "SELECT tbl_DA.DAID, tbl_DA.[DA No], tbl_DAConCheck.RAIOutcome, tbl_DAConCheck.RAITitle, tbl_DAConCheck.RequestAdditionalInformation, tbl_DAConCheck.ConditionCategory, tbl_DAConCheck.Order " & _
    "FROM tbl_DA INNER JOIN tbl_DAConCheck ON tbl_DA.DAID = tbl_DAConCheck.DAID " & _
    "WHERE (((tbl_DA.[DA No]) = " & DAX & ") And ((tbl_DAConCheck.RAIOutcome) = True)) " & _
    "ORDER BY tbl_DAConCheck.ConditionCategory, tbl_DAConCheck.Order;"
 
  'Set Recordsets
  Set QueryA = dbs.OpenRecordset(strSQLA)
  Set QueryB = dbs.OpenRecordset(strSQLB)
 
 'Start Word
  Set wdApp = CreateObject("Word.Application")
  With wdApp
    .Visible = True
    .ScreenUpdating = False
 
    'Create a New Document
    Set wdDoc = .Documents.Add
    With wdDoc
 
      'Basic Document Format
      With .Styles(wdStyleNormal)
        .Font.Name = "Arial"
        .Font.Size = 11
        With .ParagraphFormat
          .LineSpacingRule = wdLineSpaceSingle
          .SpaceAfter = 0
          .SpaceBefore = 0
        End With
      End With
      'Title/IntroPage
      'Insert Gray Table
      Set wdTbl = .Tables.Add(Range:=.Range.Characters.Last, NumRows:=1, NumColumns:=1, _
        DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed)
      With wdTbl
        .Style = "Table Grid"
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = False
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = False
        .ApplyStyleRowBands = True
        .ApplyStyleColumnBands = False
        With .Cell(1, 1).Range
          .Shading.BackgroundPatternColor = -603937025
          .InsertAfter "Council Report Summary" & vbCr & _
            "The proposed development application does not comply with the requirements of Council's relevant policies."
          .Paragraphs.First.Range.Style = wdStyleStrong
        End With
      End With
      'Make space for query insertion
      .Range.InsertAfter vbCr & vbCr & vbCr & vbCr
      Set wdRng = .Paragraphs.Last.Previous.Previous.Range
      'Insert & format Query
      With wdRng
        .InsertBefore QueryA!ConditionCategory
        .Style = wdStyleStrong
        .Paragraphs.Alignment = wdAlignParagraphCenter
      End With
 
      'Checks Loop Start
      Variable = QueryA!ConditionCategory
      Do While Not QueryA.EOF
        'Check if Check Category is the same as previous if not type new category
        If Variable <> QueryA!ConditionCategory Then
          .Range.InsertAfter vbCr
          Set wdRng = .Paragraphs.Last.Previous.Range
          'Insert & format Query
          With wdRng
            .InsertBefore QueryA!ConditionCategory
            .Style = wdStyleStrong
            .Paragraphs.Alignment = wdAlignParagraphCenter
          End With
        End If
        'Make sure check has a title and insert table fill in with notes
        If IsNull(QueryA!CheckTitle) = False Then
          Set wdTbl = .Tables.Add(Range:=.Range.Characters.Last, NumRows:=1, NumColumns:=2, _
          DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed)
          With wdTbl
            .Style = "Table Grid"
            .ApplyStyleHeadingRows = True
            .ApplyStyleLastRow = False
            .ApplyStyleFirstColumn = True
            .ApplyStyleLastColumn = False
            .ApplyStyleRowBands = True
            .ApplyStyleColumnBands = False
            If IsNull(QueryA!CheckTitle) = False Then .Cell(1, 1).Range.Text = QueryA!CheckTitle
            If IsNull(QueryA!CheckOutcome) = False Then .Cell(1, 2).Range.Text = QueryA!CheckOutcome
            If IsNull(QueryA!CheckComments) = False Then .Cell(1, 2).Range.End.InsertBefore vbCr & QueryA!CheckComments
          End With
        End If
        Variable = QueryA!ConditionCategory
        QueryA.MoveNext
      Loop
      QueryA.Close
      'Save the document
      .SaveAs CurrentProject.Path & "\TestDoc.doc"
    End With
    .ScreenUpdating = True
  End With
Else
  MsgBox "Referal Completed Date Required", 0, "Date Required"
End If
Set wdRng = Nothing: Set wdTbl = Nothing: Set wdDoc = Nothing: Set wdApp = Nothing
End Sub
Note: The above code shows how to set a paragraph range for the queries.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote