private void InsertMacro()
{
Word.Application oWord;
Word.Document oDoc;
VBIDE.VBComponent oModule;
Office.CommandBar oCommandBar;
Office.CommandBarButton oCommandBarButton;
String sCode;
Object oMissing = System.Reflection.Missing.Value; oWord = new Word.Application();
oDoc = oWord.Documents.Open(fileName);
//oDoc = oWord.Documents.Add(oMissing);
try
{
// Create a new VBA code module.
oModule = oDoc.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule);
sCode =
"sub AutoOpen()\r\n" +
"Application.DisplayAlerts = False \r\n" +
" msgbox \"VBA Macro called\"\r\n" +
"Application.DisplayAlerts = True \r\n" +
"end sub";
// Add the VBA macro to the new code module.
oModule.CodeModule.AddFromString(sCode);
}
catch (Exception e)
{
if (e.ToString().Contains("不被信任"))
MessageBox.Show("到 Visual Basic Project 的程序访问不被信任", "Error");
return;
}
try
{
// Create a new toolbar and show it to the user.
oCommandBar = oWord.CommandBars.Add("VBAMacroCommandBar", oMissing, oMissing);
oCommandBar.Visible = true;
// Create a new button on the toolbar.
oCommandBarButton = (Office.CommandBarButton)oCommandBar.Controls.Add(
Office.MsoControlType.msoControlButton,
oMissing, oMissing, oMissing, oMissing);
// Assign a macro to the button.
oCommandBarButton.OnAction = "VBAMacro";
// Set the caption of the button.
oCommandBarButton.Caption = "Call VBAMacro";
// Set the icon on the button to a picture.
oCommandBarButton.FaceId = ;
}
catch (Exception e)
{
MessageBox.Show("VBA宏命令已经存在.", "Error");
} oWord.Documents.Save();
//oWord.Visible = true; oCommandBarButton = null;
oCommandBar = null;
oModule = null;
oDoc = null;
oWord = null;
GC.Collect();
}

C#给指定doc文件写入宏的更多相关文章

  1. 视频文件写入转换之图像处理-OpenCV应用学习笔记五

    在<笔记二>中我们做了视频播放和控制的实现,仅仅算是完成了对视频文件的读取操作:今天我们来一起练习下对视频文件的写入操作:格式转换. 实现功能: 打开一个视频文件play.avi,读取文件 ...

  2. 使用POI读写Word doc文件

    使用POI读写word doc文件 目录 1     读word doc文件 1.1     通过WordExtractor读文件 1.2     通过HWPFDocument读文件 2     写w ...

  3. android使用POI读写word doc文件

    目录 1     读word doc文件 1.1     通过WordExtractor读文件 1.2     通过HWPFDocument读文件 2     写word doc文件 Apache p ...

  4. 在文件夹中 的指定类型文件中 查找字符串(CodeBlocks+GCC编译,控制台程序,仅能在Windows上运行)

    说明: 程序使用 io.h 中的 _findfirst 和 _findnext 函数遍历文件夹,故而程序只能在 Windows 下使用. 程序遍历当前文件夹,对其中的文件夹执行递归遍历.同时检查遍历到 ...

  5. 将gridFS中的图片文件写入硬盘

    开启用户验证下的gridfs 连接使用,在执行脚本前可以在python shell中 from pymongo import Connectionfrom gridfs import *con = C ...

  6. PHP文件读写操作之文件写入代码

    在PHP网站开发中,存储数据通常有两种方式,一种以文本文件方式存储,比如txt文件,一种是以数据库方式存储,比如Mysql,相对于数据库存储,文件存储并没有什么优势,但是文件读写操作在基本的PHP开发 ...

  7. iOS案例:读取指定txt文件,并把文件中的内容输出出来

    用到的是NSString中的initWithContentsOfFile: encoding方法 // // main.m // 读取指定文件并输出内容 // // Created by Apple ...

  8. 浅谈如何使用代码为MP3文件写入ID3Tags

    作者:郑童宇 GitHub:https://github.com/CrazyZty 1.前言 做了三年左右的Android开发,一直没写过博客,最近正好打算换工作,算是闲一些,就将以前开发所遇到的一些 ...

  9. 文件操作ofstream,open,close,ifstream,fin,依照行来读取数据, fstream,iosin iosout,fio.seekg(),文件写入和文件读写,文件拷贝和文件

     1.ofstream,open,close 写入文件 #include<iostream> #include<fstream> using namespace std; ...

随机推荐

  1. linux下终止相关操作

    Ctrl c 中止当前正在执行的程序. Ctrl d 相当于exit命令,退出当前会话. Ctrl z 将当前运行的程序放到后台运行.与运行时加&类似.

  2. rabbitmq exchange type

    This is the fourth installment to the series: RabbitMQ for Windows.  In thelast installment, we revi ...

  3. 九度oj 题目1184:二叉树遍历

    题目描述: 编一个程序,读入用户输入的一串先序遍历字符串,根据此字符串建立一个二叉树(以指针方式存储). 例如如下的先序遍历字符串:ABC##DE#G##F###其中“#”表示的是空格,空格字符代表空 ...

  4. C++之Effective STL学习笔记Item21

    好了,先贴一段英文,真心不想翻译过来,而且感觉也翻译不到那么到位: The STL is awash in comparisons of objects to see if they have the ...

  5. 【bzoj3261】最大异或和 可持久化Trie树

    题目描述 给定一个非负整数序列 {a},初始长度为 N.       有M个操作,有以下两种操作类型:1.A x:添加操作,表示在序列末尾添加一个数 x,序列的长度 N+1.2.Q l r x:询问操 ...

  6. java面试题之如何判断一个对象是否应该被回收

    常用的有两种办法: 引用计数法:(无法解决对象循环引用的问题,导致对象无法被回收) 可达性分析:

  7. 单线程实现并发——协程,gevent模块

    一 并发的本质 1 切换 2 保存状态 二 协程的概念 协程,又称微线程,纤程.英文名Coroutine.单线程下实现并发,用户从应用程序级别控制单线程下任务的切换,注意一定是遇到I/O才切. 协程的 ...

  8. @login_required用法简介

    在django项目中,经常会看到下面这样的代码: from django.contrib.auth.decorators import login_required @login_required d ...

  9. java mail Received fatal alert: handshake_failure java 无法发送邮件问题 java 发送qq邮件(含源码)

     java 无法发送邮件问题 java 发送qq邮件 报错:java mail Received fatal alert: handshake_failure (使用ssl) javax.mail.M ...

  10. (6)C#事务处理

    为了方便移到了ADO.NET分类里 事务的主要特征是,任务要么全部完成,要么都不完成 事务常用于写入或更新数据库中的数据.将数据写入文件或注册表也可以使用事物. ADO.NET不支持跨越多个连接的事物 ...