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书签处写入值的更多相关文章

  1. 利用COM组件实现对WORD书签各种操作大全,看这一篇就够了

    有个需求是,程序导出一份word报告,报告中有各种各样的表格,导出时还需要插入图片. 脑海中迅速闪过好几种组件,openxml组件,com组件,npoi.为了减少程序画复杂表格,我们选用了com组件+ ...

  2. python 使用win32com实现对word文档批量替换页眉页脚

    最近由于工作需要,需要将70个word文件的页眉页脚全部进行修改,在想到这个无聊/重复/没有任何技术含量的工作时,我的内心是相当奔溃的.就在我接近奔溃的时候我突然想到完全可以用python脚本来实现这 ...

  3. jeecms系统使用介绍——通过二次开发实现对word、pdf、txt等上传附件的全文检索

    转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/76912307 本文出自[我是干勾鱼的博客] 之前在文章<基于Java的门户 ...

  4. 【POI word】使用POI实现对Word的读取以及生成

    项目结构如下: 那第一部分:先是读取Word文档 package com.it.WordTest; import java.io.FileInputStream; import java.io.Fil ...

  5. C#实现对Word文件读写[转]

    手头上的一个项目报表相对比较简单,所以报表打印采用VBA引擎,通过定制Word模版,然后根据模版需要填充数据,然后OK,打印即可. 实现方法:首先需要引用VBA组建,我用的是Office2003 Pr ...

  6. 利用ini_set()函数实现对php配置文件的修改

    PHP的配置文件是php.ini,如果要开启或者关闭扩展,还有设置一些模块的相关配置是,就得对该文件进行修改, 修改的方法也很简单,打开php.ini找到对应项直接修改,修改之后需要重新启动才能生效. ...

  7. 利用SMB jcifs实现对windows中的共享文件夹的操作

    需求是在本地上传文件到服务器上,服务器是windows的,使用共享文件夹提供权限给你的. 利用第三方: CIFS (Common Internet File System) SMB(Server Me ...

  8. Android 利用xUtils框架实现对sqllite的增删改查

    首先下载xUtils,下载地址:https://github.com/wyouflf/xUtils  把下载好的文件压缩,把里面的jar包拷进项目中如图所示: 这里新建一个User类进行测试增删改查 ...

  9. 利用shell脚本实现对mysql数据库的备份

    #!/bin/bash #保存备份个数 number=3 #备份保存路径 backup_dir=/root/mysqlbackup #日期 dd=`date +%Y%m%d` #备份工具 tool=m ...

随机推荐

  1. AngularJs 指令 directive中link,controller 的区别

    其实严格来讲,link和controller是完全不同的概念,这里讲区别有点牵强. angular指令中,带有link和controller两个函数,很多人在写指令的时候不知道是写在link里 还是c ...

  2. 洛谷 P2447 [SDOI2010]外星千足虫

    P2447 [SDOI2010]外星千足虫 题目描述 公元2089年6月4日,在经历了17年零3个月的漫长旅行后,“格纳格鲁一号”载人火箭返回舱终于安全着陆.此枚火箭由美国国家航空航天局(NASA)研 ...

  3. java webservices 以Axis1.4方式 调用sap webservice接口.

    1. 首先需要下载Axis1.4 jar包,这个必应搜索大把,下载下来后把jar包加入eclipse工程项目路径中即可. 2. 下载mail.jar和activation.jar 俩个包.下载地址:h ...

  4. 3、OpenCV Python 色彩空间

    __author__ = "WSX" import cv2 as cv import numpy as np def color_space( img ): gray_img = ...

  5. 微信小程序强制横屏办法

    最近想学习学习微信小程序开发,本着先设计,再查找具体实现的方法的想法,在进行数据统计时,想着竖屏展示数据会造成重叠,或者数据显示不全而用省略号代替的问题,所以计划采用横屏的方式显示数据表格. 搜索到两 ...

  6. P1110 [ZJOI2007]报表统计 (multiset)

    [题目链接] https://www.luogu.org/problemnew/show/P1110 有以下三种操作: INSERT i k:在原数列的第i个元素后面添加一个新元素k:如果原数列的第i ...

  7. Gym - 101572G Galactic Collegiate Programming Contest 小根堆(set)

    题目传送门 题目大意: n个人,m次提交,每次提交都代表某支队伍做出一题,并且给出罚时,让你输出每次提交后,编号为1的队伍的排名. 思路: 首先处理ac和罚时,由于罚时最大1000,最多有1e5次,要 ...

  8. poj2083 分形(图形的递归)

    题目传送门 代码有注释. #include<iostream> #include<algorithm> #include<cstdlib> #include< ...

  9. 主席树的各类模板(区间第k大数【动,静】,区间不同数的个数,区间<=k的个数)

    取板粗   好东西来的 1.(HDOJ2665)http://acm.hdu.edu.cn/showproblem.php?pid=2665 (POJ2104)http://poj.org/probl ...

  10. thinkphp5 composer安装验证码

    1,安装composer,选择安装到的php的版本.在使用phpstudy的时候 用的是php5.5 .注意phpstudy的安装路径. 2.检查composer是否安装成功.cmd 然后输入comp ...