Results 1 to 14 of 14

Thread: [RESOLVED] Binary file in table, problem With adding Row

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    6

    Resolved [RESOLVED] Binary file in table, problem With adding Row

    Hello all,

    I have code in Word macro which is reading file from the disk and converting it to binary. This binary should be inserted in the internal table (Dim As object) for further posting.

    Here is the code:
    Code:
    Sub Read_File(FileNameFull As String)
        Dim oBinaryDataTab As Object
        Dim oBinaryDataRow As Object
        Dim lBytesToRead As Long
        Dim iNumChars, i As Integer
        Dim s1022, s2044, sX As String
        Dim fs, f, ts As Object
        Dim ReadFile As String
    
    ' Actually does the work of uploading the document in 1022 byte pieces.
        ReadFile = 0
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set f = fs.GetFile(FileNameFull)
        Set ts = f.OpenAsTextStream(1, -2)
        lBytesToRead = f.Size
        ReadFile = f.Size
        Do While ts.AtEndOfStream <> True
            If lBytesToRead < 1022 Then
                iNumChars = lBytesToRead
            Else
                iNumChars = 1022
            End If
            s1022 = ts.Read(iNumChars)
            s2044 = ""
            For i = 1 To Len(s1022)
                sX = Hex$(CByte(Asc(Mid(s1022, i, 1))))
                If Len(sX) = 1 Then
                    sX = "0" + sX
                End If
                s2044 = s2044 + sX
            Next i
            Set oBinaryDataRow = oBinaryDataTab.Rows.Add
            oBinaryDataRow("LINE") = s2044
            lBytesToRead = lBytesToRead - iNumChars
        Loop
            
    End Sub
    But on the row "Set oBinaryDataRow = oBinaryDataTab.Rows.Add" code just stopped to work.
    Can somebody give me a hint how to proceed?
    I also tried to Dim oBinaryDataTable As Table and oBinaryDataRow as Row with the same result.

    TIA

    Gordan
    Last edited by Gordan0810; Jan 28th, 2009 at 06:17 AM.

  2. #2
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: Binary file in table, problem With adding Row

    An object in VBA is much like a variant in being that it represents and can store any data type that is of the type "object." That was a mouthful and probably not the best way to explain it. In essence an Object by itself is Nothing and can do nothing for you without first being assigned.

    You can't do this:

    Code:
    oBinaryDataTab.Rows.Add
    until you assign oBinaryDataTab with an object that supports the Rows.Add method. My question to you is what type of object is this data tab supposed to be?

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    6

    Re: Binary file in table, problem With adding Row

    Hello,

    it should be table with single column, and n rows.

    Gordan

  4. #4
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: Binary file in table, problem With adding Row

    I understand you want it to be a table, but what type of object are you wanting to represent this table as? Your choices are things like arrays, excel spreadsheets, database tables, or anything like that.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    6

    Re: Binary file in table, problem With adding Row

    Hello,

    sorry, this is an array.
    Problem is that system which will receive data can accept this only as internal table. In it one row is interpreted as "Uninterpreted sequence of bytes".

    Gordan
    Last edited by Gordan0810; Jan 23rd, 2009 at 10:06 AM.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Binary file in table, problem With adding Row

    you need to set oBinaryDataTab to be a table in your word document, at least i think that is what you want to do, otherwise you are not keeping the data anywhere, that i can see
    maybe
    set oBinaryDataTab = activedocument.tables.add 'with parameters for single column and row, headers etc
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: Binary file in table, problem With adding Row

    It's an array, westconn.

    You manage array dimensions like this.

    Code:
    Dim arr() as string 'declare uninitialized array
    ReDim arr(0) as string 'init array to 1 dimensions
    arr(0) = "text"
    ReDim Preserve arr(1) as string 'preserve the old values in the array
    arr(1) = "new text"
    Debug.Print arr(0), arr(1)

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Binary file in table, problem With adding Row

    i saw that he said that, but
    This binary should be inserted in the internal table (Dim As object)
    and oBinaryDataTab has some properties that look like a table
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    6

    Re: Binary file in table, problem With adding Row

    Hello all,

    thank you for all of your responses.

    To clarify some things, oBinaryDataTab is internal table which is not represented in word as text. Content of the table is not visible to word user. This "table" is needed to send content of the file from file system in another program.

    Because file is bigger (longer) than s1022 string it must be put in internal table with one column named "LINE". Number of rows is dynamic and depend on length of file on the disk.

    When received in another system this table will be represented as "Uninterpreted sequence of bytes".

    This code was OK before
    Set oBinaryDataRow = oBinaryDataTab.Rows.Add
    oBinaryDataRow("LINE") = s2044
    When both oBinaryDataTab and Row were defined as Objects.

    Now through debug mode I can see only that word is stopping on the first line without any error message.

    Appreciated any response,

    Gordan

  10. #10
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Binary file in table, problem With adding Row

    No one can understand what is "internal table".
    With your code in post#1:

    with just
    Dim oBinaryDataTab As Object
    you cannot use
    Set oBinaryDataRow = .Rows.Add
    oBinaryDataRow("LINE") = s2044

    if before that you do not have a line that similar to this:
    Set oBinaryDataTab = ... '-- whatever you call "internal table"
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Binary file in table, problem With adding Row

    you can add a row to a table, once the table has been set to the object, but the "Line" item (probably named range or bookmark) will also not exist unless added in code and may be a read only property

    either you have copied some incomplete code or the items were set in some other part of the code
    or your instructions are not complete
    Last edited by westconn1; Jan 26th, 2009 at 06:04 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  12. #12

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    6

    Re: Binary file in table, problem With adding Row

    Hello all,

    again thank you very much for helping me. You all understand that I am not very familiar with VBA, but some big german ERP company which has declarations not familiar in outher world

    anhn was right, set oBinaryDataTab = "table", was missing. After I defined this it started to work, but...

    this part of the code
    Set oBinaryDataRow = oBinaryDataTab.Rows.Add
    oBinaryDataRow("LINE") = s2044

    as I understand it should be doing following:
    1. add row (oBinaryDataRow) to table (oBinaryDataTab)
    2. fill new row with value in s2044.

    As you can see this two lines of the code are in loop, so until there is nothing in s2044, new row sould be added to table and value in s2044 added to it.

    Am I right or something is missing because oBinaryDataTab is empty even in the first pass.

    TIA

    Gordan

    P.S. I will reward anhn post as soon as I discover how

  13. #13
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Binary file in table, problem With adding Row

    yes but as i indicated before "LINE" must be a valid item property name of the table, is it adding a new row each time it loops, but not filling it?i am also not sure what table (word or other) you are using
    if it is a word table try like
    vb Code:
    1. Set oBinaryDataRow = oBinaryDataTab.Rows.Add
    2. oBinaryDataRow.cells(1) = s2044
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  14. #14

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    6

    Re: [RESOLVED] Binary file in table, problem With adding Row

    Hello,

    thank you all on received responses. all of them helped me to learn something new, and on the end I achieved result that I expected.

    There were some other errors in String (s2044) definition but I resolved it and whole function is OK.

    Best regards,

    Gordan

Posting Permissions

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



Click Here to Expand Forum to Full Width