Hello,
I am looking for a way to automate the process of creating a new page in a specific section of OneNote and pasting content from Excel into it. After some research, I found a VBA code that seems to do the job. Here's the code:
Code
Sub CopyToOneNote()
Dim notebookName As String
notebookName = "GP Rounds" 'change this to the name of your notebook
Dim sectionName As String
sectionName = "Patient Name" 'change this to the name of your section
Dim pageName As String
pageName = "New page" 'change this to the name of your new page
'Get the content from the last row of column B
Dim content As String
content = Range("B" & Cells.Rows.count).End(xlUp).value
'open the OneNote app
Shell "C:\Program Files\Microsoft Office\root\Office16\ONENOTE.EXE"
'wait for the OneNote app to open
Application.Wait (Now + TimeValue("0:00:05"))
'send the keyboard shortcut to create a new page
Application.SendKeys ("^n")
'wait for the new page to be created
Application.Wait (Now + TimeValue("0:00:02"))
'send the keyboard shortcut to focus on the page title
Application.SendKeys ("%p")
'type the page title
Application.SendKeys (pageName)
'navigate to the desired notebook and section
Application.SendKeys "^g"
Application.SendKeys notebookName
Application.SendKeys "%s"
Application.Wait (Now + TimeValue("0:00:01"))
Application.SendKeys sectionName
Application.Wait (Now + TimeValue("0:00:01"))
Application.SendKeys "{ENTER}"
Application.SendKeys ("%g")
Application.SendKeys ("%g")
Application.SendKeys (pageName)
'wait for the navigation to complete
Application.Wait (Now + TimeValue("0:00:02"))
'copy the content to the Clipboard
Range("B" & Cells.Rows.count).End(xlUp).Copy
'send the keyboard shortcut to paste the content into OneNote
Application.SendKeys ("^v")
End Sub
Display More
However, I am running into an issue where the code directs me to OneNote, but not to the specific section that I want to create the new page in. Instead, it sometimes takes me to a random section or the one that is already opened in the notebook. Could you please advise me on how to modify the code to ensure that it directs me to the correct section in my OneNote notebook?
Thank you!
Best regards,
Tommaso