• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1683
  • Last Modified:

Need help utilizing left indent in Word VBA - after export from Access

In my Access database, I'm currently exporting information to Word to create a report (so that it can be combined with other departments, etc. - could not use reporting in Access). Anyway, I've almost got it completed (thanks to much help from EE...since I know nothing about Word VBA), but I'm stuck on one issue:

I need the table cells for the Description [WeeklyUpdDes] to indent if there is a second line of text.

I've perused the site, found some information but did not know how to apply it in my case (using .LeftIndent = InchesToPoints(0.11)).  I also recorded a macro and tried to utilize that information but it failed.  I'm either on the totally wrong track or not placing the code in the right place because nothing happens w.r.t. to indention.  Any help is most appreciated. My current code is pasted below.
Private Sub cmdNewExport_Click()
 
Dim Word 'the Word application
Dim doc 'the Word document
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim sql As String
Set db = CurrentDb
Dim objTable As Word.Table
Dim objRange As Word.Range
Dim intTable As Integer, intRow As Integer
 
sql = "SELECT tbl_WeeklyCategory.CategoryName, tbl_WeeklyUpdate.WeeklyUpdName, tbl_WeeklyUpdate.WeeklyUpdDes"
sql = sql & " FROM tbl_WeeklyUpdate INNER JOIN tbl_WeeklyCategory ON tbl_WeeklyUpdate.WCatID = tbl_WeeklyCategory.WCatID"
sql = sql & " WHERE tbl_WeeklyUpdate.Date = Date()"
sql = sql & " ORDER BY tbl_WeeklyUpdate.WCatID, tbl_WeeklyUpdate.WeeklyUpdName;"
 
Set rs = db.OpenRecordset(sql)
 
Set Word = CreateObject("word.application")
Set doc = Word.Documents.Add
  
With Word
    .Visible = True
    Set doc = .Documents.Open("c:\PDE_Weekly_Report.doc", , False)
End With
 
doc.GoTo what:=wdGoToBookmark, Name:="Start"
 
    With Word.Selection
 
        .TypeText Text:="Pre-Development Evaluation Weekly Summary"
        .TypeParagraph
        .TypeText Text:="Week Ending "
        doc.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
        "DATE  \@ ""dddd d MMM, yyyy"" ", PreserveFormatting:=True
        .MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
        .MoveLeft Unit:=wdCharacter, Count:=33, Extend:=wdExtend
        .Font.Bold = wdToggle
        .ParagraphFormat.Alignment = wdAlignParagraphCenter
        .MoveDown Unit:=wdLine, Count:=1
    
    End With
 
doc.Tables.Add Range:=Word.Selection.Range, NumRows:=1, NumColumns:= _
        2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitFixed
 
    With doc.PageSetup
        .LineNumbering.Active = False
        .Orientation = wdOrientPortrait
        .TopMargin = InchesToPoints(0.4)
        .BottomMargin = InchesToPoints(0.4)
        .LeftMargin = InchesToPoints(0.8)
        .RightMargin = InchesToPoints(0.6)
        .Gutter = InchesToPoints(0)
        .HeaderDistance = InchesToPoints(0.5)
        .FooterDistance = InchesToPoints(0.5)
        .PageWidth = InchesToPoints(8.5)
        .PageHeight = InchesToPoints(11)
    End With
        
    With doc.Tables(1)
        .Columns(1).SetWidth 100, wdAdjustNone
        .Columns(2).SetWidth 400, wdAdjustNone
    End With
        
    With Word.Selection.Tables(1)
        If .Style <> "Table Grid" Then
            .Style = "Table Grid"
        End If
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = True
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = True
    End With
  
   While Not rs.EOF
        If Not tmpCat = rs.Fields("CategoryName") Then
            With Word.Selection
                .SplitTable
                .Font.Size = 11
                .TypeParagraph
                .Font.Size = 11
                .MoveDown
                .Style = ActiveDocument.Styles("Categories")
                .Font.Size = 11
                .SelectRow
                .Shading.Texture = wdTextureNone
                .Shading.ForegroundPatternColor = wdColorAutomatic
                .Shading.BackgroundPatternColor = wdColorLightYellow
                .TypeText Text:=rs.Fields("CategoryName").Value
                .MoveRight Unit:=wdCell
                .MoveRight Unit:=wdCell
            End With
            tmpCat = rs.Fields("CategoryName")
        End If
        
        With Word.Selection
            .Style = ActiveDocument.Styles("Projects")
            .Font.Bold = wdToggle
            .Font.Size = 11
            .SelectRow
            .Shading.Texture = wdTextureNone
            .Shading.ForegroundPatternColor = wdColorAutomatic
            .Shading.BackgroundPatternColor = wdColorWhite
            .TypeText Text:=rs.Fields("WeeklyUpdName").Value
            .MoveRight Unit:=wdCell
        End With
        
        With Word.Selection
                With Selection.ParagraphFormat
                    .LeftIndent = InchesToPoints(0.25)
                End With
            .Style = ActiveDocument.Styles("Desc")
            .Font.Size = 11
            .TypeText Text:=rs.Fields("WeeklyUpdDes").Value
            .MoveRight Unit:=wdCell
        End With
        rs.MoveNext
        
    Wend
    
    intTable = ActiveDocument.Tables.Count
    intRow = ActiveDocument.Tables(intTable).Rows.Count
    doc.Tables(intTable).Rows(intRow).Delete
    
    Selection.HomeKey Unit:=wdStory
    
    Word.Dialogs(wdDialogFileSaveAs).Show
 
End Sub

Open in new window

0
setalley
Asked:
setalley
  • 2
  • 2
1 Solution
 
puppydogbuddyCommented:
I am not a word expert, but try this in place of your code begining with line 111, and see if it helps.

Set objRange = ActiveDocument.Paragraphs(3).Range
Set objTable = ActiveDocument.Tables.Add(Range:=oRg, numrows:=2, numcolumns:=3)

intTable = ActiveDocument.Tables.Count
intRow = ActiveDocument.Tables(intTable).Rows.Count

With Word.Selection
    If intRow > 1 Then
        objTable.Rows.LeftIndent = InchesToPoints(0.25)
    End If
    .Style = ActiveDocument.Styles("Desc")
    .Font.Size = 11
    .TypeText Text:=rs.Fields("WeeklyUpdDes").Value
    .MoveRight Unit:=wdCell
End With
0
 
setalleyAuthor Commented:
I'm sure it's something I've done wrong (I'm not a 'coder') but I'm receiving the following error message when I try using your code:

Run-time error '424'
Object required
Debug highlights the following line:
Set objTable = ActiveDocument.Tables.Add(Range:=oRg, numrows:=2, numcolumns:=3)
        With Word.Selection
            .Style = ActiveDocument.Styles("Projects")
            .Font.Bold = wdToggle
            .Font.Size = 11
            .SelectRow
            .Shading.Texture = wdTextureNone
            .Shading.ForegroundPatternColor = wdColorAutomatic
            .Shading.BackgroundPatternColor = wdColorWhite
            .TypeText Text:=rs.Fields("WeeklyUpdName").Value
            .MoveRight Unit:=wdCell
        End With
        
        'Placed your code here
                
        Set objRange = ActiveDocument.Paragraphs(2).Range
        Set objTable = ActiveDocument.Tables.Add(Range:=oRg, numrows:=2, numcolumns:=3)
 
        intTable = ActiveDocument.Tables.Count
        intRow = ActiveDocument.Tables(intTable).Rows.Count
 
    With Word.Selection
        If intRow > 1 Then
        objTable.Rows.LeftIndent = InchesToPoints(0.25)
    End If
        .Style = ActiveDocument.Styles("Desc")
        .Font.Size = 11
        .TypeText Text:=rs.Fields("WeeklyUpdDes").Value
        .MoveRight Unit:=wdCell
    End With
    Wend
    
    
    'Continued on with the remainder of my existing code
    
    intTable = ActiveDocument.Tables.Count
        With Word.Selection
            .Style = ActiveDocument.Styles("Projects")
            .Font.Bold = wdToggle
            .Font.Size = 11
            .SelectRow
            .Shading.Texture = wdTextureNone
            .Shading.ForegroundPatternColor = wdColorAutomatic
            .Shading.BackgroundPatternColor = wdColorWhite
            .TypeText Text:=rs.Fields("WeeklyUpdName").Value
            .MoveRight Unit:=wdCell
        End With
           
        
        
        
        
        Set objRange = ActiveDocument.Paragraphs(2).Range
        Set objTable = ActiveDocument.Tables.Add(Range:=oRg, numrows:=2, numcolumns:=3)
 
        intTable = ActiveDocument.Tables.Count
        intRow = ActiveDocument.Tables(intTable).Rows.Count
 
    With Word.Selection
        If intRow > 1 Then
        objTable.Rows.LeftIndent = InchesToPoints(0.25)
    End If
        .Style = ActiveDocument.Styles("Desc")
        .Font.Size = 11
        .TypeText Text:=rs.Fields("WeeklyUpdDes").Value
        .MoveRight Unit:=wdCell
    End With
    Wend
    
    
    
    
    intTable = ActiveDocument.Tables.Count
        With Word.Selection
            .Style = ActiveDocument.Styles("Projects")
            .Font.Bold = wdToggle
            .Font.Size = 11
            .SelectRow
            .Shading.Texture = wdTextureNone
            .Shading.ForegroundPatternColor = wdColorAutomatic
            .Shading.BackgroundPatternColor = wdColorWhite
            .TypeText Text:=rs.Fields("WeeklyUpdName").Value
            .MoveRight Unit:=wdCell
        End With
           
        
        
        
        
        Set objRange = ActiveDocument.Paragraphs(2).Range
        Set objTable = ActiveDocument.Tables.Add(Range:=oRg, numrows:=2, numcolumns:=3)
 
        intTable = ActiveDocument.Tables.Count
        intRow = ActiveDocument.Tables(intTable).Rows.Count
 
    With Word.Selection
        If intRow > 1 Then
        objTable.Rows.LeftIndent = InchesToPoints(0.25)
    End If
        .Style = ActiveDocument.Styles("Desc")
        .Font.Size = 11
        .TypeText Text:=rs.Fields("WeeklyUpdDes").Value
        .MoveRight Unit:=wdCell
    End With
    Wend
    
    
    
    
    intTable = ActiveDocument.Tables.Count

Open in new window

0
 
dlc110161Commented:
Why don't you change your Style "Desc" so that it is a hanging indent? That way the paragraph will automatically take care of itself anytime the paragraph extends more than one line.

Dawn Bleuel
Word MVP
0
 
puppydogbuddyCommented:
If Dawn's comment does not resolve your poblem
I just noticed that begining with line 63  in your original code addresses the table properties, which I missed during the  first look at your code,
1.revert back to your original code
2.place this code between line 63 and 64
       intTable = ActiveDocument.Tables.Count
       intRow = ActiveDocument.Tables(intTable).Rows.Count
       If intRow > 1 Then
           .Rows.LeftIndent = InchesToPoints(0.25)
      End If

       

0
 
setalleyAuthor Commented:
Dawn,
Thank you so much...such a simple solution but the approach never even crossed my mind.  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now