1.      private void InsertMacro()
  2. {
  3. Word.Application oWord;
  4. Word.Document oDoc;
  5. VBIDE.VBComponent oModule;
  6. Office.CommandBar oCommandBar;
  7. Office.CommandBarButton oCommandBarButton;
  8. String sCode;
  9. Object oMissing = System.Reflection.Missing.Value;
  10.  
  11. oWord = new Word.Application();
  12. oDoc = oWord.Documents.Open(fileName);
  13. //oDoc = oWord.Documents.Add(oMissing);
  14. try
  15. {
  16. // Create a new VBA code module.
  17. oModule = oDoc.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule);
  18. sCode =
  19. "sub AutoOpen()\r\n" +
  20. "Application.DisplayAlerts = False \r\n" +
  21. " msgbox \"VBA Macro called\"\r\n" +
  22. "Application.DisplayAlerts = True \r\n" +
  23. "end sub";
  24. // Add the VBA macro to the new code module.
  25. oModule.CodeModule.AddFromString(sCode);
  26. }
  27. catch (Exception e)
  28. {
  29. if (e.ToString().Contains("不被信任"))
  30. MessageBox.Show("到 Visual Basic Project 的程序访问不被信任", "Error");
  31. return;
  32. }
  33. try
  34. {
  35. // Create a new toolbar and show it to the user.
  36. oCommandBar = oWord.CommandBars.Add("VBAMacroCommandBar", oMissing, oMissing);
  37. oCommandBar.Visible = true;
  38. // Create a new button on the toolbar.
  39. oCommandBarButton = (Office.CommandBarButton)oCommandBar.Controls.Add(
  40. Office.MsoControlType.msoControlButton,
  41. oMissing, oMissing, oMissing, oMissing);
  42. // Assign a macro to the button.
  43. oCommandBarButton.OnAction = "VBAMacro";
  44. // Set the caption of the button.
  45. oCommandBarButton.Caption = "Call VBAMacro";
  46. // Set the icon on the button to a picture.
  47. oCommandBarButton.FaceId = ;
  48. }
  49. catch (Exception e)
  50. {
  51. MessageBox.Show("VBA宏命令已经存在.", "Error");
  52. }
  53.  
  54. oWord.Documents.Save();
  55. //oWord.Visible = true;
  56.  
  57. oCommandBarButton = null;
  58. oCommandBar = null;
  59. oModule = null;
  60. oDoc = null;
  61. oWord = null;
  62. GC.Collect();
  63. }

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. phpcms 后台也名称

    announce 公告 show.html 内容页 comment 评论 show_list.html 内容页评论列表 list.html 评论列表 content 内容模型 category.htm ...

  2. CCF第四题无向图打印路径 欧拉问题

    #include<iostream> #include<vector> #include<algorithm> #include<stack> #def ...

  3. java2 实用教程第四章

    博主原创 转载请注明地址 博客:http://www.cnblogs.com/13224ACMer/ 1成员变量 声明变量所声明的变量被称为成员变量和域变量,成员变量在类中的书写位置与前后顺序无关, ...

  4. Unity3D - UGUI的初级应用

    添加字体: 把下载好的字体拖拽到Project面板中 - 点击Text组件中Text属性后面的圆点 - 选择刚刚拖拽的字体即可. 创建ToggleGroup(开关组): 1.在Canvas下创建两个T ...

  5. NOJ——聊天止于呵呵(string流重定向+map,水题)

    [1645] 聊天止于呵呵 时间限制: 5000 ms 内存限制: 65535 K 问题描述 (现代版)俗话说:流言止于智者,聊天止于呵呵.输入一段聊天记录,你的任务是数一数有 多少段对话“止于呵呵” ...

  6. HDU——1215七夕节(因数和)

    七夕节 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  7. net4:MultiView(view)行为中的commmandname与commmandargument,ListBox的使用及移动操作

    原文发布时间为:2008-07-29 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...

  8. PhpStorm配置svn:Can't use Subversion command line client:svn

    Can't use Subversion command line client:svn 感谢: 萌芽的绿豆的文章:https://www.cnblogs.com/yuanchaoyong/p/616 ...

  9. 矩阵乘法加速fib数列

    考虑矩阵(1,1)(1,0) #include<cstdio> #include<cstring> #include<iostream> using namespa ...

  10. BZOJ题目(持续更新)

    bzoj1009:kmp想法+递推+矩阵快速幂.很好的想法,考虑用长串去kmp匹配短串,dp[i][j]表示匹配指针分别指在i.j位置时候,前i位母字符串一共有多少种可能性,那么dp[i][j]=Σd ...