Option Explicit Private mInitializing As Boolean Private mFormattingTime As Boolean Private Sub UserForm_Initialize() mInitializing = True ' ========================================================= ' ★ 1. 각 칸마다 한글이 먼저 타이핑되도록 IME 모드 강제 고정 ' ========================================================= txtNumber.IMEMode = 10 ' fmIMEModeHangul txtViolationDate.IMEMode = 10 txtViolationTime.IMEMode = 10 txtReporterRank.IMEMode = 10 txtReporterName.IMEMode = 10 txtViolationContent.IMEMode = 10 txtNote.IMEMode = 10 ' ========================================================= ' ★ 2. 드롭다운(콤보박스) 위반내용 9종 자동 세팅 ' ========================================================= On Error Resume Next With cmbViolationList .Clear .AddItem "지시불이행 (정당한 직무상 지시 위반)" .AddItem "근무자 모욕 및 폭언" .AddItem "수용자 간 폭행 및 싸움" .AddItem "지정된 구역 임의 이탈" .AddItem "소란 행위 (취침시간 등)" .AddItem "금지물품 반입 및 소지" .AddItem "허가받지 않은 물품 수수" .AddItem "시설물 훼손 및 파괴" .AddItem "기타 규율 위반" End With On Error GoTo 0 ' 기본 경로 로딩 및 세팅 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 InitializeQueue RefreshQueue mInitializing = False End Sub ' ★ 드롭다운에서 항목을 고르면, 위반내용 칸에 글씨가 자동으로 쏙! Private Sub cmbViolationList_Change() If mInitializing Then Exit Sub If cmbViolationList.ListIndex <> -1 Then txtViolationContent.Text = cmbViolationList.Value End If End Sub Private Sub btnBrowse_Click() Dim selectedPath As String, loadMessage As String selectedPath = SelectExcelFile(txtExcelPath.Text) If Len(selectedPath) = 0 Then Exit Sub txtExcelPath.Text = selectedPath SaveSetting "DisciplineApp", "Settings", "ExcelPath", txtExcelPath.Text lblQueueGuide.Caption = "기존 데이터를 비우고 새 엑셀을 불러오는 중입니다..." lblQueueGuide.ForeColor = RGB(0, 100, 255) Me.Repaint: DoEvents CloseDataConnection If LoadExcelDataCache(selectedPath, loadMessage) Then lblQueueGuide.Caption = "데이터 로딩 완료! 이제 번개처럼 조회됩니다." lblQueueGuide.ForeColor = RGB(0, 150, 0) Else MsgBox "데이터를 미리 불러오는 데 실패했습니다." & vbCrLf & loadMessage, vbExclamation, "로딩 실패" lblQueueGuide.Caption = "※ 다수 스티커 발부 시 사용해 주세요." lblQueueGuide.ForeColor = RGB(90, 90, 90) End If 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 Not mInitializing And chkSaveReporter.Value = True Then SaveReporterSettings: End Sub Private Sub txtReporterName_Change(): If Not mInitializing And 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 Not mInitializing And chkSaveViolationDateTime.Value = True Then SaveViolationDateTimeSettings: End Sub Private Sub txtViolationTime_Change() Dim rawValue As String, formattedValue As String, 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) 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 Not mInitializing And chkSaveViolationContent.Value = True Then SaveSetting "DisciplineApp", "Settings", "ViolationContent", txtViolationContent.Text: 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 Not mInitializing And chkSaveNote.Value = True Then SaveSetting "DisciplineApp", "Settings", "NoteText", txtNote.Text: End Sub Private Sub chkCreateFile_Click(): If Not mInitializing Then SaveSetting "DisciplineApp", "Settings", "CreateFileWithPrint", CStr(chkCreateFile.Value): End Sub Private Sub chkCreateFileNow_Click(): If Not mInitializing Then 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, violationDateTime As String, reporter As String If Trim(txtNumber.Text) = "" Then MsgBox "번호를 입력해 주세요.", vbExclamation, "입력 오류": txtNumber.SetFocus: Exit Sub 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 RefreshQueue If InStr(1, message, "추가하지 못한 번호", vbTextCompare) > 0 Then MsgBox message, vbExclamation, "일부 명단 추가 실패" txtNumber.Text = "" If chkSaveViolationDateTime.Value = False Then txtViolationDate.Text = Format$(Date, "yy.mm.dd"): txtViolationTime.Text = "" If chkSaveReporter.Value = False Then txtReporterRank.Text = "": txtReporterName.Text = "" 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, outputPath As String, violationDateTime As String, reporter As String, 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, violationDateTime As String, reporter As String, 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 btnCreateFiles_Click() Dim message As String, outPath As String, sourceDoc As Document If QueueCount() = 0 Then MsgBox "대기 명단이 비어 있습니다. 먼저 명단에 추가해 주세요.", vbInformation: Exit Sub Set sourceDoc = ActiveDocument If CreateQueueFile(sourceDoc, txtSaveFolder.Text, outPath, message) Then MsgBox message & vbCrLf & vbCrLf & "[저장경로]" & vbCrLf & outPath, vbInformation, "파일 생성 완료" Else: MsgBox message, vbExclamation, "파일 생성 오류" End If End Sub Private Sub btnPrint_Click() Dim message As String, outPath As String, sourceDoc As Document If QueueCount() = 0 Then MsgBox "대기 명단이 비어 있습니다. 먼저 명단에 추가해 주세요.", vbInformation: Exit Sub 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: CloseDataConnection: 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 If Len(timeValue) = 0 Then MsgBox "위반 시각을 입력해 주세요.", vbExclamation, "입력 오류": txtViolationTime.SetFocus: Exit Function 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 If Len(nameValue) = 0 Then MsgBox "보고자 성명을 입력해 주세요.", vbExclamation, "입력 오류": txtReporterName.SetFocus: Exit Function 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 If InStr(lblQueueGuide.Caption, "기다려주세요") = 0 And InStr(lblQueueGuide.Caption, "로딩 완료") = 0 Then lblQueue.Caption = "대기 명단 (" & CStr(QueueCount()) & "건)" End If End Sub