IMPAX print-script

 Print liste fra Impax liste-område

' Author: Thomas Axelsen, RegionH
 ' v0.1
 ' Last Update 26.04.16
 ' Added Try/Catch to enable export of lists containing pure RIS-exams
 ' Added XML export for fulll set of (shown) columns

' Export Script for printing (very crude) lists for Impax 6.6.1
 ' Template is saved on regional drive
 ' Templates can be created with Word, exported as RTF, opened in Wordpad and exported as RTF.
 ' Might work directly from Word
 ' The following TAGS are currently supported: $PID, $NAME, $DEPT, $KONF
 ' $PID = patientID, $NAME = patientname, $DEPT = ref. dept., $KONF is conferencename (from inputbox)

Option Strict Off

Imports System
 Imports AgfaHC.pacs.framework
 Imports system.collections
 Imports System.Windows.Forms
 Imports System.Text.RegularExpressions
 Imports System.IO
 Imports System.Text

Module Script
 Sub Main()

Dim count As Integer = 0
 Dim study As PacsStudy
 Dim list As ArrayList
 Dim ColumnError = 0

Dim pid, lastPid, textString, textFinal, suid, pname, dept, RtfFinal As String
 Dim sLine, lineMod, encName, lineKonf, lineDate As String
 Dim sArrayToWrite As New ArrayList
 Dim spid, sname, sdept As String
 Dim fTemplate As String = "R:\RIS PACS Vejledninger\Documents\Template.rtf"
 Dim fTemp As String = "C:\Temp\templist.rtf"

Dim konfname As String = InputBox("Indtast konferencenavn", "Indtast konferencenavn")

ScriptListArea.SelectAllInActiveWorkList()
 list = ScriptListArea.GetSelectedItemsInActiveWorklist()
 If list.Count > 0 Then
 Try
 Using sr As New StreamReader(fTemplate)
 While sr.Peek() >= 0
 sLine = sr.ReadLine()
 If sLine.Contains("$PID") Then
 For Each study In list
 If risOrPacsList(study, list) = 1 Then
 spid = "ris.patient_id"
 sname = "ris.patient_name"
 sdept = "ris.requesting_department_name"
 Else
 spid = "patient.patient_id"
 sname = "patient.patient_name"
 sdept = ""
 End If
 Try
 pid = study.ColumnData(spid).ToString
 pname = study.ColumnData(sname).ToString
 dept = study.ColumnData(sdept).ToString
 Catch ex As Exception
 ColumnError = 1
 End Try
 reformatPid(pid)
 lineMod = sLine.replace("$PID", pid).replace("$NAME", pname).replace("$DEPT", dept)
 sArrayToWrite.Add(lineMod)
 If String.Compare(pid, lastPid) <> 0
 count += 1
 lastPid = pid
 End If
 Next
 Else If sLine.Contains("$KONF") Then
 lineKonf = sLine.Replace("$KONF", konfname).replace("$DATO", DateTime.Today)
 sArrayToWrite.Add(lineKonf)
 Else
 sArrayToWrite.Add(sLine)
 End If
 End While
 End Using
 Catch e As Exception
 MsgBox("The process failed: {0}", e.ToString())
 End Try

If ColumnError = 1 Then
 MessageBox.Show("Én eller flere kolonner vises ikke på den valgte liste." + VbCrLF + "Følgende kolonner bør vises: Patient-ID, Navn og Henv. afd. Udskrift fortsætter uden disse oplysninger.", "Advarsel", MessageBoxButtons.OK)
 End If

Using outputFile As New StreamWriter(fTemp, False, encoding.GetEncoding(1252))
 For Each line As String In sArrayToWrite
 outputFile.WriteLine(line)
 Next
 End Using
 Else If list.Count = 0 Then
 MessageBox.Show("Listen ser ud til at være tom", "Fejl", MessageBoxButtons.OK)
 Exit Sub
 End If

dim result As integer
 result = MessageBox.Show("Udskriv til standard-printer?", _
 "Vil du fortsætte", MessageBoxButtons.OKCancel)
 If result = DialogResult.Cancel Then
 Exit Sub
 ElseIf result = DialogResult.OK Then
 Dim objWord = CreateObject("Word.Application")
 Dim objDoc = objWord.Documents.Open(fTemp)
 objDoc.PrintOut()
 objWord.Quit
 End If

End Sub

Function risOrPacsList(byVal study As PacsStudy, ByVal list As ArrayList) As Integer
 Dim ColumnData, Key, columnText, MyColumns, colHeader
 For Each study In list
 MyColumns = study.ColumnData.Keys()
 For Each Key In MyColumns
 colHeader = chr(34) & Key.ToString & chr(34)
 Try
 If Key.ToString.Contains("ris") Then
 risOrPacsList = 1
 Exit Function
 End If
 Catch ex As Exception
 msgbox(ex.message)
 Continue For
 End Try
 Next
 Next
 End Function

Sub reformatPid(ByRef pid As String)
 Dim patternString = "[0-9]{7}[0-9|a-zA-Z]{2}[0-9]{1}"
 pid = Regex.Match(pid, patternString).Value
 End Sub
 End Module