VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "s_WordDataAccess"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

' TODO:
' AccessDatabase
  ' Check documents folder
' WordDatabaseAccess

Private fso_ As Scripting.FileSystemObject

Public Function Setup()
  ' Mandatory setup function
  Set fso_ = New Scripting.FileSystemObject
  On Error Resume Next
  Call fso_.DeleteFolder(Dev_GetTestFolder)
  On Error GoTo 0
  Call EnsureFolderExists(Dev_GetTestFolder & "\" & HT03_DEFAULT_WORKDIR)
End Function

Public Function Teardown()
  ' Mandatory teardown function
  Call fso_.DeleteFolder(Dev_GetTestFolder)
End Function

Public Function t_GetDatabaseDocument()
  On Error GoTo PROPAGATE_ERROR
  
  Dim sFile$: sFile = Dev_GetTestFolder & "\" & HT03_DEFAULT_WORKDIR & "\" & "reference.docx"
  Dim wordApp As New API_WordWrapper: Call wordApp.SetApplication(ThisDocument.Application)
  Dim doc As Word.Document: Set doc = wordApp.NewDocument()
  Call wordApp.SaveAs(sFile)
  
  Call Dev_ExpectNothing(GetDatabaseDocument(doc), "DB missing")
  
  Dim dbWrap As API_XLWrapper: Set dbWrap = Dev_GetDatabase
  Dim sFileDB$: sFileDB = dbWrap.Document.FullName
  Call dbWrap.ReleaseDocument
  
  Call Dev_NewCase("Parent folder DB")
  Dim dbWB As Excel.Workbook: Set dbWB = GetDatabaseDocument(doc)
  Call Dev_ExpectEQ(sFileDB, GetDatabaseDocument(doc).FullName)
  Call dbWB.Close(SaveChanges:=False)
  
  Call Dev_NewCase("Same folder DB")
  Dim sNewFileDB$: sNewFileDB = doc.Path & "\" & HT03_DBNAME
  Call fso_.MoveFile(sFileDB, sNewFileDB)
  Set dbWB = GetDatabaseDocument(doc)
  Call Dev_ExpectEQ(sNewFileDB, GetDatabaseDocument(doc).FullName)
  Call dbWB.Close(SaveChanges:=False)
  
  Call wordApp.ReleaseDocument
  
  Exit Function
PROPAGATE_ERROR:
  Call wordApp.ReleaseDocument
  Call Dev_LogError(Err.Number, Err.Description)
End Function
