OA_Documents.bas 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. Option Compare Database
  2. Option Private Module
  3. Option Explicit
  4. Public Function utf8_conversion() As Boolean
  5. ' utf8 conversion is needed for Access 2007 and later
  6. utf8_conversion = (CurrentProject.FileFormat >= acFileFormatAccess2007)
  7. End Function
  8. Public Function get_container_name(ByVal acType As Integer)
  9. 'return the name of an access object container from its acType
  10. Select Case acType
  11. Case acTable
  12. get_container_name = "tables"
  13. Case acForm
  14. get_container_name = "forms"
  15. Case acReport
  16. get_container_name = "reports"
  17. Case acMacro
  18. get_container_name = "scripts"
  19. Case acModule
  20. get_container_name = "modules"
  21. End Select
  22. End Function
  23. ' Export a database object with optional UCS2-to-UTF-8 conversion.
  24. Public Sub ExportDocument(ByVal acType As Integer, ByVal obj_name As String, ByVal file_path As String)
  25. On Error GoTo err
  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 utf8_conversion() 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", "> exported"
  43. Exit Sub
  44. err:
  45. logger "ExportDocument", "CRITICAL", "Unable to export " & obj_name & " [" & err.Description & "]"
  46. End Sub
  47. ' Import a database object with optional UTF-8-to-UCS2 conversion.
  48. Public Sub ImportDocument(ByVal acType As Integer, ByVal obj_name As String, ByVal file_path As String)
  49. On Error GoTo err
  50. Dim tempFileName As String
  51. logger "ImportDocument", "DEBUG", "Try to import " & obj_name & "(type " & acType & ") from: " & file_path
  52. If acType <> acModule Then
  53. If utf8_conversion() Then
  54. logger "ImportDocument", "DEBUG", "Encode in UCS2 before import"
  55. tempFileName = TempFile()
  56. ConvertUtf8Ucs2 file_path, tempFileName
  57. file_path = tempFileName
  58. End If
  59. End If
  60. Application.LoadFromText acType, obj_name, file_path
  61. logger "ImportDocument", "DEBUG", "> imported"
  62. end_:
  63. If Len(tempFileName) > 0 Then
  64. del_if_exist tempFileName
  65. End If
  66. Exit Sub
  67. err:
  68. logger "ImportDocument", "CRITICAL", "Unable to import " & obj_name & " [" & err.Description & "]"
  69. End Sub