Onenote代码高亮的实现方法

  1. 最终效果图
  • 最终的效果图如下:
    • clip_image001[4]
VBA的编写参考 VBA脚本复制到Word中并设置快捷键
  • Alt+F11 打开Word中的 VBS,将下列的VB代码复制 Normal 模板下的 Microsoft 对象下的 ThisDocument
    • 复制位置
      • clip_image002[4]
    • VB代码
      • #If VBA7 Then

            Private Declare PtrSafe Function timeGetTime Lib "winmm.dll" () As Long

        #Else

            Private Declare Function timeGetTime Lib "winmm.dll" () As Long

        #End If

         

        Sub EditCode()

            'Word 正文需要设置为无首行缩进

            'Notepad++不能以管理员的身份运行

            'Notepad++中,'Plugin Commands'中的 'Copy HTML to clipboard'需设置快捷键为 ALT + Shift + C

         

            AppActivate "NotePad++"

            SendKeys "%+c"

            ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _

                1, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _

                wdAutoFitFixed

            Dim Savetime As Double

            Savetime = timeGetTime '记下开始时的时间

            While timeGetTime < Savetime + 5 '循环等待

                DoEvents '转让控制权,以便让操作系统处理其它的事件

        Wend

           

        Selection.Paste

                '删除所有可编辑的区域

            ActiveDocument.DeleteAllEditableRanges wdEditorEveryone

            '添加可编辑区域

            Selection.Tables(1).Range.Editors.Add wdEditorEveryone

            '选中所有可编辑区域

            ActiveDocument.SelectAllEditableRanges wdEditorEveryone

           

           

           

            设置代码表格

           

            Selection.Cut

           

            '删除所有可编辑的区域

            ActiveDocument.DeleteAllEditableRanges wdEditorEveryone

           

            SendKeys ("^%({TAB})")

           

           

        End Sub

         

         

         Sub 设置代码表格()

        ' author: code4101

        ' 设置代码表格 宏

        '

        '

            ' 背景色为morning的配色方案,RGB(229,229,229)

            With Selection.Tables(1)

                With .Shading

                    .Texture = wdTextureNone

                    .ForegroundPatternColor = wdColorAutomatic

                    .BackgroundPatternColor = 15066597

                End With

                .Borders(wdBorderLeft).LineStyle = wdLineStyleNone

                .Borders(wdBorderRight).LineStyle = wdLineStyleNone

                .Borders(wdBorderTop).LineStyle = wdLineStyleNone

                .Borders(wdBorderBottom).LineStyle = wdLineStyleNone

                .Borders(wdBorderVertical).LineStyle = wdLineStyleNone

                .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone

                .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone

                .Borders.Shadow = False

                .AutoFitBehavior (wdAutoFitContent)  '自动调整大小

            End With

            With Options

                .DefaultBorderLineStyle = wdLineStyleSingle

                .DefaultBorderLineWidth = wdLineWidth050pt

                .DefaultBorderColor = wdColorAutomatic

            End With

             

            ' 段落无首行缩进,行间距为固定值12

            With Selection.ParagraphFormat

                .LeftIndent = CentimetersToPoints(0)

                .RightIndent = CentimetersToPoints(0)

                .SpaceBefore = 0

                .SpaceBeforeAuto = False

                .SpaceAfter = 0

                .SpaceAfterAuto = False

                .LineSpacingRule = wdLineSpaceExactly

                .LineSpacing = 12

                .KeepWithNext = False

                .KeepTogether = False

                .PageBreakBefore = False

                .NoLineNumber = False

                .Hyphenation = True

                .FirstLineIndent = CentimetersToPoints(0)

                .OutlineLevel = wdOutlineLevelBodyText

                .CharacterUnitLeftIndent = 0

                .CharacterUnitRightIndent = 0

                .CharacterUnitFirstLineIndent = 0

                .LineUnitBefore = 0

                .LineUnitAfter = 0

                .MirrorIndents = False

                .TextboxTightWrap = wdTightNone

                .AutoAdjustRightIndent = True

                .DisableLineHeightGrid = False

                .FarEastLineBreakControl = True

                .WordWrap = True

                .HangingPunctuation = True

                .HalfWidthPunctuationOnTopOfLine = False

                .AddSpaceBetweenFarEastAndAlpha = True

                .AddSpaceBetweenFarEastAndDigit = True

                .BaseLineAlignment = wdBaselineAlignAuto

            End With

            ' 清除原有的段落底纹

            Selection.ParagraphFormat.Shading.BackgroundPatternColor = wdColorAutomatic

            Selection.Font.Name = "Consolas"

        End Sub

  • 设置宏 EditCode的快捷键
    • 在Word选项的自定义功能区,选项自定义键盘快捷键
      • clip_image003[4]
    • EditCode设置为自己想要的快捷键,我设置的是 Alt + 1注意不要与现存的快捷键冲突
      • clip_image004[4]
  1. NotePad++设置
  • 确保NotePad++安装了NppExport插件,并设置 Copy HTML to clipboard 的快捷键为 Alt + Shift + C
    • clip_image005[4]
  1. 宏的使用
  • 首先确保只打开一个NotePad++窗口,并且把当前的文件设置为要复制的文件,而且要确保NotePad++ 中的代码是高亮状态。
  • 然后在Word中按之前设置好的快捷键,我设置的是ALt+1,等到出现Alt + Tab 页面时,就说明复制完成。
    • clip_image006[4]
  • 此时,直接在Onenote中按 Ctrl + V 即可完成复制
  • 使用注意事项
    • Word 正文需要设置为无首行缩进
    • 'Notepad++不能以管理员的身份运行
后续改进方向
  • 本来想让Word宏自动识别出Onenote窗口,并完成复制操作。但是Appactive好像不能很好的识别桌面版的 Onenote 窗口,测试了下,可以识别应用商店版的Onenote窗口,但是发送组合键Ctrl+V时,有时会出现失效,所以只是把相应的代码贴出来供大家参考吧。
    • 代码
      • #If VBA7 Then

            Private Declare PtrSafe Function timeGetTime Lib "winmm.dll" () As Long

        #Else

            Private Declare Function timeGetTime Lib "winmm.dll" () As Long

        #End If

         

        Sub EditCode()

            'Word 正文需要设置为无首行缩进

            'Notepad++不能以管理员的身份运行

            'Notepad++中,'Plugin Commands'中的 'Copy HTML to clipboard'需设置快捷键为 ALT + Shift + C

           

            AppActivate "NotePad++"

            SendKeys "%+c"

            ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _

                1, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _

                wdAutoFitFixed

            Dim Savetime As Double

            Savetime = timeGetTime '记下开始时的时间

            While timeGetTime < Savetime + 5 '循环等待

            DoEvents '转让控制权,以便让操作系统处理其它的事件

            Wend

               

            Selection.Paste

                '删除所有可编辑的区域

            ActiveDocument.DeleteAllEditableRanges wdEditorEveryone

            '添加可编辑区域

            Selection.Tables(1).Range.Editors.Add wdEditorEveryone

            '选中所有可编辑区域

            ActiveDocument.SelectAllEditableRanges wdEditorEveryone

           

           

           

            设置代码表格

           

            Selection.Cut

           

            '删除所有可编辑的区域

            ActiveDocument.DeleteAllEditableRanges wdEditorEveryone

           

            'SendKeys ("^%({TAB})")

            AppActivate "OneNote"

            SendKeys ("{ESC}{ESC}^v")

           

           

        End Sub

         

         

         Sub 设置代码表格()

        ' author: code4101

        ' 设置代码表格 宏

        '

        '

            ' 背景色为morning的配色方案,RGB(229,229,229)

            With Selection.Tables(1)

                With .Shading

                    .Texture = wdTextureNone

                    .ForegroundPatternColor = wdColorAutomatic

                    .BackgroundPatternColor = 15066597

                End With

                .Borders(wdBorderLeft).LineStyle = wdLineStyleNone

                .Borders(wdBorderRight).LineStyle = wdLineStyleNone

                .Borders(wdBorderTop).LineStyle = wdLineStyleNone

                .Borders(wdBorderBottom).LineStyle = wdLineStyleNone

                .Borders(wdBorderVertical).LineStyle = wdLineStyleNone

                .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone

                .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone

                .Borders.Shadow = False

                .AutoFitBehavior (wdAutoFitContent)  '自动调整大小

            End With

            With Options

                .DefaultBorderLineStyle = wdLineStyleSingle

                .DefaultBorderLineWidth = wdLineWidth050pt

                .DefaultBorderColor = wdColorAutomatic

            End With

             

            ' 段落无首行缩进,行间距为固定值12

            With Selection.ParagraphFormat

                .LeftIndent = CentimetersToPoints(0)

                .RightIndent = CentimetersToPoints(0)

                .SpaceBefore = 0

                .SpaceBeforeAuto = False

                .SpaceAfter = 0

                .SpaceAfterAuto = False

                .LineSpacingRule = wdLineSpaceExactly

                .LineSpacing = 12

                .KeepWithNext = False

                .KeepTogether = False

                .PageBreakBefore = False

                .NoLineNumber = False

                .Hyphenation = True

                .FirstLineIndent = CentimetersToPoints(0)

                .OutlineLevel = wdOutlineLevelBodyText

                .CharacterUnitLeftIndent = 0

                .CharacterUnitRightIndent = 0

                .CharacterUnitFirstLineIndent = 0

                .LineUnitBefore = 0

                .LineUnitAfter = 0

                .MirrorIndents = False

                .TextboxTightWrap = wdTightNone

                .AutoAdjustRightIndent = True

                .DisableLineHeightGrid = False

                .FarEastLineBreakControl = True

                .WordWrap = True

                .HangingPunctuation = True

                .HalfWidthPunctuationOnTopOfLine = False

                .AddSpaceBetweenFarEastAndAlpha = True

                .AddSpaceBetweenFarEastAndDigit = True

                .BaseLineAlignment = wdBaselineAlignAuto

            End With

            ' 清除原有的段落底纹

            Selection.ParagraphFormat.Shading.BackgroundPatternColor = wdColorAutomatic

            Selection.Font.Name = "Consolas"

        End Sub

    • 使用时,要先将Onenote中的光标放在要插入代码的位置,运行宏后可以实现自动插入。

转载于:https://www.cnblogs.com/lijunjie9502/p/9933548.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值