VBA code for Word to OneNote

G

GailTB

Based on the MrExcel code at http://www.mrexcel.com/tip078.shtml, this is the
code I'm using for pushing a Word document to OneNote. It needs the basGuid
module from the MrExcel site and the instructions for setting a reference
must also be followed.

Sub WordToOneNote()
' Requires basGuid module from above
Dim strTempFile As String
Dim intCharsPerLine As Integer
Dim intLineHeight As Integer
Dim intObjectGap As Integer
Dim intYCoord As Integer
Dim strSection As String
Dim strPageTitle As String
Dim strDate As String
Dim strPageGuid As String
Dim i As Integer
Dim strHTML As String
Dim strParagraph As String
Dim intChars As Integer
Dim intLines As Integer
Dim strXML As String
Dim strData As String

' Build a temporary XML file
strTempFile = Environ("TEMP") & "\OneNoteImport.xml"
On Error Resume Next
Kill (strTempFile)
On Error GoTo 0

intCharsPerLine = 90 'An approximation unless the font is fixed width
intLineHeight = 12 'Will depend on font size in OneNote
intObjectGap = 20 'Gap between last line of text in one object and first
line in next object
intYCoord = 50 'Starting y position
strSection = "Temp.one" 'Can be a new section or an existing one
strPageTitle = ActiveDocument.Paragraphs(1).Range 'First paragraph is
used for page title

Open strTempFile For Output As #1
Print #1, "<?xml version=""1.0""?> "
Print #1, "<Import
xmlns=""http://schemas.microsoft.com/office/onenote/2004/import""> "
strDate = Format(Date, "yyyy-mm-dd") & "T" & Time
strPageGuid = StGuidGen()
Print #1, " <EnsurePage path=""" & strSection & """"
Print #1, " guid=""" & strPageGuid & """ "
Print #1, " date=""" & strDate & """ "
Print #1, " title=""" & strPageTitle & """/>"
Print #1, ""
Print #1, " <PlaceObjects pagePath=""" & strSection & """ "
Print #1, " pageGuid=""" & strPageGuid & """>"
Print #1, ""

For i = 1 To ActiveDocument.Paragraphs.Count
Print #1, " <Object guid=""" & StGuidGen() & """>"
Print #1, " <Position x=""34"" y=""" & intYCoord & """/>"
Print #1, " <Outline width=""460"">"
Print #1, " <Html>"
Print #1, " <Data>"
Print #1, " <![CDATA["
' Build the HTML string
strHTML = "<html><body>"
strParagraph = ActiveDocument.Paragraphs(i).Range
strHTML = strHTML + "<p>" & strParagraph & "</p>"
strHTML = strHTML & "</body></html>"
' Write out the HTML string
Print #1, strHTML
Print #1, " ]]>"
Print #1, " </Data>"
Print #1, " </Html>"
Print #1, " </Outline>"
Print #1, " </Object>"
Print #1, ""
intChars = ActiveDocument.Paragraphs(i).Range.Characters.Count
intLines = (intChars + intCharsPerLine) \ intCharsPerLine
intYCoord = intYCoord + intObjectGap + (intLineHeight * intLines)
Next i

Print #1, " </PlaceObjects>"
Print #1, ""
Print #1, "</Import>"
Close #1

' Load the temp file into a string variable
strXML = ""
Open strTempFile For Input As #1
Do
Line Input #1, strData
strXML = strXML & strData
Loop While EOF(1) = False
Close #1

Dim CSI As New CSimpleImporter
' Import the string into OneNote
CSI.Import strXML

' Navigate to the page
CSI.NavigateToPage bstrPath:=strSection, bStrGuid:=strPageGuid

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top