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. java java web及tomcat的使用

     java web及tomcat的使用 一.什么是java web: 参考百度百科: http://baike.baidu.com/link?url=HnaWXFD7wFfPAlFMW02GV6r5p ...

  2. loj#6363. 「地底蔷薇」(拉格朗日反演+多项式全家桶)

    题面 传送门 题解 肝了一个下午--我老是忘了拉格朗日反演计算的时候多项式要除以一个\(x\)--结果看它推倒简直一脸懵逼-- 做这题首先你得知道拉格朗日反演是个什么东西->这里 请坐稳,接下来 ...

  3. 5分钟构建无服务器敏感词过滤后端系统(基于FunctionGraph)

    摘要:开发者通过函数工作流,无需配置和管理服务器,以无服务器的方式构建应用,便能开发出一个弹性高可用的后端系统.托管函数具备以毫秒级弹性伸缩.免运维.高可靠的方式运行,极大地提高了开发和运维效率,减小 ...

  4. SIGHUP信号

    SIGHUP会在以下3种情况下被发送给相应的进程:1.终端关闭时,该信号被发送到session首进程以及作为job提交的进程(即用 & 符号提交的进程)2.session首进程退出时,该信号被 ...

  5. 江西财经大学第一届程序设计竞赛 D

    链接:https://www.nowcoder.com/acm/contest/115/D来源:牛客网 题目描述 事情,是这样的. 有这么一天双休日的中午. 我刚把我衣服扔进了洗衣机,然后拿了个小板凳 ...

  6. hdu3746 KMP-next数组的应用

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  7. jQuery form插件的使用--ajaxForm()和ajaxSubmit()

    转载:https://blog.csdn.net/qq_38602656/article/details/78668924

  8. php程序设计 1,2章节

    <?php/** * Created by PhpStorm. * User: DY040 * Date: 2017/9/4 * Time: 10:39 * * * 1: php起源于1995 ...

  9. java——调用一个静态方法的时候有没有执行这个类的构造方法,以及这个类中的静态常量?

    尝试一下: public class Try { final static int a = 1; public Try() { System.out.print("构造方法"); ...

  10. 单例模式+volatile禁止指令重排序

    单例模式: 单例,顾名思义就是只能有一个.不能再出现第二个.就如同地球上没有两片一模一样的树叶一样. 在这里就是说:一个类只能有一个实例,并且整个项目系统都能访问该实例. 单例模式共分为两大类: 懒汉 ...