Skip to main content

Printing Loop

Hello from Germany,
my question, I hope it is in the right Forum here, is as follows:
I have a number of databases that I want to print. I have a script for the first one of them (shown below) and now I want to do a loop, that prints all the data from 1 to 46 without changing the skript for every database.
 
Sub Main
Call PrintDatabase() ´Datei anhängen1.IMD
End Sub
Function PrintDatabase
Set db=Client.OpenDatabase("Dateien anhängen1.IMD")
db.Print
End Function
 
Thanks for your help.
 

Brian Element Mon, 03/15/2021 - 14:09

Hello MrStone and welcome to the site.

You could do something like this.  This script assumes you are printing all your files in the project folder.  If this is not the case you will have to adjust the array to hold the names of the items you do want printed.


Option Explicit

Dim sDatabaseList() As String

Sub Main
	Dim i As Integer
	Call getDatabases()
	'loop through each database item and send the database name to the print function
	For i = 0 To UBound(sDatabaseList)
		Call PrintDatabase(sDatabaseList(i))
	Next i
End Sub

Function PrintDatabase(sFilename As String)
	Dim db As database
	
	Set db=Client.OpenDatabase(sFilename)
		db.Print
		db.close
	Set db = Nothing
End Function

'obtain all the databases in the current project and place them in the sDatabaseList array
Function getDatabases()
	Dim task As task
	Dim collection As Object
	Dim bFirstItem As Boolean
	Dim ThisName As String
	
	bFirstItem = True
	
	Set task = Client.ProjectManagement

		Set collection = task.Databases

			For Each ThisName In collection
				'if first item then redefine sDatabaseList to 0 (1 item)
				If bFirstItem Then
					bFirstItem = False
					ReDim sDatabaseList(0)
					sDatabaseList(0) = ThisName
				Else
					ReDim preserve sDatabaseList(UBound(sDatabaseList) + 1) 'increment the array by 1
					sDatabaseList(UBound(sDatabaseList)) = ThisName 'place the new database name in the last slot of the array
				End If

			Next 


		Set collection = Nothing
	Set task = Nothing
End Function