office2010 OneNote VBA / VB 官方范例 3
OneNote.fromVBA.DockWindow Dim oneNote As OneNote14.Application Set oneNote = New OneNote14.Application ### Office2010 OneNote VBA/VB官方示例3解析 #### 一、概述 本示例展示了如何利用Visual Basic for Applications (VBA) 和 Visual Basic (VB) 来控制Office2010中OneNote应用程序的窗口操作。OneNote本身并不支持VBA编程环境,但是可以通过其他支持VBA的Office应用(如Excel2010、PowerPoint2010或Word2010)来编写代码,并通过引用OneNote的对象库来实现对OneNote的自动化控制。 #### 二、代码解析 ##### 1. 引用外部库 在编写此脚本之前,需要在VBA环境中添加对`Microsoft OneNote14.0 Object Library`的引用。这一步骤是必要的,因为只有这样VBA才能识别OneNote的对象模型。 ##### 2. Option Explicit ```vb Option Explicit ``` 这条语句用于确保所有变量都必须被显式声明,这对于避免类型错误和提高代码可读性是非常重要的。 ##### 3. 创建OneNote 应用程序对象 ```vb Dim oneNote As OneNote14.Application Set oneNote = New OneNote14.Application ``` 这里创建了一个名为`oneNote`的对象实例,该实例指向了OneNote的应用程序对象。`Set`语句用于将一个对象变量与一个对象实例关联起来。 ##### 4. DockOneNoteWindow子程序 ```vb Public Sub DockOneNoteWindow() ``` 这是一个公开的过程,可以在任何地方调用它。它的功能是根据当前窗口的状态来决定是否将窗口停靠到屏幕右侧。 ##### 5. 连接到OneNote2010 ```vb If oneNote.Windows.CurrentWindow Is Nothing Then MsgBox "There isn't a visible OneNote2010 window. Please start OneNote." Else ' Get the Current Window Dim oneNoteWindow As OneNote14.Window Set oneNoteWindow = oneNote.Windows.CurrentWindow ``` 首先检查当前是否有可见的OneNote窗口。如果没有,则弹出消息框提示用户启动OneNote;如果有,则获取当前窗口的引用。 ##### 6. 判断并操作窗口 ```vb If Not oneNoteWindow.SideNote Then If oneNoteWindow.DockedLocation = dlNone Then oneNoteWindow.DockedLocation = dlRight Else oneNoteWindow.DockedLocation = dlNone End If Else MsgBox "The current OneNote window is a SideNote." End If End If End Sub ``` 这部分代码实现了主要的功能:如果当前窗口不是一个侧边笔记(SideNote),则检查其是否已经停靠。如果没有停靠,则将其停靠到屏幕右侧;如果已经停靠,则取消停靠。如果当前窗口是一个侧边笔记,则弹出消息框提示用户。 #### 三、重要概念解释 - **Application对象**:表示OneNote应用程序本身。 - **Windows集合**:包含打开的所有OneNote窗口。 - **CurrentWindow属性**:返回当前活动窗口的引用。 - **SideNote属性**:指示窗口是否为侧边笔记窗口。 - **DockedLocation属性**:用于设置或获取窗口的停靠位置。 #### 四、注意事项 - 在编写VBA脚本时,需要注意版本兼容性问题。不同版本的OneNote可能具有不同的对象模型。 - 当使用`Set`语句时,必须确保释放不再使用的对象,以避免内存泄漏。 - 使用VBA进行OneNote自动化时,需要确保OneNote已经启动并且至少有一个窗口处于活动状态。 - 在实际开发过程中,可能还需要处理更多复杂的窗口管理和用户交互逻辑。 通过上述分析,我们可以了解到这个示例脚本是如何通过VBA来控制OneNote窗口的操作,并且掌握了一些基本的概念和技巧。这对于那些希望利用VBA来增强Office应用程序功能的开发者来说是非常有用的。






























' OneNote 2010
' Demonstrates the Window object.
' Use any VBA host including Excel 2010, PowerPoint 2010,
' or Word 2010.
' OneNote 2010 is not a VBA host.
' In your VBA host, add references to the following
' external libraries using the Add References dialog:
' Microsoft OneNote 14.0 Object Library
' OneNote 2010 provides the ability to interact with
' the open OneNote user interface.
' DockOneNoteWindow retrieves the current Window.
' If the current window is not a SideNote window, it checks
' to see if the Window is not docked.
' If it's not, it's docked to the right of the screen.
' Otherwise the Window is undocked.
' Paste all this code into a module.
' Start OneNote 2010.
' Then place the cursor within the
' DockOneNoteWindow procedure,
' and press F5.
Public Sub DockOneNoteWindow()
' Connect to OneNote 2010.


- 粉丝: 3
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- Excel水力计算展示——挖深式消力池计算-演示2022优秀文档.ppt
- 网络营销方案提纲.doc
- 课程设计 药品进销存系统
- 电子商务客户细分与营销策略电商论文.doc
- 电子商务工作总结参考.pptx
- 水下传感器网络路由协议和仿真模型分析.doc
- 2021互联网公司合作协议范本.doc
- 素材2使用conda包管理程序安装第三方python软件包.docx
- 中国互联网的20个特色.pptx
- 算法合集之《偶算法及应用》.doc
- linux心得体会范文.doc
- 《图像处理的均衡化技术》课件.ppt
- 基于plc的液位控制系统设计.doc
- XX年9月计算机三级考试网络技术复习题及答案.doc
- 春八年英语下册Unit1SpringIsComingLesson3SunIsRising课件(新版).pptx
- 微软官方产品宣传动态PPT模板.pptx


