1. 最终效果图
    • 最终的效果图如下:
      • Onenote代码高亮的实现方法
  • VBA的编写参考
  • VBA脚本复制到Word中并设置快捷键
    • Alt+F11 打开Word中的 VBS,将下列的VB代码复制 Normal 模板下的 Microsoft 对象下的 ThisDocument
      • 复制位置
        • Onenote代码高亮的实现方法
      • 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选项的自定义功能区,选项自定义键盘快捷键
        • Onenote代码高亮的实现方法
      • EditCode设置为自己想要的快捷键,我设置的是 Alt + 1注意不要与现存的快捷键冲突
        • Onenote代码高亮的实现方法
    1. NotePad++设置
    • 确保NotePad++安装了NppExport插件,并设置 Copy HTML to clipboard 的快捷键为 Alt + Shift + C
      • Onenote代码高亮的实现方法
    1. 宏的使用
    • 首先确保只打开一个NotePad++窗口,并且把当前的文件设置为要复制的文件,而且要确保NotePad++ 中的代码是高亮状态。
    • 然后在Word中按之前设置好的快捷键,我设置的是ALt+1,等到出现Alt + Tab 页面时,就说明复制完成。
      • Onenote代码高亮的实现方法
    • 此时,直接在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中的光标放在要插入代码的位置,运行宏后可以实现自动插入。

    相关文章:

    • 2022-12-23
    • 2022-01-19
    • 2022-12-23
    • 2021-05-21
    • 2021-06-23
    • 2021-05-03
    猜你喜欢
    • 2021-06-20
    • 2021-11-23
    • 2021-07-14
    • 2021-12-12
    • 2021-05-11
    • 2021-05-24
    • 2021-04-29
    相关资源
    相似解决方案