Sub GetWordList()
Dim usr As String, msg As VbMsgBoxResult
Dim Target As Collection
Dim sht As Worksheet
Dim CurDate As Date, StartDate As Date, EndDate As Date
Dim i As Integer
Set Target = New Collection
Set sht = ActiveSheet
usr = InputBox("대상 날짜를 '2018-01-01' 형식으로 입력하세요:" & vbNewLine & vbNewLine & _
"대상기간은 시트이름이 Monthly, Weekly, Daily 로 시작할 때 각각 다릅니다.", "네이버 오늘의 단어", Format(Date, "yyyy-mm-dd"))
If Len(usr) <> 10 Then Exit Sub
CurDate = DateSerial(Left(usr, 4), Mid(usr, 6, 2), Right(usr, 2))
' With sht.Shapes("Button 1").OLEFormat.Object
' .Text = Format(CurDate, "yyyy-mm-dd")
' .Font.Size = 9
' End With
' If Cells(1, 1).NumberFormat = "General" Then
' CurDate = DateSerial(Year(Left([A1], 4)), Month(Mid([A1], 6, 2)), Day(Right([A1], 2)))
' ElseIf Cells(1, 1).NumberFormat = "yyyy-mm-dd" Then
' CurDate = CDate(Cells(1, 1))
' Else
' MsgBox "[A1]셀에 '2018-01-01' 형식으로 입력하세요.": Exit Sub
' End If
If sht.Name Like "Weekly*" Then
StartDate = CurDate - Weekday(CurDate, vbUseSystemDayOfWeek) + 1
For i = 0 To 6
Target.Add Format(StartDate + i, "yyyy-mm-dd")
Next i
ElseIf sht.Name Like "Monthly*" Then
StartDate = DateSerial(Year(CurDate), Month(CurDate), 1)
EndDate = DateSerial(Year(CurDate), Month(CurDate) + 1, 0)
For i = 0 To Day(EndDate) - 1
Target.Add Format(StartDate + i, "yyyy-mm-dd")
Next i
Else
Target.Add CurDate 'Cells(1, "A").Value
End If
If Target.Count > 1 Then
msg = MsgBox(Format(StartDate, "yyyy-mm-dd") & "부터 " & Target.Count & _
"일간의 데이터를 가져옵니다.", vbOKCancel, "데이터 가져오기 시작")
If msg = vbCancel Then Exit Sub
End If
GetDailyWordList Target
Set Target = Nothing
End Sub
Function GetDailyWordList(TargetDates As Collection)
Dim sht As Worksheet
Dim Winhttp As WinHttpRequest
Dim Html As HTMLDocument, Html2 As HTMLDocument
Dim Result As IHTMLElementCollection
Dim Url As String
Dim wordlist As Object
Dim i As Long
Dim str As String
On Error Resume Next
Set Winhttp = New WinHttpRequest
Set Html = New HTMLDocument
Set Html2 = New HTMLDocument
Set sht = ActiveSheet
Rows("2:" & Rows.Count).Clear
UserForm1.Show vbModeless 'progress bar
'Application.ScreenUpdating = False
For Each TargetDate In TargetDates
'Url = "http://m.wordbook.naver.com/endic/today/words.nhn?targetDate=" & TargetDate
Url = "http://m.wordbook.naver.com/endic/today/recite.nhn?targetDate=" & _
Format(TargetDate, "yyyy.mm.dd")
With Winhttp
.Open "Get", Url
.send
.WaitForResponse
Html.body.innerHTML = .responseText
Set Result = Html.getElementsByClassName("entryPage book_bg3")
For Each wordlist In Result
i = i + 1
Html2.body.innerHTML = wordlist.innerHTML
'Date
Cells(1 + i, 1).Value = TargetDate & "(" & _
WeekdayName(Weekday(TargetDate, vbUseSystemDayOfWeek), True, vbUseSystemDayOfWeek) & ")"
Cells(1 + i, 1).NumberFormat = "yyyy-mm-dd"
sht.Hyperlinks.Add anchor:=Cells(1 + i, 1), _
Address:=Url, _
ScreenTip:="Daily words(Web)"
Cells(1 + i, 1).Font.Underline = xlUnderlineStyleNone
Cells(1 + i, 1).Font.ColorIndex = xlAutomatic
Cells(1 + i, 1).ShrinkToFit = True
'index
Cells(1 + i, 2).Value = i
'단어
Cells(1 + i, 3).Value = Html2.getElementsByTagName("a")(1).innerText
sht.Hyperlinks.Add anchor:=Cells(1 + i, 3), _
Address:=Html2.getElementsByTagName("a")(1).href, _
ScreenTip:="Search(via WebBrowser)"
Cells(1 + i, 3).Font.Underline = xlUnderlineStyleNone
Cells(1 + i, 3).Font.Color = rgbDarkBlue
Cells(1 + i, 3).Font.Bold = True
'발음기호
str = Html2.getElementsByClassName("cen_dn")(0).innerText
Cells(1 + i, 4).Value = IIf(Len(str), str, "Listen")
'발음듣기
'Cells(1 + i, 5).Value = Html2.getElementsByTagName("a")(2).getAttribute("title")
sht.Hyperlinks.Add anchor:=Cells(1 + i, 4), _
Address:=Html2.getElementsByTagName("a")(2).getAttribute("data-purl"), _
ScreenTip:="Listen(via WebBrowser)"
Cells(1 + i, 4).Font.Underline = xlUnderlineStyleNone
Cells(1 + i, 4).Font.ColorIndex = xlAutomatic
'뜻
Cells(1 + i, 5).Value = Html2.getElementsByClassName("cen_txt_sub")(0).innerText
'Cells(1 + i, 5).ShrinkToFit = True
'예문
str = Html2.getElementsByClassName("cen_dn tts_area")(0).innerText
If Right(str, 4) = "play" Then str = Left(str, Len(str) - 4): _
Cells(1 + i, 6).Value = str: _
Cells(1 + i, 6).IndentLevel = 1
Cells(1 + i, 7).Value = Html2.getElementsByClassName("cen_dn")(2).innerText
Cells(1 + i, 7).ShrinkToFit = True
Next wordlist
End With
UserForm1.ProgressBar1.Value = CInt((i / TargetDates.Count) * 100)
Next TargetDate
Set Html2 = Nothing
Set Html = Nothing
Set Winhttp = Nothing
Unload UserForm1
'Application.ScreenUpdating = True
End Function
최근댓글