利用COM组件实现对WORD书签处写入值
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Word;
using System.Windows.Forms;
using System.IO;
using System.Reflection; namespace HustCAD.IntePLM.Win.BatchEnterWinUI
{
public class SignWord
{
//Word应用程序变量
_Application wordApp;
_Document wordDoc;
public bool signWord(string filePath, string bookMark, string code)
{
bool success = false;
try
{
//由于使用的是COM库,因此有许多变量需要用Missing.Value代替
wordApp = new ApplicationClass();
object missing = System.Reflection.Missing.Value;
object templateName = filePath;
wordDoc = wordApp.Documents.Open(ref templateName, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing);
if (wordApp.ActiveDocument.Bookmarks.Exists(bookMark))
{ object bk = (object)bookMark;
wordApp.ActiveDocument.Bookmarks.get_Item(ref bk).Select();
wordApp.Selection.TypeText(code);
//关闭wordDoc,wordApp对象
object SaveChanges = WdSaveOptions.wdSaveChanges;
object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat;
object RouteDocument = false;
wordDoc.Save();
wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
wordDoc = null;
wordApp = null;
success = true;
return true;
}
else
{
System.IO.File.AppendAllText(System.IO.Path.Combine(GetCurrentPath(), "signWordlog.txt"), "文件" + filePath + "签入编号失败,书签不存在! " + DateTime.Now.ToString("yyyy-MM-dd-hh:mm:ss")); //关闭wordDoc,wordApp对象
object SaveChanges = WdSaveOptions.wdSaveChanges;
object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat;
object RouteDocument = false;
wordDoc.Save();
wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
wordDoc = null;
wordApp = null;
return false;
}
}
catch (Exception e)
{
object SaveChanges = WdSaveOptions.wdSaveChanges;
object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat;
object RouteDocument = false;
//wordDoc.Save();
wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument); System.IO.File.AppendAllText(System.IO.Path.Combine(GetCurrentPath(), "signWordlog.txt"), "文件" + filePath + "签入编号失败! ,错误:" + e.Message.ToString() + "。 " + DateTime.Now.ToString("yyyy-MM-dd-hh:mm:ss"));
wordDoc = null;
wordApp = null;
return false; }
finally
{
try
{
object SaveChanges = WdSaveOptions.wdSaveChanges;
object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat;
object RouteDocument = false;
if (wordDoc!=null&&success==false )
{
wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
wordDoc = null;
}
if (wordApp != null && success == false)
{
wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
wordApp = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
catch { }
} }
/// <summary>
/// 得到当前程序的路径
/// </summary>
/// <returns></returns>
static public string GetCurrentPath()
{
string asstring = Assembly.GetExecutingAssembly().Location;
string[] aa = asstring.Split('\\');
string path = string.Empty;
foreach (string var in aa)
{
if (var != aa[aa.Length - 1])
path += var + @"\";
}
return path;
}
}
}
利用COM组件实现对WORD书签处写入值的更多相关文章
- 利用COM组件实现对WORD书签各种操作大全,看这一篇就够了
有个需求是,程序导出一份word报告,报告中有各种各样的表格,导出时还需要插入图片. 脑海中迅速闪过好几种组件,openxml组件,com组件,npoi.为了减少程序画复杂表格,我们选用了com组件+ ...
- python 使用win32com实现对word文档批量替换页眉页脚
最近由于工作需要,需要将70个word文件的页眉页脚全部进行修改,在想到这个无聊/重复/没有任何技术含量的工作时,我的内心是相当奔溃的.就在我接近奔溃的时候我突然想到完全可以用python脚本来实现这 ...
- jeecms系统使用介绍——通过二次开发实现对word、pdf、txt等上传附件的全文检索
转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/76912307 本文出自[我是干勾鱼的博客] 之前在文章<基于Java的门户 ...
- 【POI word】使用POI实现对Word的读取以及生成
项目结构如下: 那第一部分:先是读取Word文档 package com.it.WordTest; import java.io.FileInputStream; import java.io.Fil ...
- C#实现对Word文件读写[转]
手头上的一个项目报表相对比较简单,所以报表打印采用VBA引擎,通过定制Word模版,然后根据模版需要填充数据,然后OK,打印即可. 实现方法:首先需要引用VBA组建,我用的是Office2003 Pr ...
- 利用ini_set()函数实现对php配置文件的修改
PHP的配置文件是php.ini,如果要开启或者关闭扩展,还有设置一些模块的相关配置是,就得对该文件进行修改, 修改的方法也很简单,打开php.ini找到对应项直接修改,修改之后需要重新启动才能生效. ...
- 利用SMB jcifs实现对windows中的共享文件夹的操作
需求是在本地上传文件到服务器上,服务器是windows的,使用共享文件夹提供权限给你的. 利用第三方: CIFS (Common Internet File System) SMB(Server Me ...
- Android 利用xUtils框架实现对sqllite的增删改查
首先下载xUtils,下载地址:https://github.com/wyouflf/xUtils 把下载好的文件压缩,把里面的jar包拷进项目中如图所示: 这里新建一个User类进行测试增删改查 ...
- 利用shell脚本实现对mysql数据库的备份
#!/bin/bash #保存备份个数 number=3 #备份保存路径 backup_dir=/root/mysqlbackup #日期 dd=`date +%Y%m%d` #备份工具 tool=m ...
随机推荐
- MasterPage + UpdatePanel + FileUpload
上传文件在母版页与Ajax的UpdatePanel的环境进行.由于在母版内使用Ajax,建议使用AjaxControlToolkit.dll组件,去微软官网下载后,并拉入BIN目录中. 然后去web. ...
- UITouch
触摸事件基本都是发生在 viewController 中,首先触摸的对象是视图,而视图的类 UIView 继承了 UIRespnder 类,但是要对事件作出处理,还需要重写 UIResponder 类 ...
- loj #2008. 「SCOI2015」小凸想跑步
#2008. 「SCOI2015」小凸想跑步 题目描述 小凸晚上喜欢到操场跑步,今天他跑完两圈之后,他玩起了这样一个游戏. 操场是个凸 n nn 边形,N NN 个顶点按照逆时针从 0∼n−1 0 ...
- 浅谈python web框架django2.x
1.Django简介 Python下有多款不同的 Web 框架,Django是最有代表性的一种.许多成功的网站和APP都基于Django. Django是一个开源的Web应用框架,由Python写成. ...
- Fxx and game hdu 5945 单调队列dp
dfs你怕是要爆炸 考虑dp; 很容易想到 dp[ i ] 表示到 i 时的最少转移步数: 那么: dp[ i ]= min( dp[ i ],dp[ i-j ]+1 ); 其中 i-t<=j& ...
- Web安全工程师(进阶)课程表
01-SQL注入漏洞原理与利用 预备知识: 了解HTTP协议,了解常见的数据库.脚本语言.中间件.具备基本的编程语言基础. 授课大纲: 第一章:SQL注入基础 1.1 Web应用架构分析1.2 SQL ...
- php 常见递归实例
//计算数组{1,1,2,3,5,8.......} 第n位值 function Process1($i){ if ($i == 0) return 0; if ($i == 1) return 1; ...
- Django工程创建
方法一: 1.win+r进入cmd命令窗口: 2.找到Django的安装地址: 3.cmd窗口中利用cd 进入相应的文件夹,再输入命令如下: django-admin.exe startproject ...
- php 在 匿名函数中 调用自身。。
//php闭包实现函数的自调用,也就是实现递归 function closure($n,$counter,$max){ //匿名函数,这里函数的参数加&符号是,引址调用参数自己 $fn = f ...
- java程序员的从0到1:@Resource与@Autowired的比较
目录: 1.@Resource与@Autowired的源码分析 2.@Resource与@Autowired的相同点 3.@Resource与@Autowired的不同点 正文: 1.@Resourc ...