Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
Anonymous
in reply to: Anonymous

Reasonably cleaned up version of final code:

Function func_ConvertFolderContentToJT(ByVal strSourceFolder As String, ByVal strSourceFileType As String, ByVal strDestinationFolder As String, ByVal strDestinationFileType As String, ByVal strConverterConfigFilePath As String) As Boolean
    func_ConvertFolderContentToJT = True
    Dim objFS As FileSystemObject
    Set objFS = CreateObject("Scripting.FileSystemObject")

    If Right(strSourceFolder, 1) <> "\" Then
        strSourceFolder = strSourceFolder & "\"
    End If
    If objFS.FolderExists(strSourceFolder) <> True Then
        func_ConvertFolderContentToJT = False
        Exit Function
    End If
    If strDestinationFolder <> "" Then
        If objFS.FolderExists(strDestinationFolder) <> True Then
            strDestinationFolder = False
        End If
        If Right(strDestinationFolder, 1) <> "\" Then
            strDestinationFolder = strDestinationFolder & "\"
        End If
    End If

    If strSourceFileType = "" Then
        func_ConvertFolderContentToJT = False
        Exit Function
    End If
    
    If strDestinationFileType = "" Then
        func_ConvertFolderContentToJT = False
        Exit Function
    End If
    
    Dim strToLookFor As String
    strToLookFor = strDestinationFileType
    
    If Left(strDestinationFileType, 1) = "." Then
        strToLookFor = Right(strDestinationFileType, Len(strDestinationFileType) - 1)
    Else
        strToLookFor = strDestinationFileType
        strDestinationFileType = "." & strDestinationFileType
    End If
    
    If objFS.FileExists(strConverterConfigFilePath) = False Then
        func_ConvertFolderContentToJT = False
        Exit Function
    End If
    
    Dim flg_TranslationAddInFound As Boolean
    flg_TranslationAddInFound = False
    Dim intAddIn As Long
    intAddIn = 1
    Do
        If InStr(1, UCase(ThisApplication.ApplicationAddIns.Item(intAddIn).Description), UCase(strToLookFor)) > 0 Then
            flg_TranslationAddInFound = True
            Exit Do
        End If
        intAddIn = intAddIn + 1
    Loop Until intAddIn = ThisApplication.ApplicationAddIns.Count + 1

    If flg_TranslationAddInFound <> True Then
        func_ConvertFolderContentToJT = False
        Exit Function
    End If
    
    Dim JTAddIn As TranslatorAddIn
    Set JTAddIn = ThisApplication.ApplicationAddIns.ItemById(ThisApplication.ApplicationAddIns.Item(intAddIn).ClientId)
   
    Dim oContext As TranslationContext
    Set oContext = ThisApplication.TransientObjects.CreateTranslationContext
            
    Dim oOptions As NameValueMap
    Set oOptions = ThisApplication.TransientObjects.CreateNameValueMap

    Dim oDataMedium As DataMedium
    Set oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
    
    Dim objFolder As Folder
    Dim objFile 'As File
    Set objFolder = objFS.GetFolder(strSourceFolder)
    Debug.Print "Convert Content of Folder: " & strSourceFolder
    For Each objFile In objFolder.Files
        Dim strFullFilePath As String
        strFullFilePath = objFile
        If Right(objFile, Len(strSourceFileType)) = strSourceFileType Then
            Dim oPartDoc As PartDocument
            Set oPartDoc = ThisApplication.Documents.Open(strFullFilePath)
            If JTAddIn.HasSaveCopyAsOptions(oPartDoc, oContext, oOptions) = True Then
                oOptions.Value("ConfigFilePath") = strConverterConfigFilePath
                oOptions.Value("ConfigFileEnabled") = True
                oContext.Type = kFileBrowseIOMechanism
                If strDestinationFolder <> "" Then
                    strFullFilePath = Replace(strFullFilePath, strSourceFolder, strDestinationFolder)
                End If
                oDataMedium.FileName = strFullFilePath & strDestinationFileType
                JTAddIn.SaveCopyAs oPartDoc, oContext, oOptions, oDataMedium
                oPartDoc.Close True
            Else
                If strDestinationFolder <> "" Then
                    strFullFilePath = Replace(strFullFilePath, strSourceFolder, strDestinationFolder)
                End If
                
                oPartDoc.SaveAs strFullFilePath & strDestinationFileType, True
                oPartDoc.Close True
            End If
        End If
    Next objFile
End Function