dcsimg
Results 1 to 2 of 2

Thread: [RESOLVED] Creating Table in Word from string array

  1. #1

    Thread Starter
    Lively Member dprontnicki's Avatar
    Join Date
    Sep 2016
    Posts
    78

    Resolved [RESOLVED] Creating Table in Word from string array

    Hi I have a method that creates a table in Word and populates it from a string array. The problem I am having is it throws a (Exception from HRESULT: 0x800706BE) and closes word when run.

    BUT, if I put a break point on any line in the method and step through the populating of the table it works just fine.

    I am also willing to change any of this if there is a better way. This is my first time trying to create a table in word.

    Code:
        Private Sub CreateTable(ByVal userInfo As String())
    
            wordTable = wordDocument.Tables.Add(wordDocument.Bookmarks.Item("\endofdoc").Range, userInfo.Count, 6)
            wordTable.Range.ParagraphFormat.SpaceAfter = 6
    
            For row As Integer = 0 To userInfo.Count - 1
    
                Dim userInfoRaw As String = userInfo(row)
                Dim userInfoRawArray As String() = userInfoRaw.Split(", ")
    
                Dim user As String = userInfoRawArray(0)
                Dim checkOutLength As String = userInfoRawArray(1)
                Dim computerNumber As String = userInfoRawArray(2)
                Dim checkOutDay As String = userInfoRawArray(3)
                Dim checkOutDate As String = userInfoRawArray(4)
                Dim checkOutTime As String = userInfoRawArray(5)
    
                wordTable.Cell(row, 1).Range.Text = user
                wordTable.Cell(row, 2).Range.Text = checkOutLength
                wordTable.Cell(row, 3).Range.Text = computerNumber
                wordTable.Cell(row, 4).Range.Text = checkOutDay
                wordTable.Cell(row, 5).Range.Text = checkOutDate
                wordTable.Cell(row, 6).Range.Text = checkOutTime
    
            Next
    
        End Sub
    Also, when I step through and it works, the first string ends up at the end and only the username is populated. Not sure what is causing that. But they may be related.

    UPDATE: So I think I know what the issue; because the first item in the array is "0", word for some reason thinks its null. But I'm not sure how to get around that.
    Last edited by dprontnicki; May 17th, 2019 at 02:27 PM. Reason: Added More info

  2. #2

    Thread Starter
    Lively Member dprontnicki's Avatar
    Join Date
    Sep 2016
    Posts
    78

    Re: Creating Table in Word from string array

    OK So I figured it out. I was correct, word does not like that the first array is "0". So I changed the approach slightly.

    Instead of:
    Code:
    For row As Integer = 0 To userInfo.Count - 1
    I did:

    Code:
    Dim row As Integer = 1
    
            For Each user In userInfo
    
    ....
    
    row += 1
    This works, it's a little slower for whatever reason, but it is working. I figured I would post in case anyone else ran into the same issue.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width