The first thing I did was to look in AndrewMacro.odt, the famous macro
document by Andrew Pitonyak. I found this example, and I actually also
found the same example in the OpenOffice.org BASIC help section, that
seems to be broken though (I added line numbers to it, so I can easier
refer to it later):
00. '?? This is broken!
01. Sub ExampleRandomAccess
02.   Dim iNumber As Integer, aFile As String
03.   Dim sText As Variant REM Must be a variant
04.   aFile = "c:\data1.txt"
05.   iNumber = Freefile
06.   Open aFile For Random As #iNumber Len=5
07.   Seek #iNumber,1 REM Position at beginning
08.   Put #iNumber,, "1234567890" REM Fill line with text
09.   Put #iNumber,, "ABCDEFGHIJ"
10.   Put #iNumber,, "abcdefghij"
11.   REM This is how the file looks now!
12.   REM 08 00 0A 00 31 32 33 34  35 36 37 38 39 30 08 00   ....1234567890..
13.   REM 0A 00 41 42 43 44 45 46  47 48 49 4A 08 00 0A 00   ..ABCDEFGHIJ....
14.   REM 61 62 63 64 65 66 67 68  69 6A 00 00 00 00 00 00   abcdefghij
15.   REM
16.   Seek #iNumber,1
17.   Get #iNumber,,sText
18.   Print "on open:" & sText
19.   Close #iNumber
20.   iNumber = Freefile
21.   Open aFile For Random As #iNumber Len=5
22.   Get #iNumber,,sText
23.   Print "reopened: " & sText
24.   Put #iNumber,,"ZZZZZ"
25.   Get #iNumber,1,sText
26.   Print "anoter get "& sText
27.   Get #iNumber,1,sText
28.   Put #iNumber,20,"This is the text in record 20"
29.   Print Lof(#iNumber)
30.   Close #iNumber
31. End Sub

Broken or not, maybe it will help me to understand this thing I
thought, so I tried it out.

I guess the brokenness in this is that the idea is that after
reopening in line 21, text should be added at the end of the file in
line 24, but in fact it isn't. After executing the macro, the file is
42 bytes with the text added in line 28 located at the end of the
file.

In line 06 and 21, does Len=5 mean that each ”record” is 5 bytes? In
that case there are not as many as 20 records in the file when line 28
is going to be executed, so it feels natural that the last text
appears at the end of the text.

Even if the text added in line 24 was added right after the text added
in line 08-10, there still wouldn't be as many as 20 records, would
it?

It would be interesting however, to know why the text added in line 24
ends up at the beginning of the file, making the earlier added text
disappear.

Anyway, for experimental purpose - I am a very beginner at this part
of macro programming, I rewrote the macro:
00. Sub ExampleRandomAccess2
01.   Dim iNumber As Integer, aFile As String
02.   Dim sText As Variant REM Must be a variant
03.   aFile = "~/data" REM I'm on Linux
04.   iNumber = Freefile
05.   Open aFile For Random As #iNumber Len=5
06.   Seek #iNumber,1 REM Position at beginning
07.   Put #iNumber,, "1234567890" REM Fill line with text
08.   Put #iNumber,, "ABCDEFGHIJ"
09.   Put #iNumber,, "abcdefghij"
10.   REM This is how the file looks now!
11.   REM 08 00 0A 00 31 32 33 34  35 36 37 38 39 30 08 00   ....1234567890..
12.   REM 0A 00 41 42 43 44 45 46  47 48 49 4A 08 00 0A 00   ..ABCDEFGHIJ....
13.   REM 61 62 63 64 65 66 67 68  69 6A 00 00 00 00 00 00   abcdefghij
14.   REM
15.   Seek #iNumber,1
16.   Get #iNumber,,sText
17.   Print "on open:" & sText
18.   Close #iNumber
19.   iNumber = Freefile
20.   Open aFile For Random As #iNumber Len=5
21.   Get #iNumber,,sText
22.   Print "reopened: " & sText
23.   Dim i As Integer, Msg As String
24.   For i=1 To 50
25.     Msg=Msg & "<" & Right("0" & LTrim(Str(i)),2) & ".>"
26.   Next i
27. '  Msg="ZZZZZ"
28. '  Put #iNumber,,"ZZZZZ"
29.   Put #iNumber,,Msg
30.   Get #iNumber,1,sText
31.   Print "anoter get "& sText
32.   Get #iNumber,1,sText
33.   Put #iNumber,20,"This is not the text in record 20"
34.   Print Lof(#iNumber)
35.   Close #iNumber
36. End Sub

Instead of the ”ZZZZZ” I added numbers so I could easily see
approximately where in the file things ends up. Msg will contain
”<01.><02.><03.><04.><05.><06.>… … <50.>” when the For loop at line
24-26 is done.
 If I comment lines 30 and 32 (due to errors, more about that soon)
and run the macro, the text at line 33 actually seems to end up at
record 20, so that part works obviously.

But a few more questions comes in mind when I run the macro as it is,
that is with the lines 30 and 32 intact. At line 30 the following
error message appears:
”BASIC runtime error - Device I/O error” (last line translated from
Swedish, so I don't know the exact sentence).

If I remove the comment from line 27, making Msg contain ”ZZZZ”, the
following message appears at line 31 (is that ”another” spelled wrong,
by the way?):
”anoter get -8.39322075940694E-45”

If uncommenting line 28 and commenting line 29, the following message
appears instead:
”another get ZZZZZ” which I assume is correct.

So for some reason I can't use a string variable instead of a string
like ”ZZZZZ”. Why is that?

Does anyone know if the ”brokenness” I was talking about is a bug in
OpenOffice.org or if the macro just needs to be rewritten somehow?
Since this example is in the OpenOffice.org BASIC help section I think
it really need to be corrected (or the bug fixed), because it's very
frustrating to learn from examples if the examples doesn't work…

Regards

Johnny Rosenberg
OpenOffice.org 3.2 RC1
Ubuntu 8.10

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@api.openoffice.org
For additional commands, e-mail: dev-h...@api.openoffice.org

Reply via email to