'https://kin.naver.com/qna/detail.nhn?d1id=1&dirId=102020101&docId=300736954
Option Explicit
Const TITLE = "방명록"
Public xlsApp As Object 'New Excel.Application
Public xlsWb As Object 'Workbook
Public Sub WriteGuestbook()
Dim sht As Object 'Worksheet
Dim r As Long
Dim i As Integer
Dim user As VbMsgBoxResult
Dim msg As String
Dim xlsFile As String
Dim Labels(1 To 4) As String
On Error GoTo Er
If Len(ActivePresentation.Slides(1).Shapes("TextBox1").OLEFormat.Object.Text) = 0 Then
MsgBox "성명은 필수항목입니다.", vbOKOnly, TITLE
ActivateShape.ActivateShape ActivePresentation.Slides(1).Shapes("TextBox1")
Exit Sub
End If
Labels(1) = "성명": Labels(2) = "이메일": Labels(3) = "연락처": Labels(4) = "메모"
xlsFile = ActivePresentation.Path & "\방명록.xlsx"
msg = "아래 내용으로 방명록을 남기시겠습니까?"
For i = 1 To 4
msg = msg & vbNewLine & vbNewLine & Labels(i) & " : " & _
ActivePresentation.Slides(1).Shapes("TextBox" & i).OLEFormat.Object.Text
Next i
user = MsgBox(msg, vbOKCancel, TITLE)
If user = vbCancel Then Exit Sub
Set xlsApp = CreateObject("Excel.Application")
If xlsApp Is Nothing Then MsgBox "엑셀 실행 오류", vbOKOnly, TITLE: Exit Sub
xlsApp.Visible = False
If Len(Dir(xlsFile)) < 1 Then
Set xlsWb = xlsApp.Workbooks.Add(-4167): 'xlsWb.SaveAs FileName:=xlsFile
xlsWb.SaveAs FileName:=xlsFile: xlsWb.Close SaveChanges:=False
End If
'Else
Set xlsWb = xlsApp.Workbooks.Open(xlsFile)
'End If
Set sht = xlsWb.Worksheets(1)
r = sht.Cells(sht.Rows.Count, 1).End(-4162).Offset(1).Row '맨 아래줄 다음줄에 추가(-4162=xlUp)
For i = 1 To 4
sht.Cells(r, i).Value = ActivePresentation.Slides(1).Shapes("TextBox" & i).OLEFormat.Object.Text
Next i
sht.Cells(r, i).Value = Format(Date, "YYYY/MM/DD") & Format(Time, " Hh:nn:ss")
sht.Cells(r, i).NumberFormat = "YYYY/MM/DD hh:mm:ss"
sht.Columns.AutoFit
Er:
If Err.Number Then MsgBox Err.Description
If Not (xlsWb Is Nothing) Then
xlsWb.Save
'xlsWb.Close SaveChanges:=True, FileName:=xlsFile
'xlsWb.Saved = True
xlsWb.Close SaveChanges:=False
End If
If Not (xlsApp Is Nothing) Then
xlsApp.Quit
Set xlsApp = Nothing
Set xlsWb = Nothing
Set sht = Nothing
End If
CancelGuestbook '기존 내용 지우기
On Error Resume Next
SlideShowWindows(1).View.GotoSlide 1, msoTrue
End Sub
Public Sub CancelGuestbook()
Dim i As Integer
For i = 1 To 4
ActivePresentation.Slides(1).Shapes("TextBox" & i).OLEFormat.Object.Text = ""
Next i
End Sub
Sub OnSlideShowPageChange(SSW As SlideShowWindow)
If SSW.View.CurrentShowPosition = 1 Then _
'FindFirstTextboxControlAndSetFocus (SSW.View.Slide)
ActivateShape.ActivateShape ActivePresentation.Slides(1).Shapes("TextBox1")
End If
End Sub
Sub OnSlideShowTerminate_notUsed(ByVal Wn As SlideShowWindow)
If Not (xlsApp Is Nothing) Then
If Not (xlsWb Is Nothing) Then
xlsWb.Save
xlsWb.Close SaveChanges:=False
End If
xlsApp.Quit
Set xlsApp = Nothing
Set xlsWb = Nothing
Set sht = Nothing
End If
End Sub
최근댓글