|
-
Jan 23rd, 2009, 07:41 AM
#1
Thread Starter
New Member
[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.
-
Jan 23rd, 2009, 09:04 AM
#2
Fanatic Member
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?
-
Jan 23rd, 2009, 09:14 AM
#3
Thread Starter
New Member
Re: Binary file in table, problem With adding Row
Hello,
it should be table with single column, and n rows.
Gordan
-
Jan 23rd, 2009, 09:22 AM
#4
Fanatic Member
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.
-
Jan 23rd, 2009, 09:58 AM
#5
Thread Starter
New Member
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.
-
Jan 24th, 2009, 03:20 AM
#6
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
-
Jan 24th, 2009, 12:39 PM
#7
Fanatic Member
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)
-
Jan 24th, 2009, 04:25 PM
#8
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
-
Jan 26th, 2009, 01:54 AM
#9
Thread Starter
New Member
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
-
Jan 26th, 2009, 02:17 AM
#10
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"
-
Jan 26th, 2009, 05:56 AM
#11
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
-
Jan 26th, 2009, 10:17 AM
#12
Thread Starter
New Member
-
Jan 26th, 2009, 03:35 PM
#13
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:
Set oBinaryDataRow = oBinaryDataTab.Rows.Add 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
-
Jan 28th, 2009, 06:19 AM
#14
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|