-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOneNoteVBA-SearchPageTitlesReturnResults
145 lines (121 loc) · 4.95 KB
/
OneNoteVBA-SearchPageTitlesReturnResults
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
Function test()
Call fONGetPageID("a")
End Function
Function fONGetPageID(sSearchInTitle As String)
'============================================================================
' Name : fONGetSections
' Author : Erica L Ingram
' Copyright : 2019, A Quo Co.
' Call command: Call fONGetPageID()
' Description : get ON page by searching title
'blog post explanation at https://transcription.aquoco.co/2019/05/08/using-vba-with-json-onenote/
'sample database at https://www.aquoco.co/database3.zip
'============================================================================
Dim sURL As String, sToken As String, sLine4 As String
Dim Parsed As Dictionary
Dim sFile1 As String, sFile2 As String, sFile3 As String, sFile4 As String
Dim sLine1 As String, sLine2 As String, sLine3 As String
Dim sResponseText As String, apiWaxLRS As String
Dim sONID As String, sSelfLink As String, sCreatedDateTime As String
Dim sDisplayName As String, sLastModifiedDateTime As String, isDefault As String
Dim sUserRole As String, sIsShared As String, sSectionsUrl As String, sSectionGroupsUrl As String
Dim sCBDisplayName As String, sCBLastModifiedName As String
Dim sCBLWebURL As String, sIsDefault As String
Dim sJSONResponse As String, sCBID As String
Dim rep
Dim sCBLastModifiedID As String, sCBLClientURL As String
Dim sPNID As String, sPNDisplayName As String
Dim sPNSelf As String, sPSGODContext As String, sPSG As String
'before auth if you make privilege changes you must visit this website and accept permissions.
'sState = ""
'"https://login.microsoftonline.com/" & sLine2 & "adminconsent?client_id=" & sLine3 & "&state=" & sState & "&redirect_uri=https://login.microsoftonline.com/common/oauth2/nativeclient"
sFile1 = "C:\other\6.txt" 'object id / user ID
sFile2 = "C:\other\7.txt" 'directory tenant id
sFile3 = "C:\other\8.txt" 'application client id
sFile4 = "C:\other\9.txt" 'secret
Open sFile1 For Input As #1
Line Input #1, sLine1
Close #1
Open sFile2 For Input As #2
Line Input #2, sLine2
Close #2
Open sFile3 For Input As #3
Line Input #3, sLine3
Close #3
Open sFile4 For Input As #4
Line Input #4, sLine4
Close #4
sURL = "https://login.microsoftonline.com/" & sLine2 & "/oauth2/v2.0/token"
With CreateObject("WinHttp.WinHttpRequest.5.1")
.Open "POST", sURL, False
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.setRequestHeader "Host", "login.microsoftonline.com"
.send ("client_id=" & sLine3 & "&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret=" & sLine4 & "&grant_type=client_credentials")
.waitForResponse
apiWaxLRS = .responseText
Debug.Print "--------------------------------------------"
.abort
End With
Set Parsed = JsonConverter.ParseJson(apiWaxLRS)
sToken = Parsed("access_token")
sNotebookID = ""
sURL = "https://graph.microsoft.com/v1.0/users/" & sLine1 & "/onenote/pages"
With CreateObject("WinHttp.WinHttpRequest.5.1")
.Open "GET", sURL, False
.setRequestHeader "Host", "graph.microsoft.com"
.setRequestHeader "Authorization", "Bearer " & sToken
.send
apiWaxLRS = .responseText
sToken = ""
sFile2 = ""
sFile3 = ""
sFile4 = ""
sURL = ""
sLine1 = ""
sLine2 = ""
sLine3 = ""
sLine4 = ""
Debug.Print apiWaxLRS
Debug.Print "--------------------------------------------"
.abort
End With
Set Parsed = JsonConverter.ParseJson(apiWaxLRS)
Set vDetails = Parsed("value")
For Each rep In vDetails
sTitle = rep("title")
If InStr(1, sTitle, sSearchInTitle) Then
sONID = rep("id")
sSelfLink = rep("self")
sCreatedDateTime = rep("createdDateTime")
sTitle = rep("title")
scontentUrl = rep("contentUrl")
sIsShared = rep("parentNotebook@odata.context")
sLastModifiedDateTime = rep("lastModifiedDateTime")
Set rep1 = rep("links")
Set rep2 = rep1("oneNoteClientUrl")
sCBLClientURL = rep2("href")
Set rep3 = rep1("oneNoteWebUrl")
sCBLWebURL = rep3("href")
Set rep9 = rep("parentSection")
sPNID = rep9("id")
sPNDisplayName = rep9("displayName")
sPNSelf = rep9("self")
Debug.Print "sONID = " & sONID
Debug.Print "sTitle = " & sTitle
Debug.Print "scontentUrl = " & scontentUrl
Debug.Print "sSelfLink = " & sSelfLink
Debug.Print "sCreatedDateTime = " & sCreatedDateTime
Debug.Print "sLastModifiedDateTime = " & sLastModifiedDateTime
Debug.Print "sCBLClientURL = " & sCBLClientURL
Debug.Print "sCBLWebURL = " & sCBLWebURL
Debug.Print "sPNID = " & sPNID
Debug.Print "sPNDisplayName = " & sPNDisplayName
Debug.Print "sPNSelf = " & sPNSelf
Debug.Print "--------------------------------------------"
'return id of title
Debug.Print "Page with Title " & Chr(34) & sTitle & Chr(34) & " has page ID number " & sONID & "."
Debug.Print "--------------------------------------------"
Else
End If
Next
End Function