View Full Version : complex tables
najam_ftp
10-11-2006, 01:57 AM
Hi, 
I have developed a macro that can read word doc save all its info in form of xml, and another macro with read that xml and create doc for me.
all is going well except complex tables, i am not able to read out their height and width (as no. of rows and columns are not uniform through out table).
if any one can help
thanks
fumei
10-11-2006, 04:45 AM
More information please.
Post your code.
mdmackillop
10-11-2006, 04:49 AM
Hi,
Welcome to VBAX
You can post a sample document containing your table using Manage Attachments in the Go Advanced section.  You'll probably have to zip it first.
najam_ftp
10-11-2006, 08:46 PM
Sub xml_create()
    Dim i, j, num As Integer
    
    num = 1
    
    Dim odoc As New DOMDocument
    Dim oEle As IXMLDOMElement
    Dim oRoot As IXMLDOMElement
    Dim onode, oNode1 As IXMLDOMNode
    
    
    Set odoc = New DOMDocument
    
    odoc.resolveExternals = True
    
    Set onode = odoc.createProcessingInstruction("xml", "version='1.0'")
    Set onode = odoc.InsertBefore(onode, odoc.childNodes.Item(0))
    Set oRoot = odoc.createElement("table")
    Set odoc.documentElement = oRoot
    
    Set oNode1 = odoc.createElement("no_row_of_table")
    oNode1.Text = ActiveDocument.Range.Tables(num).Range.Information(wdMaximumNumberOfRows)
    oRoot.appendChild oNode1
    
    Set oNode1 = odoc.createElement("no_col_width_of_table")
    oNode1.Text = ActiveDocument.Range.Tables(num).Range.Information(wdMaximumNumberOfColumns )
    oRoot.appendChild oNode1
    
    For i = 1 To ActiveDocument.Range.Tables(num).Range.Cells.Count
        Set onode = odoc.createElement("cells")
        Set oNode1 = odoc.createElement("height")
        
        oNode1.Text = ActiveDocument.Range.Tables(num).Range.Cells(i).Height
        onode.appendChild oNode1
        
        Set oNode1 = odoc.createElement("width")
        
        oNode1.Text = ActiveDocument.Range.Tables(num).Range.Cells(i).Width
        onode.appendChild oNode1
        
        Set oNode1 = odoc.createElement("text")
        
        oNode1.Text = ActiveDocument.Range.Tables(num).Range.Cells(i).Range.Text
        onode.appendChild oNode1
        oRoot.appendChild onode
        
        'ActiveDocument.Range.Tables(1).Range.Cells(i).Range.Select
        
        'If Selection.IsEndOfRowMark = True Then
            'Set oNode1 = odoc.createElement("end_of_row")
            'oNode1.Text = ActiveDocument.Range.Tables(num).Range.Cells(i).Range.Text
            'onode.appendChild oNode1
        
            oRoot.appendChild onode
        'End If
    Next i
    
    odoc.Save ("c:\najam1.xml")
End Sub
Sub read_xml()
    Dim h, w, r, c, i As Integer
    Dim str As String
    Dim bol As Boolean
    Dim odoc As New DOMDocument
    
    odoc.Load ("c:\najam1.xml")
    
    Dim oEle As IXMLDOMElement
    Dim oRoot As IXMLDOMElement
    Dim onode, oNode1 As IXMLDOMNode
    
    Set oRoot = odoc.documentElement
    
    For Each onode In oRoot.childNodes
        If onode.nodeName = "no_row_of_table" Then
            r = CInt(onode.Text)
            'MsgBox (r)
        End If
    
        If onode.nodeName = "no_col_width_of_table" Then
            c = CInt(onode.Text)
            'MsgBox (c)
        End If
    
        If (onode.nodeName <> "no_row_of_table") And (onode.nodeName <> "no_col_width_of_table") Then
            For Each oNode1 In onode.childNodes
                If oNode1.nodeName = "height" Then
                    h = CInt(oNode1.Text)
                End If
                
                If oNode1.nodeName = "width" Then
                    w = CInt(oNode1.Text)
                End If
                
                If oNode1.nodeName = "text" Then
                    str = oNode1.Text
                End If
            Next
    
            i = i + 1
    
            Call create_table(h, w, str, bol, r, c, i)
        End If
    Next
End Sub
Sub create_table(ByVal h, ByVal w, ByVal str, ByRef bol, ByVal r, ByVal c, ByVal i)
    If bol = False Then
        Documents.Add Template:="Normal"
    
        Application.WindowState = wdWindowStateMaximize
    
        ActiveDocument.Range.Tables.Add Range:=Selection.Range, _
            numrows:=r, numcolumns:=c, _
            DefaultTableBehavior:=wdWord9TableBehavior, _
            AutoFitBehavior:=wdAutoFitWindow
    
        bol = True
    End If
    
    ActiveDocument.Range.Tables(1).Range.Cells(i).Height = h
    ActiveDocument.Range.Tables(1).Range.Cells(i).Width = w
    ActiveDocument.Range.Tables(1).Range.Cells(i).Range.Text = str
    ActiveDocument.SaveAs ("c:\najam11111.doc")
End Sub
 
 
attachment has two table my macro does well for first but fails for second table
 
Edited 13-Oct-06 by geekgirlau. Reason: insert vba tags
fumei
10-11-2006, 09:15 PM
If you were trying to post some sort of image...it did not work.
OK.  Working with NON-uniform rows and columns in Word tables is tricky.  Is there any way to avoid it?  If it is possible...then do that.  Why do they need to be non-uniform?
Are you aware that "i" inActiveDocument.Range.Tables(1).Range.Cells(i).height = his the count of cells?  If i = 5, it is the fifth cell.  The code will set the height for that cell ONLY.
najam_ftp
10-11-2006, 09:36 PM
but problem is when no of colums and rows are different at different places in table and we read out height or width of such cell it comes 9999999 and you cant use it
fumei
10-13-2006, 07:35 AM
we read out height or width of such cell it comes 9999999Explain that, AND post code where you are doing this.  There is no sign of such code in what you have posted so far.
NOTE!  if the code you are using uses any range of the Selection object, AND you have multiple cells in that Selection, then yes, you will get 9999999.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.