Hi,
I had a need to publish several hundred PDFs to Infoview and did not want to do it by hand. I wasn’t able to find any sample code, so I thought that what I wrote might be useful to someone else.
I wrote this as an Excel VBA macro, although it doesn’t actually use Excel.
The code recurses through a given UNC path, publishing all found files into Infoview, keeping the folder structure intact. I’ve only tested it with PDFs, but it should work with all other types.
Joe
Option Explicit
Dim pluginPDF As PluginInfo
Dim pluginExcel As PluginInfo
Dim pluginWord As PluginInfo
Dim pluginTxt As PluginInfo
Dim pluginRtf As PluginInfo
Dim pluginPowerpoint As PluginInfo
Dim pluginAgnostic As PluginInfo
Dim pluginFolder As PluginInfo
Sub loadAgnosticFiles()
Dim userName As String
Dim userPass As String
Dim server As String
Dim auth As String
' Define the starting UNC path to load files from
' IMPORTANT: string MUST end with a backslash
Dim pathStart As String
pathStart = "\\server\share\path\"
' Define the name of the BOE folder to be created
Dim folderNewReports As String
folderNewReports = "<folder name>"
' Define the existing BOE parent folder into which the new structure will be added
Dim folderExistingParentToPutNewReportsInto As Long
folderExistingParentToPutNewReportsInto = <parent id>
' BOE logon parameters
userName = ""
userPass = ""
server = ""
auth = ""
Dim oSessionMgr As CrystalEnterpriseLib.SessionMgr
Set oSessionMgr = New CrystalEnterpriseLib.SessionMgr
Dim oEnterpriseSession As CrystalEnterpriseLib.EnterpriseSession
Set oEnterpriseSession = oSessionMgr.Logon(userName, userPass, server, auth)
Dim oInfoStore As CrystalInfoStoreLib.InfoStore
Set oInfoStore = oEnterpriseSession.Service("", "InfoStore")
' Define all the plugin objects
Set pluginPDF = oInfoStore.PluginManager.PluginInfo("Pdf")
Set pluginExcel = oInfoStore.PluginManager.PluginInfo("Excel")
Set pluginWord = oInfoStore.PluginManager.PluginInfo("Word")
Set pluginTxt = oInfoStore.PluginManager.PluginInfo("Txt")
Set pluginRtf = oInfoStore.PluginManager.PluginInfo("Rtf")
Set pluginPowerpoint = oInfoStore.PluginManager.PluginInfo("Powerpoint")
Set pluginAgnostic = oInfoStore.PluginManager.PluginInfo("Agnostic")
Set pluginFolder = oInfoStore.PluginManager.PluginInfo("Folder")
Dim newInfoObjects As InfoObjects
Dim newInfoObject As InfoObject
' Create new InfoObjects collection
Set newInfoObjects = oInfoStore.NewInfoObjectCollection
' Loop
Call recurse(oInfoStore, newInfoObjects, pathStart, folderNewReports, folderExistingParentToPutNewReportsInto)
End Sub
' Sub: recurse
' What: Main loop - copies files from specified UNC folder into BOE
' In: InfoStore
' InfoObjects new IO collection created in main
' String UNC path to folder; this will grow upon each recursive call as
' we go further into the directory structure.
' String name of immediate parent folder. This is just used to entitle
' the BOE folder, so it can be different than the actual UNC
' path for the initial call.
' Long si_id of the folder into which this new folder will be added
'
' Out: PlugInInfo of the specified type -- "Agnostic" type if no match
Sub recurse(oInfoStore As InfoStore, oInfoObjects As InfoObjects, parentPath As String, parentName As String, parentID As Long)
Dim theFile As String
Dim newObject As InfoObject
Dim newParentID As Long
' Create the BOE folder, if it's not already there
newParentID = getObjectByName(oInfoStore, parentName, "Folder", parentID)
If newParentID = -1 Then
Dim newFolder As InfoObject
Set newFolder = oInfoObjects.Add(pluginFolder)
newFolder.Title = parentName
newFolder.parentID = parentID
newParentID = newFolder.ID
Call oInfoStore.Commit(oInfoObjects)
End If
' Get all files in this directory, add to BOE
theFile = Dir(parentPath, vbNormal)
While (theFile <> "")
Debug.Print "Uploading: " & parentPath & theFile
Set newObject = oInfoObjects.Add(getPlugIn(theFile))
newObject.Files.Add (parentPath & theFile)
newObject.parentID = newParentID
newObject.Title = Left(theFile, Len(theFile) - 4)
theFile = Dir()
Call oInfoStore.Commit(oInfoObjects)
Wend
Dim subFolders As Collection
Set subFolders = New Collection
' Now recurse through any and all subfolders. Since we can't use Dir() with
' re-entrant code, we have to first create a Collection of all the subfolder names,
' then call recurse for each one.
theFile = Dir(parentPath, vbDirectory)
While (theFile <> "")
If theFile <> "." And theFile <> ".." And GetAttr(parentPath & theFile) And vbDirectory Then
subFolders.Add (theFile)
End If
theFile = Dir()
Wend
Dim x As Long
For x = 1 To subFolders.Count
theFile = subFolders.Item(x)
Call recurse(oInfoStore, oInfoObjects, parentPath & theFile & "\", theFile, newParentID)
Next
End Sub
' Function: getPlugIn
' What: Gets the PlugInInfo object for the specified file extension
' In: String file extension
'
' Out: PlugInInfo of the specified type -- "Agnostic" type if no match
Function getPlugIn(inFile As String) As PluginInfo
Select Case LCase(Right(inFile, 3))
Case "pdf": Set getPlugIn = pluginPDF
Case "xls": Set getPlugIn = pluginExcel
Case "doc": Set getPlugIn = pluginWord
Case "txt": Set getPlugIn = pluginTxt
Case "Rtf": Set getPlugIn = pluginRtf
Case "ppt": Set getPlugIn = pluginPowerpoint
Case Else: Set getPlugIn = pluginAgnostic
End Select
End Function
' Function: getObjectByName
' What: Checks if the specified object exists in the parent
' In: InfoStore
' String name of the object to look for
' String kind of the object to look for (in si_kind format)
' Long parent ID to look in
'
' Out: Long ID of the object if found, -1 otherwise
' Note: Only looks in ci_infoobjects, so only folders, reports, etc. are searchable
Function getObjectByName(oInfoStore As InfoStore, strName As String, strKind As String, lParentId As Long) As Long
Dim oInfoObjects As InfoObjects
Set oInfoObjects = oInfoStore.Query("select si_id from ci_infoobjects where si_name = '" & strName & "' and si_kind = '" & strKind & "' and si_parentid = " & lParentId)
If oInfoObjects.Count = 0 Then
getObjectByName = -1
Else
getObjectByName = oInfoObjects.Item(1).ID
End If
End Function
joepeters (BOB member since 2002-08-29)