SaveAsText acMacro - resulting file contains bad hexadecimal characters

evertVB

Registered User.
Local time
Today, 10:29
Joined
Sep 21, 2010
Messages
21
I'm doing the following in VBA:
Open another database:
Code:
[SIZE=2]Private Sub OpenTargetDB()[/SIZE]
[SIZE=2]Set accapp = New Access.Application[/SIZE]
[SIZE=2]accapp.OpenCurrentDatabase (TargetDBName)[/SIZE]
[SIZE=2]'accapp.Visible = True[/SIZE]
[SIZE=2]End Sub[/SIZE]
Then pick some macro from the other target database and export it to a textfile:
Code:
[SIZE=2]Dim i As Integer[/SIZE]
[SIZE=2]Dim obj[/SIZE]
 
[SIZE=2]For Each obj In accapp.CurrentProject.AllMacros[/SIZE]
[SIZE=2]If obj.Name = myMacroName Then[/SIZE]
[SIZE=2]FileName = ThisDbPath & "Macro_" & obj.Name & ".txt"[/SIZE]
[SIZE=2]FileNameo = ThisDbPath & "Macro_" & obj.Name & "_report.txt"[/SIZE]
[SIZE=2]accapp.SaveAsText acMacro, obj.Name, FileName[/SIZE]
[SIZE=2]End If[/SIZE]
[SIZE=2]Next[/SIZE]

Now I look into the created file with notepad and it looks as expected:
Code:
[SIZE=2]Version =196611[/SIZE]
[SIZE=2]ColumnsShown =0[/SIZE]
[SIZE=2]Begin[/SIZE]
[SIZE=2]Action ="SetWarnings"[/SIZE]
[SIZE=2]Argument ="0"[/SIZE]
[SIZE=2]End[/SIZE]
[SIZE=2]Begin[/SIZE]
[SIZE=2]Action ="OpenQuery"[/SIZE]
[SIZE=2]Argument ="create_01_bedraga"[/SIZE]
[SIZE=2]Argument ="0"[/SIZE]
[SIZE=2]Argument ="1"[/SIZE]
[SIZE=2]End[/SIZE]
 
[SIZE=2]... etc etc[/SIZE]

Now I look into the created file with a hex editor UltraEdit - see screenshot.


How did the bad characters hex FF, FE, 00 end up in the file??
NOTE: when using SaveAsText within the CURRENT database, I'm not experiencing this problem.

attachment.php
 

Attachments

  • hexview.jpg
    hexview.jpg
    29.9 KB · Views: 1,348
Now I look into the created file with a hex editor UltraEdit - see screenshot.

Sort of looks like files created with UEX during their beta process for the original Linux release of UEX! ;) In one beta version, something happened and the default file format got flipped from UTF-8 to something with a 16 in it, and new files would re-open as binary glop! (While editing existing files was safe... thus very bazaar to track down.)

So, what are you actually trying to accomplish with your code? An automated export of all VBA in a database? (Forms / Reports / Classes / Modules?)
 
So, what are you actually trying to accomplish with your code? An automated export of all VBA in a database? (Forms / Reports / Classes / Modules?)
After this some other VBA would parse the exported macro-code, and ultimately make a nice report of all queries executed from the specified macro. This whole thing worked nicely until now that I run it from one DB targeting a different DB.

Looks like I could use some VBA code that could remove the wrong characters from the file.
 
Looks like I could use some VBA code that could remove the wrong characters from the file.

This is accomplished by the following VBA code:
Code:
Option Compare Database
Option Explicit
 
Sub TestFunction()
  MsgBox "fn_ReadWriteStream started"
  If fn_ReadWriteStream("C:\Project\Utilities\Access en VB\Macro2SQLtext\Macro_mcr_all_qry.txt") = True Then
      MsgBox "fn_ReadWriteStream succeeded"
    Else
      MsgBox "fn_ReadWriteStream failed"
  End If
End Sub
Public Function fn_ReadWriteStream(pFileName As String) As Boolean
    Dim fname As String
    Dim fname2 As String
    Dim fnr As Integer
    Dim fnr2 As Integer
    Dim tstring As String * 1
    Dim i As Integer
    Dim ByteCount As Integer
 
 
        fn_ReadWriteStream = False
 
        fname = pFileName
        fname2 = pFileName & ".clean.txt"
 
        fnr2 = FreeFile()
        Open fname2 For Binary Lock Read Write As #fnr2
        fnr = FreeFile()
        Open fname For Binary Access Read As #fnr
        Do
            Get #fnr, , tstring
            If EOF(fnr) Then Exit Do
 
 
            ByteCount = ByteCount + 1
 
            'If ByteCount < 10 Then
            '   MsgBox Asc(tstring)
            'End If
 
 
            If Asc(tstring) = 254 Or _
               Asc(tstring) = 255 Or _
               Asc(tstring) = 0 Then
              Else
               Put #fnr2, , tstring
            End If
 
        Loop
        Close #fnr
        Close #fnr2
 
        fn_ReadWriteStream = True
End Function
 

Users who are viewing this thread

Back
Top Bottom