Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-27-2011, 12:21 PM
Jaymond Flurrie Jaymond Flurrie is offline Excel data to Word table Windows 7 64bit Excel data to Word table Office 2010 32bit
Novice
Excel data to Word table
 
Join Date: Feb 2011
Posts: 13
Jaymond Flurrie is on a distinguished road
Default Excel data to Word table

I have a 3D array, which I create in Excel. The first dimension indicates the file number, the second indicates the column and the third indicates the row (should be vice versa, I know). I try to "print" each first dimension to a Word table, by creating a Word document, creating a table there and printing everything in the particular 2D array into the table.

So for example if I have (3,2,4) it means I have three Word documents, two columns X four rows sized table. The actual number of rows in each table will be much smaller than the maximum number, because I check whether a row has anything to do with this particular 2D array and if doesn't, I just skip over it. The second dimension is always 2.

I know the creation of the array works fine since I have tested it by making a new Excel workbook where I make a sheet for each 2D array.



I've got this up to the point where my lack of understanding of Word VBA becomes the limit. Here's what I have tried:

Code:
Sub ControlWord(vPalautteet As Variant)
    Dim appWD As Word.Application
    Dim lEsMiesKom As Long 'The actual row counter in the particular Word table
    Dim wdRngTable As Word.Range   'create a range variable
    Dim j As Integer
    Dim i As Integer
    
    ' Open and show Word
    Set appWD = CreateObject("Word.Application.14")
    appWD.Visible = True

    'Loop thru the files
    For j = LBound(vPalautteet, 1) To UBound(vPalautteet, 1) - 1
        lEsMiesKom = 1 'The number of row for this particular document
        
        ' Tell Word to create a new document
        appWD.Documents.Add
    
        'If table is at end of contents
        Set wdRngTable = ActiveDocument.Content
        wdRngTable.Collapse Direction:=wdCollapseEnd
        
        'Create The Table with one row and two columns
        ActiveDocument.Tables.Add wdRngTable, 1, 2
        ActiveDocument.Tables(1).PreferredWidth = InchesToPoints(10#)
        ActiveDocument.Tables(1).Range.Font.Size = 10
        ActiveDocument.Tables(1).Range.Font.Name = "Arial"
        
        'adjust the column width in inches
        ActiveDocument.Tables(1).Columns(1).Width = InchesToPoints(2#)
        ActiveDocument.Tables(1).Columns(2).Width = InchesToPoints(2#)
        
        'Loop thru the table rows
        For i = LBound(vPalautteet, 3) + 1 To UBound(vPalautteet, 3)
            If vPalautteet(j, 0, i) = vbNullString Then
                'Skip over
            Else
                'Write contents to the table
                lEsMiesKom = lEsMiesKom + 1
                
                ActiveDocument.Tables(1).Range.Cells(lEsMiesKom, 1).Range Text:=vPalautteet(j, 1, i) 'This gives an error
                ActiveDocument.Tables(1).Range.Cells(lEsMiesKom, 2).Range Text:=vPalautteet(j, 2, i)
            End If
        Next i
        
        ' Save the new document with a sequential file name
        appWD.ActiveDocument.SaveAs Filename:=vPalautteet(j, 0, 0)
        
        ' Close this new word document
        appWD.ActiveDocument.Close
    Next j
    
    ' Close the Word application
    appWD.Quit
End Sub
The error comes on the row I try to write to the table. What's wrong? I took the part almost directly from a recorded macro and it seems to work there.

Thanks for any help!
Reply With Quote
  #2  
Old 02-27-2011, 07:37 PM
macropod's Avatar
macropod macropod is online now Excel data to Word table Windows 7 32bit Excel data to Word table Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Hi Jaymond,

What is the error message? and what do you get if you use the line:
MsgBox vPalautteet(j, 1, i)
just before the error line? If this gives the same error, that suggests there is a problem with the way you're referencing vPalautteet
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 02-27-2011, 07:42 PM
Jaymond Flurrie Jaymond Flurrie is offline Excel data to Word table Windows 7 64bit Excel data to Word table Office 2010 32bit
Novice
Excel data to Word table
 
Join Date: Feb 2011
Posts: 13
Jaymond Flurrie is on a distinguished road
Default

It was actually about using the cells wrong. So yes, this is solved.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel data to Word table Use changeable table data in doc text? rtlight Word Tables 4 02-25-2011 07:29 PM
inserting a string of data into an MS Word table??? matto Word VBA 0 07-16-2010 09:35 AM
pivot table source data hannu Excel 0 07-03-2010 04:54 AM
Excel pivot table into an MS Word Doc "office 2007" wmarsh3561 Excel 1 01-09-2010 08:03 PM
Automate Data from Excel into Word DropDown...? sigraves Word VBA 0 08-03-2009 06:54 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:46 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft