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. [python篇][其他] python博客学习汇总

    http://blog.csdn.net/zhangxinrun/article/details/8141913

  2. C/C++、Java、Python谁是编译型语言,谁是解释型语言?

    最近各大互联网公司线上笔试,编程题目里的编译器只支持C/C++.Java,甚至有的支持javaScrpit和Pascal,就是不支持Python.让一直以来用惯了Python的我直吐血,于是今天痛定思 ...

  3. 自定义AlertView(Swift)

    MyAlertView.swift // Pop Up Styles enum MyAlertViewStyle: Int { case success case error case notice ...

  4. HDU——2066一个人的旅行(优先队列SPFA水题)

    一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  5. 学习 WebService 第四步:利用WSDL(URL)生成WebService客户端<初级>

    我用的是最简单的方法,利用jdk的命令wsimport -keep -p 包路径 -d 代码存放位置 WSDL网址 蓝色是命令,粉色是存放位置,橘色是URL C:\Program Files\IBM\ ...

  6. tomcat Enabling JMX Remote

    wiki 利用JMX做存活监控 cat /opt/wiki/work/bin/setenv.sh | grep jmxremoteCATALINA_OPTS="-Dcom.sun.manag ...

  7. 【CF1023C】Bracket Subsequence(模拟)

    题意:给定一个正则括号序列 s ,让你在当中选择一个长度正好为 t 的子串,使得 t 恰好也是一个正则括号序列 思路:用栈模拟 #include<cstdio> #include<c ...

  8. HRBUST 2064:萌萌哒十五酱的宠物~(最近公共祖先LCA)

    题意:一个n个点的树,询问某两点之间的简单路径,问路径上任选三边能否组成一个三角形. N<100000,权值<109 思路: 这里最神奇的思路过于以下这个: n个数,任意三个都不能组成三角 ...

  9. 一个页面多个ng-app注意事项

    1.一个页面会自动加载第一个ng-app 2.如果想启动其它ng-app,需要通过下列代码的红色部分来启动,此时一共启动了2个ng-app 3.特别注意:代码红色部分一定要放在最后,比如,不能放在蓝色 ...

  10. UE3客户端服务器GamePlay框架

    客户端(当前玩家)与服务器对应关系图: 整体上看,UE3的GamePlay框架使用的是MVC架构 ① 橙色的Actor对象及橙色箭头相连的成员变量只会被同步给Owner客户端 Controller:控 ...