OA_Documents.bas 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. ' encoding can be either 'UCS2' (default), either 'utf-8'
  27. logger "ExportDocument", "DEBUG", "Try to export " & obj_name & "(type " & acType & ") from: " & file_path
  28. mktree parent_dir(file_path)
  29. del_if_exist file_path
  30. Application.SaveAsText acType, obj_name, file_path
  31. If acType <> acModule Then
  32. If UTF8CONVERSION Then
  33. logger "ExportDocument", "DEBUG", "Encode file in UTF-8"
  34. Dim tempFileName As String
  35. tempFileName = TempFile()
  36. ConvertUcs2Utf8 file_path, tempFileName
  37. Kill file_path
  38. Name tempFileName As file_path
  39. End If
  40. SanitizeFile file_path
  41. End If
  42. logger "ExportDocument", "DEBUG", obj_name & " (type " & acType & ") exported to " & file_path
  43. End Sub
  44. ' Import a database object with optional UTF-8-to-UCS2 conversion.
  45. Public Sub ImportDocument(ByVal acType As Integer, ByVal obj_name As String, ByVal file_path As String)
  46. On Error GoTo err
  47. Dim tempFileName As String
  48. logger "ImportDocument", "DEBUG", "Try to import " & obj_name & "(type " & acType & ") from: " & file_path
  49. If acType <> acModule Then
  50. If UTF8CONVERSION Then
  51. logger "ImportDocument", "DEBUG", "Encode in UCS2 before import"
  52. tempFileName = TempFile()
  53. ConvertUtf8Ucs2 file_path, tempFileName
  54. file_path = tempFileName
  55. End If
  56. End If
  57. Application.LoadFromText acType, obj_name, file_path
  58. logger "ImportDocument", "DEBUG", "> imported"
  59. end_:
  60. If Len(tempFileName) > 0 Then
  61. del_if_exist tempFileName
  62. End If
  63. Exit Sub
  64. err:
  65. logger "ImportDocument", "CRITICAL", "Unable to import " & obj_name & "[" & err.Description & "]"
  66. End Sub