Option Explicit Private mInitializing As Boolean Private mFormattingTime As Boolean Private Sub UserForm_Initialize() mInitializing = True Me.Caption = "규율위반자 개인별 명부 작성" txtExcelPath.Locked = True txtExcelPath.BackColor = RGB(245, 245, 245) txtExcelPath.TabStop = False txtSaveFolder.Locked = True txtSaveFolder.BackColor = RGB(245, 245, 245) txtSaveFolder.TabStop = False ' 기존 경로 불러오기 txtExcelPath.Text = GetSetting("DisciplineApp", "Settings", "ExcelPath", "") txtSaveFolder.Text = GetSetting("DisciplineApp", "Settings", "SaveFolder", DefaultSaveFolder()) ' 날짜·시각 저장상태 불러오기 If GetSetting("DisciplineApp", "Settings", "SaveViolationDateTime", "False") = "True" Then txtViolationDate.Text = GetSetting("DisciplineApp", "Settings", "ViolationDate", "") txtViolationTime.Text = GetSetting("DisciplineApp", "Settings", "ViolationTime", "") chkSaveViolationDateTime.Value = True Else txtViolationDate.Text = Format$(Date, "yy.mm.dd") txtViolationTime.Text = "" End If ' 보고자 저장상태 불러오기 If GetSetting("DisciplineApp", "Settings", "SaveReporter", "False") = "True" Then txtReporterRank.Text = GetSetting("DisciplineApp", "Settings", "ReporterRank", "") txtReporterName.Text = GetSetting("DisciplineApp", "Settings", "ReporterPersonName", _ GetSetting("DisciplineApp", "Settings", "ReporterName", "")) chkSaveReporter.Value = True End If ' 위반내용 저장상태 불러오기 If GetSetting("DisciplineApp", "Settings", "SaveViolationContent", "False") = "True" Then txtViolationContent.Text = GetSetting("DisciplineApp", "Settings", "ViolationContent", "") chkSaveViolationContent.Value = True End If ' 비고 저장상태 불러오기 If GetSetting("DisciplineApp", "Settings", "SaveNote", "False") = "True" Then txtNote.Text = GetSetting("DisciplineApp", "Settings", "NoteText", "") chkSaveNote.Value = True End If ' 파일 동시생성 체크박스 상태 불러오기 If GetSetting("DisciplineApp", "Settings", "CreateFileWithPrint", "True") = "True" Then chkCreateFile.Value = True Else chkCreateFile.Value = False End If If GetSetting("DisciplineApp", "Settings", "CreateFileWithPrintNow", "False") = "True" Then chkCreateFileNow.Value = True Else chkCreateFileNow.Value = False End If lstQueue.ColumnCount = 7 lstQueue.ColumnWidths = "45 pt;60 pt;100 pt;310 pt;60 pt;60 pt;80 pt" InitializeQueue RefreshQueue mInitializing = False End Sub Private Sub btnBrowse_Click() Dim selectedPath As String selectedPath = SelectExcelFile(txtExcelPath.Text) If Len(selectedPath) = 0 Then Exit Sub CloseDataWorkbook txtExcelPath.Text = selectedPath SaveSetting "DisciplineApp", "Settings", "ExcelPath", txtExcelPath.Text End Sub Private Sub chkSaveReporter_Click() If mInitializing Then Exit Sub SaveSetting "DisciplineApp", "Settings", "SaveReporter", CStr(chkSaveReporter.Value) If chkSaveReporter.Value = True Then SaveReporterSettings Else SaveSetting "DisciplineApp", "Settings", "ReporterRank", "" SaveSetting "DisciplineApp", "Settings", "ReporterPersonName", "" SaveSetting "DisciplineApp", "Settings", "ReporterName", "" txtReporterRank.Text = "" txtReporterName.Text = "" End If End Sub Private Sub txtReporterRank_Change() If mInitializing Then Exit Sub If chkSaveReporter.Value = True Then SaveReporterSettings End Sub Private Sub txtReporterName_Change() If mInitializing Then Exit Sub If chkSaveReporter.Value = True Then SaveReporterSettings End Sub Private Sub chkSaveViolationDateTime_Click() If mInitializing Then Exit Sub SaveSetting "DisciplineApp", "Settings", "SaveViolationDateTime", CStr(chkSaveViolationDateTime.Value) If chkSaveViolationDateTime.Value = True Then SaveViolationDateTimeSettings Else SaveSetting "DisciplineApp", "Settings", "ViolationDate", "" SaveSetting "DisciplineApp", "Settings", "ViolationTime", "" txtViolationDate.Text = Format$(Date, "yy.mm.dd") txtViolationTime.Text = "" End If End Sub Private Sub txtViolationDate_Change() If mInitializing Then Exit Sub If chkSaveViolationDateTime.Value = True Then SaveViolationDateTimeSettings End Sub Private Sub txtViolationTime_Change() Dim rawValue As String, formattedValue As String Dim index As Long, currentChar As String If mInitializing Or mFormattingTime Then Exit Sub rawValue = txtViolationTime.Text For index = 1 To Len(rawValue) currentChar = Mid$(rawValue, index, 1) If currentChar >= "0" And currentChar <= "9" Then formattedValue = formattedValue & currentChar If Len(formattedValue) = 4 Then Exit For End If Next index If Len(formattedValue) > 2 Then formattedValue = Left$(formattedValue, 2) & ":" & Mid$(formattedValue, 3) End If If txtViolationTime.Text <> formattedValue Then mFormattingTime = True txtViolationTime.Text = formattedValue txtViolationTime.SelStart = Len(formattedValue) mFormattingTime = False End If If chkSaveViolationDateTime.Value = True Then SaveViolationDateTimeSettings End Sub Private Sub chkSaveViolationContent_Click() If mInitializing Then Exit Sub SaveSetting "DisciplineApp", "Settings", "SaveViolationContent", CStr(chkSaveViolationContent.Value) If chkSaveViolationContent.Value = True Then SaveSetting "DisciplineApp", "Settings", "ViolationContent", txtViolationContent.Text Else SaveSetting "DisciplineApp", "Settings", "ViolationContent", "" txtViolationContent.Text = "" End If End Sub Private Sub txtViolationContent_Change() If mInitializing Then Exit Sub If chkSaveViolationContent.Value = True Then SaveSetting "DisciplineApp", "Settings", "ViolationContent", txtViolationContent.Text End If End Sub Private Sub chkSaveNote_Click() If mInitializing Then Exit Sub SaveSetting "DisciplineApp", "Settings", "SaveNote", CStr(chkSaveNote.Value) If chkSaveNote.Value = True Then SaveSetting "DisciplineApp", "Settings", "NoteText", txtNote.Text Else SaveSetting "DisciplineApp", "Settings", "NoteText", "" txtNote.Text = "" End If End Sub Private Sub txtNote_Change() If mInitializing Then Exit Sub If chkSaveNote.Value = True Then SaveSetting "DisciplineApp", "Settings", "NoteText", txtNote.Text End If End Sub Private Sub chkCreateFile_Click() If mInitializing Then Exit Sub SaveSetting "DisciplineApp", "Settings", "CreateFileWithPrint", CStr(chkCreateFile.Value) End Sub Private Sub chkCreateFileNow_Click() If mInitializing Then Exit Sub SaveSetting "DisciplineApp", "Settings", "CreateFileWithPrintNow", CStr(chkCreateFileNow.Value) End Sub Private Sub btnClearSavedInputs_Click() mInitializing = True chkSaveViolationDateTime.Value = False chkSaveReporter.Value = False chkSaveViolationContent.Value = False chkSaveNote.Value = False txtViolationDate.Text = Format$(Date, "yy.mm.dd") txtViolationTime.Text = "" txtReporterRank.Text = "" txtReporterName.Text = "" txtViolationContent.Text = "" txtNote.Text = "" mInitializing = False SaveSetting "DisciplineApp", "Settings", "SaveViolationDateTime", "False" SaveSetting "DisciplineApp", "Settings", "ViolationDate", "" SaveSetting "DisciplineApp", "Settings", "ViolationTime", "" SaveSetting "DisciplineApp", "Settings", "SaveReporter", "False" SaveSetting "DisciplineApp", "Settings", "ReporterRank", "" SaveSetting "DisciplineApp", "Settings", "ReporterPersonName", "" SaveSetting "DisciplineApp", "Settings", "ReporterName", "" SaveSetting "DisciplineApp", "Settings", "SaveViolationContent", "False" SaveSetting "DisciplineApp", "Settings", "ViolationContent", "" SaveSetting "DisciplineApp", "Settings", "SaveNote", "False" SaveSetting "DisciplineApp", "Settings", "NoteText", "" txtNumber.SetFocus End Sub Private Sub btnAdd_Click() Dim addedCount As Long, message As String, succeeded As Boolean Dim violationDateTime As String, reporter As String If Trim(txtNumber.Text) = "" Then MsgBox "번호를 입력해 주세요.", vbExclamation, "입력 오류": txtNumber.SetFocus: Exit Sub End If If Not TryGetViolationDateTime(violationDateTime) Then Exit Sub If Not TryGetReporter(reporter) Then Exit Sub succeeded = QueueAddNumbers(txtExcelPath.Text, txtNumber.Text, violationDateTime, _ txtViolationContent.Text, reporter, "", txtNote.Text, _ addedCount, message) If Not succeeded Then MsgBox message, vbExclamation, "명단 추가 실패" Exit Sub End If RefreshQueue If InStr(1, message, "추가하지 못한 번호", vbTextCompare) > 0 Then MsgBox message, vbExclamation, "일부 명단 추가 실패" End If txtNumber.Text = "" If chkSaveViolationDateTime.Value = False Then txtViolationDate.Text = Format$(Date, "yy.mm.dd") txtViolationTime.Text = "" End If If chkSaveViolationContent.Value = False Then txtViolationContent.Text = "" If chkSaveNote.Value = False Then txtNote.Text = "" txtNumber.SetFocus End Sub Private Sub btnPrintNow_Click() Dim message As String Dim outputPath As String, violationDateTime As String, reporter As String Dim sourceDoc As Document If Not TryGetViolationDateTime(violationDateTime) Then Exit Sub If Not TryGetReporter(reporter) Then Exit Sub Set sourceDoc = ActiveDocument If chkCreateFileNow.Value = True Then If PrintSingleRecordWithFile(sourceDoc, txtExcelPath.Text, txtNumber.Text, _ violationDateTime, txtViolationContent.Text, _ reporter, "", txtNote.Text, txtSaveFolder.Text, _ outputPath, message) Then MsgBox message & vbCrLf & vbCrLf & "[저장경로]" & vbCrLf & outputPath, _ vbInformation, "완료" Else MsgBox message, vbExclamation, "인쇄 및 파일 생성 실패" End If Else If PrintSingleRecord(sourceDoc, txtExcelPath.Text, txtNumber.Text, _ violationDateTime, txtViolationContent.Text, _ reporter, "", txtNote.Text, message) Then MsgBox message, vbInformation, "바로 인쇄 완료" Else MsgBox message, vbExclamation, "바로 인쇄 실패" End If End If End Sub Private Sub btnCreateNow_Click() Dim message As String, outputPath As String Dim violationDateTime As String, reporter As String Dim sourceDoc As Document If Not TryGetViolationDateTime(violationDateTime) Then Exit Sub If Not TryGetReporter(reporter) Then Exit Sub Set sourceDoc = ActiveDocument If CreateSingleRecordFile(sourceDoc, txtExcelPath.Text, txtNumber.Text, _ violationDateTime, txtViolationContent.Text, _ reporter, "", txtNote.Text, _ txtSaveFolder.Text, outputPath, message) Then MsgBox message & vbCrLf & vbCrLf & "[저장경로]" & vbCrLf & outputPath, _ vbInformation, "파일 생성 완료" Else MsgBox message, vbExclamation, "파일 생성 실패" End If End Sub Private Sub btnRemove_Click() If lstQueue.ListIndex < 0 Then MsgBox "삭제할 항목을 선택해 주세요.", vbInformation: Exit Sub QueueRemove lstQueue.ListIndex RefreshQueue End Sub Private Sub btnClear_Click() If QueueCount() = 0 Then Exit Sub If MsgBox("명단을 모두 지울까요?", vbQuestion + vbYesNo, "전체 삭제") <> vbYes Then Exit Sub QueueClear RefreshQueue End Sub Private Sub btnSaveFolder_Click() Dim selectedFolder As String selectedFolder = SelectSaveFolder(txtSaveFolder.Text) If Len(selectedFolder) = 0 Then Exit Sub txtSaveFolder.Text = selectedFolder SaveSetting "DisciplineApp", "Settings", "SaveFolder", txtSaveFolder.Text End Sub Private Sub btnPrint_Click() Dim message As String, outPath As String Dim sourceDoc As Document If QueueCount() = 0 Then MsgBox "대기 명단이 비어 있습니다. 먼저 명단에 추가해 주세요.", vbInformation Exit Sub End If Set sourceDoc = ActiveDocument If chkCreateFile.Value = True Then If PrintQueueWithFiles(sourceDoc, txtSaveFolder.Text, outPath, message) Then MsgBox message & vbCrLf & vbCrLf & "[저장경로]" & vbCrLf & outPath, _ vbInformation, "완료" Else MsgBox message, vbExclamation, "인쇄 및 파일 생성 오류" End If Else If PrintQueue(sourceDoc, message) Then MsgBox message, vbInformation, "인쇄 완료" Else MsgBox message, vbExclamation, "인쇄 오류" End If End If End Sub Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) ResetTemplateData ActiveDocument CloseDataWorkbook End Sub Private Function TryGetViolationDateTime(ByRef combinedValue As String) As Boolean Dim dateValue As String, timeValue As String dateValue = Trim$(txtViolationDate.Text) timeValue = Trim$(txtViolationTime.Text) If Len(dateValue) = 0 Then MsgBox "위반 날짜를 입력해 주세요.", vbExclamation, "입력 오류" txtViolationDate.SetFocus Exit Function End If If Len(timeValue) = 0 Then MsgBox "위반 시각을 입력해 주세요.", vbExclamation, "입력 오류" txtViolationTime.SetFocus Exit Function End If combinedValue = dateValue & vbCr & timeValue TryGetViolationDateTime = True End Function Private Function TryGetReporter(ByRef combinedValue As String) As Boolean Dim rankValue As String, nameValue As String rankValue = Trim$(txtReporterRank.Text) nameValue = Trim$(txtReporterName.Text) If Len(rankValue) = 0 Then MsgBox "보고자 직급을 입력해 주세요.", vbExclamation, "입력 오류" txtReporterRank.SetFocus Exit Function End If If Len(nameValue) = 0 Then MsgBox "보고자 성명을 입력해 주세요.", vbExclamation, "입력 오류" txtReporterName.SetFocus Exit Function End If combinedValue = rankValue & vbCr & nameValue TryGetReporter = True End Function Private Sub SaveReporterSettings() SaveSetting "DisciplineApp", "Settings", "ReporterRank", txtReporterRank.Text SaveSetting "DisciplineApp", "Settings", "ReporterPersonName", txtReporterName.Text End Sub Private Sub SaveViolationDateTimeSettings() SaveSetting "DisciplineApp", "Settings", "ViolationDate", txtViolationDate.Text SaveSetting "DisciplineApp", "Settings", "ViolationTime", txtViolationTime.Text End Sub Private Sub RefreshQueue() Dim index As Long, rowIndex As Long, record As Variant lstQueue.Clear For index = 1 To QueueCount() record = QueueRecord(index) lstQueue.AddItem CStr(record(0)) rowIndex = lstQueue.ListCount - 1 lstQueue.List(rowIndex, 1) = CStr(record(1)) lstQueue.List(rowIndex, 2) = Replace(CStr(record(8)), vbCr, " ") lstQueue.List(rowIndex, 3) = CStr(record(9)) lstQueue.List(rowIndex, 4) = Replace(CStr(record(10)), vbCr, " ") lstQueue.List(rowIndex, 5) = CStr(record(11)) lstQueue.List(rowIndex, 6) = CStr(record(12)) Next index lblQueue.Caption = "대기 명단 (" & CStr(QueueCount()) & "건)" End Sub