OA_Documents.bas 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. Option Compare Database
  2. Option Private Module
  3. Option Explicit
  4. 'Dim UTF8CONVERSION As Boolean
  5. 'Public Sub activate_Utf8Conversion()
  6. ' UTF8CONVERSION = True
  7. 'End Sub
  8. Const UTF8CONVERSION = True
  9. Public Function get_container_name(ByVal acType As Integer)
  10. 'return the name of an access object container from its acType
  11. Select Case acType
  12. Case acTable
  13. get_container_name = "tables"
  14. Case acForm
  15. get_container_name = "forms"
  16. Case acReport
  17. get_container_name = "reports"
  18. Case acMacro
  19. get_container_name = "scripts"
  20. Case acModule
  21. get_container_name = "modules"
  22. End Select
  23. End Function
  24. ' Export a database object with optional UCS2-to-UTF-8 conversion.
  25. Public Sub ExportDocument(ByVal acType As Integer, ByVal obj_name As String, ByVal file_path As String)
  26. On Error GoTo err
  27. ' encoding can be either 'UCS2' (default), either 'utf-8'
  28. logger "ExportDocument", "DEBUG", "Try to export " & obj_name & "(type " & acType & ") from: " & file_path
  29. mktree parent_dir(file_path)
  30. del_if_exist file_path
  31. Application.SaveAsText acType, obj_name, file_path
  32. If acType <> acModule Then
  33. If UTF8CONVERSION Then
  34. logger "ExportDocument", "DEBUG", "Encode file in UTF-8"
  35. Dim tempFileName As String
  36. tempFileName = TempFile()
  37. ConvertUcs2Utf8 file_path, tempFileName
  38. Kill file_path
  39. Name tempFileName As file_path
  40. End If
  41. SanitizeFile file_path
  42. End If
  43. logger "ExportDocument", "DEBUG", "> exported"
  44. Exit Sub
  45. err:
  46. logger "ExportDocument", "CRITICAL", "Unable to export " & obj_name & " [" & err.Description & "]"
  47. End Sub
  48. ' Import a database object with optional UTF-8-to-UCS2 conversion.
  49. Public Sub ImportDocument(ByVal acType As Integer, ByVal obj_name As String, ByVal file_path As String)
  50. On Error GoTo err
  51. Dim tempFileName As String
  52. logger "ImportDocument", "DEBUG", "Try to import " & obj_name & "(type " & acType & ") from: " & file_path
  53. If acType <> acModule Then
  54. If UTF8CONVERSION Then
  55. logger "ImportDocument", "DEBUG", "Encode in UCS2 before import"
  56. tempFileName = TempFile()
  57. ConvertUtf8Ucs2 file_path, tempFileName
  58. file_path = tempFileName
  59. End If
  60. End If
  61. Application.LoadFromText acType, obj_name, file_path
  62. logger "ImportDocument", "DEBUG", "> imported"
  63. end_:
  64. If Len(tempFileName) > 0 Then
  65. del_if_exist tempFileName
  66. End If
  67. Exit Sub
  68. err:
  69. logger "ImportDocument", "CRITICAL", "Unable to import " & obj_name & " [" & err.Description & "]"
  70. End Sub