Results 1 to 4 of 4
  1. #1
    Dave_D's Avatar
    Dave_D is offline Advanced Beginner
    Windows XP Access 2010 64bit
    Join Date
    Aug 2015
    Posts
    67

    Creating a table in MS Word

    Using VBA code from within my MS Access DB, I'm trying to create a table in MS Word, however I'm getting a 450 run-time error - "Wrong number of arguments or invalid property assignment" on the following statement:

    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=4, NumColumns:=3, _
    DefaultTableBehavior:=wdWord9TableBehavior, AutofitBehavior:=wdAutoFitFixed



    This is the exact same code which the macro recorder created which it failing on.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    You used macro recorder in Word to generate code then pasted that code to a procedure in Access? Macro can help to generate code syntax but will usually have to be modified. Post the entire procedure for analysis. Something like:

    Code:
    Dim oWord As Word.Application
    Dim oDoc As Word.Document
    Dim oTbl As Word.Table
    Set oWord = CreateObject("Word.Application")
    oWord.Visible = True
    Set oDoc = oWord.Documents.Add
    Set oTbl = oDoc.Tables.Add(oDoc.Range(0, 0), 4, 3, wdWord9TableBehavior, wdAutoFitFixed)
    With oTbl
        If .Style <> "Table Grid" Then
            .Style = "Table Grid"
        End If
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = False
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = False
        .ApplyStyleRowBands = True
        .ApplyStyleColumnBands = False
        .Cell(1, 1).Range.Text = "abc"
        .Cell(1, 2).Range.Text = "def"
        .Cell(1, 3).Range.Text = "ghi"
    End With
    Review https://msdn.microsoft.com/en-us/lib.../ff845710.aspx

    Consider using a Word template document instead.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Dave_D's Avatar
    Dave_D is offline Advanced Beginner
    Windows XP Access 2010 64bit
    Join Date
    Aug 2015
    Posts
    67
    Thank you for you recent reply, June7. I checked out the link you provided. Are you able to explain how the RANGE is interrupted within the TABLE.ADD method. The link showed an example where range was set to (0,0) which indicated at the top of the document and I also tried the example wsCollpseEnd, but no empty table was made. Here's the entire procedure

    Public Sub CreateWordReport(oXl As Excel.Application,ExecSumRptMon As String, ExecSumRptYear As String)

    'Early binding method
    Dim wdapp As Word.Application
    Dim RptPeriod As String, DataPeriod As String
    Dim wsLastRow As Integer
    Dim wsLastColumn As Integer
    Set wdapp = New Word.Application
    Dim wsTotNbrAcctReviewed As String
    Dim wsNbrAcctCorrect As String
    Dim wsNbrAcctWErrors As String
    Dim wsErrors As String
    Dim Counter As Long
    Dim wsPctErrorDbl As Double
    Dim wsPctError As String
    Dim wsTopErrors As Byte
    Dim DischargeMonthSort As Byte
    Dim DischargeYear As String
    Dim TotAcct() As Variant
    Dim Deficiencies() As Variant
    Dim Dimension1 As Long, Dimension2 As Long


    With wdapp
    .Visible = True
    .Activate
    .Documents.Add

    With .Selection
    .Style =wdapp.ActiveDocument.Styles("Title")
    .Font.Name= "Cambria"
    .Font.Size= 26
    .TypeTextText:="Summary"
    .TypeParagraph
    .Font.Size= 16
    .Font.ColorIndex = wdBlue
    .BoldRun
    .TypeTextExecSumRptMon & " " & ExecSumRptYear & " PFS StaffReviews"
    End With

    oXl.Sheets("ExecSum01").Select
    oXl.Range("A2").Select

    wsLastRow =(oXl.Cells.SpecialCells(xlCellTypeLastCell).Row) - 2

    ReDim TotAcct(0 To wsLastRow, 0 To3)

    For Dimension1 = LBound(TotAcct, 1)To UBound(TotAcct, 1)
    For Dimension2 =LBound(TotAcct, 2) To UBound(TotAcct, 2)
    TotAcct(Dimension1, Dimension2) = oXl.Range("A2").Offset(Dimension1,Dimension2).Valu e
    Next Dimension2
    Next Dimension1

    oXl.Sheets("Sheet2").Select
    oXl.Range("A4").Select

    wsLastRow =(oXl.Cells.SpecialCells(xlCellTypeLastCell).Row) - 6

    ReDim Deficiencies(0 To wsLastRow, 0To 1)

    For Dimension1 =LBound(Deficiencies, 1) To UBound(Deficiencies, 1)
    For Dimension2 =LBound(Deficiencies, 2) To UBound(Deficiencies, 2)
    Deficiencies(Dimension1, Dimension2) =oXl.Range("A4").Offset(Dimension1, Dimension2).Value
    Next Dimension2
    Next Dimension1


    With .Selection
    .Font.Name = "Tahoma"
    .Font.Size = 12
    .Font.ColorIndex = wdBlack
    .TypeParagraph
    .TypeText Text:=TotAcct(0, 3)
    .TypeParagraph
    .BoldRun
    .TypeText Text:=TotAcct(0, 2) & " accounts were reviewed with "& Deficiencies(0, 1) & " deficiencies noted."
    .TypeParagraph
    End With

    ActiveDocument.Tables.AddRange:=Selection.Range, NumRows:=4, NumColumns:= _
    3,DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
    wdAutoFitFixed

    With Selection.Tables(1)
    If .Style<> "Table Grid" Then
    .Style = "Table Grid"
    End If
    .ApplyStyleHeadingRows= True
    .ApplyStyleLastRow = False
    .ApplyStyleFirstColumn = True
    .ApplyStyleLastColumn = False
    .ApplyStyleRowBands = True
    .ApplyStyleColumnBands = False
    End With

    End With

    End Sub

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    You want to pull data from Excel and write to table in Word and code is in Access? This is getting quite complicated and I don't want to build the structure to test this. I've never had any requirement like this. My little code snippet worked for me and was result of some quick research.

    I can only suggest you research and debug, debug and debug. Set breakpoints and step through code.

    Why are Excel and Word involved? Why is data not in Access and use Access report?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 03-20-2016, 02:13 PM
  2. Error when creating mail merge doc or any word doc
    By maxmaggot in forum Programming
    Replies: 5
    Last Post: 08-25-2013, 01:35 PM
  3. Creating Report using Word Mail Merge
    By JoeM in forum Reports
    Replies: 1
    Last Post: 02-20-2013, 11:32 AM
  4. Creating Links to Word Docs
    By kingharvest in forum Access
    Replies: 1
    Last Post: 05-19-2010, 10:28 PM
  5. Creating report from Word file
    By jonny in forum Reports
    Replies: 0
    Last Post: 10-15-2009, 03:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums