C#小程序,调用WORD修改文档.请问调用哪个OFFICE组件?程序的实现思想是调用WORD命令,原理类似于WORD的VBA宏。
这个程序的前一个版本就是调用事先做好的宏,但其出错情况难以控制,便改为现在的情况。这个程序在本机及有限的几个同学电脑上测试通过,并无报任何错误,而且效果不错。
只是在一些电脑上运行时,总会出现问题。
询问下不能解决问题,所以把程序拿上来与达人求教。
问题可能出现在:操作系统与OFFICE套件的安装上,也无法排除程序的缺陷。
出问题机器的系统为GHOST XP,自带OFFICE套件。
源代码private void handelFile() //处理文件
{
bool isError = false; string thisTime = getCurrentTime();
string thisLog = this.Text + "\r\n" + thisTime + "\r\n\r\n";
int thisThread = selectedThread;
//以当前时间为名建立文件夹
Directory.CreateDirectory(jobDist[thisThread] + "\\" + thisTime); Word.ApplicationClass MyWord = null;
Object Nothing = System.Reflection.Missing.Value;
Word.Document MyDoc = null;
Word.Document dotDoc = null; try
{
MyWord = new Word.ApplicationClass(); //打开模板文件
object readOnly = true;
dotDoc = MyWord.Documents.Open(ref jobDot[thisThread], ref Nothing, ref readOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
Word.Sections dotSections = dotDoc.Sections;
Word.Section dotSection = null; //取页面信息
//foreach (Word.Section section in dotDoc.Sections)
{
dotSection = dotSections[1];//section;//
} //开始处理
for (int i = 0; i < jobListBox[thisThread].Items.Count; i++)
{
try
{
//选择一个待操作文件
jobListBox[thisThread].SelectedIndex = i;
//源文件名
object source = jobSour[thisThread] + "\\" + jobListBox[thisThread].SelectedItem.ToString();
//打开指定文件
MyDoc = MyWord.Documents.Open(ref source, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//处理
//查找删除
object replaceAll = Word.WdReplace.wdReplaceAll;
MyWord.Selection.Find.ClearFormatting();
MyWord.Selection.Find.Text = tBFind.Text;
MyWord.Selection.Find.Replacement.ClearFormatting();
MyWord.Selection.Find.Replacement.Text = tBRepl.Text;
MyWord.Selection.Find.Execute(
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref replaceAll, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//页面设置
MyDoc.PageSetup.Orientation = dotDoc.PageSetup.Orientation; //方向
MyDoc.PageSetup.LeftMargin = dotDoc.PageSetup.LeftMargin; //边距
MyDoc.PageSetup.RightMargin = dotDoc.PageSetup.RightMargin;
MyDoc.PageSetup.TopMargin = dotDoc.PageSetup.TopMargin;
MyDoc.PageSetup.BottomMargin = dotDoc.PageSetup.BottomMargin;
MyDoc.PageSetup.PaperSize = dotDoc.PageSetup.PaperSize; //纸张大小
MyDoc.PageSetup.HeaderDistance = dotDoc.PageSetup.HeaderDistance; //页眉页脚边距
MyDoc.PageSetup.FooterDistance = dotDoc.PageSetup.FooterDistance;
//页眉页脚
int ct = 0;
foreach (Word.Section section in MyDoc.Sections)
{
object myUnit = Word.WdUnits.wdStory;
//head
if (ct > 0)
{
section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].LinkToPrevious = true;
}
else
{
dotSection.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Copy();
section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Delete(ref Nothing, ref Nothing);
section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Paste();
MyWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader;
MyWord.Selection.EndKey(ref myUnit, ref Nothing);
MyWord.Selection.Delete(ref Nothing, ref Nothing);
}
//foot
if (ct > 0)
{
section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].LinkToPrevious = true;
}
else
{
dotSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Copy();
section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Delete(ref Nothing, ref Nothing);
section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Paste();
MyWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter;
MyWord.Selection.EndKey(ref myUnit, ref Nothing);
MyWord.Selection.Delete(ref Nothing, ref Nothing);
ct++;
}
}
//设置属性
if (cBSetProt.Checked == true)
setCopyRight(MyWord, MyDoc); //目地文件名
object NewFileName = jobDist[thisThread] + "\\" + thisTime + "\\" + jobListBox[thisThread].SelectedItem.ToString();
//将WordDoc文档对象的内容保存为DOC文档
MyDoc.SaveAs(ref NewFileName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
}
catch (Exception Err)
{
thisLog += jobListBox[thisThread].SelectedItem.ToString() + "\r\n" + Err.Message + "\r\n=====\r\n";
isError = true;
}
finally
{
//关闭WordDoc文档对象
MyDoc.Close(ref Nothing, ref Nothing, ref Nothing);
jobProc[thisThread].Text = Convert.ToString(100 * (i + 1) / jobListBox[thisThread].Items.Count) + "%";
} //终止操作
if (jobStop[thisThread].Checked)
{
i = jobListBox[thisThread].Items.Count;
}
}
}
catch (Exception Err)
{
MessageBox.Show(Err.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
//关闭模板文件
dotDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭WordApp组件对象
MyWord.Quit(ref Nothing, ref Nothing, ref Nothing);
} if (isError)
{
logFile(thisLog, thisThread, thisTime);
MessageBox.Show("处理完毕!\r\n\r\n有错误,请查看日志文件。\r\n于文件夹:" + thisTime, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
} //清理该线程,以便下次使用
jobProc[thisThread].Visible = false;
jobListBox[thisThread].Visible = false;
jobStop[thisThread].Visible = false;
}
这个程序的前一个版本就是调用事先做好的宏,但其出错情况难以控制,便改为现在的情况。这个程序在本机及有限的几个同学电脑上测试通过,并无报任何错误,而且效果不错。
只是在一些电脑上运行时,总会出现问题。
询问下不能解决问题,所以把程序拿上来与达人求教。
问题可能出现在:操作系统与OFFICE套件的安装上,也无法排除程序的缺陷。
出问题机器的系统为GHOST XP,自带OFFICE套件。
源代码private void handelFile() //处理文件
{
bool isError = false; string thisTime = getCurrentTime();
string thisLog = this.Text + "\r\n" + thisTime + "\r\n\r\n";
int thisThread = selectedThread;
//以当前时间为名建立文件夹
Directory.CreateDirectory(jobDist[thisThread] + "\\" + thisTime); Word.ApplicationClass MyWord = null;
Object Nothing = System.Reflection.Missing.Value;
Word.Document MyDoc = null;
Word.Document dotDoc = null; try
{
MyWord = new Word.ApplicationClass(); //打开模板文件
object readOnly = true;
dotDoc = MyWord.Documents.Open(ref jobDot[thisThread], ref Nothing, ref readOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
Word.Sections dotSections = dotDoc.Sections;
Word.Section dotSection = null; //取页面信息
//foreach (Word.Section section in dotDoc.Sections)
{
dotSection = dotSections[1];//section;//
} //开始处理
for (int i = 0; i < jobListBox[thisThread].Items.Count; i++)
{
try
{
//选择一个待操作文件
jobListBox[thisThread].SelectedIndex = i;
//源文件名
object source = jobSour[thisThread] + "\\" + jobListBox[thisThread].SelectedItem.ToString();
//打开指定文件
MyDoc = MyWord.Documents.Open(ref source, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//处理
//查找删除
object replaceAll = Word.WdReplace.wdReplaceAll;
MyWord.Selection.Find.ClearFormatting();
MyWord.Selection.Find.Text = tBFind.Text;
MyWord.Selection.Find.Replacement.ClearFormatting();
MyWord.Selection.Find.Replacement.Text = tBRepl.Text;
MyWord.Selection.Find.Execute(
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref replaceAll, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//页面设置
MyDoc.PageSetup.Orientation = dotDoc.PageSetup.Orientation; //方向
MyDoc.PageSetup.LeftMargin = dotDoc.PageSetup.LeftMargin; //边距
MyDoc.PageSetup.RightMargin = dotDoc.PageSetup.RightMargin;
MyDoc.PageSetup.TopMargin = dotDoc.PageSetup.TopMargin;
MyDoc.PageSetup.BottomMargin = dotDoc.PageSetup.BottomMargin;
MyDoc.PageSetup.PaperSize = dotDoc.PageSetup.PaperSize; //纸张大小
MyDoc.PageSetup.HeaderDistance = dotDoc.PageSetup.HeaderDistance; //页眉页脚边距
MyDoc.PageSetup.FooterDistance = dotDoc.PageSetup.FooterDistance;
//页眉页脚
int ct = 0;
foreach (Word.Section section in MyDoc.Sections)
{
object myUnit = Word.WdUnits.wdStory;
//head
if (ct > 0)
{
section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].LinkToPrevious = true;
}
else
{
dotSection.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Copy();
section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Delete(ref Nothing, ref Nothing);
section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Paste();
MyWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader;
MyWord.Selection.EndKey(ref myUnit, ref Nothing);
MyWord.Selection.Delete(ref Nothing, ref Nothing);
}
//foot
if (ct > 0)
{
section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].LinkToPrevious = true;
}
else
{
dotSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Copy();
section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Delete(ref Nothing, ref Nothing);
section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Paste();
MyWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter;
MyWord.Selection.EndKey(ref myUnit, ref Nothing);
MyWord.Selection.Delete(ref Nothing, ref Nothing);
ct++;
}
}
//设置属性
if (cBSetProt.Checked == true)
setCopyRight(MyWord, MyDoc); //目地文件名
object NewFileName = jobDist[thisThread] + "\\" + thisTime + "\\" + jobListBox[thisThread].SelectedItem.ToString();
//将WordDoc文档对象的内容保存为DOC文档
MyDoc.SaveAs(ref NewFileName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
}
catch (Exception Err)
{
thisLog += jobListBox[thisThread].SelectedItem.ToString() + "\r\n" + Err.Message + "\r\n=====\r\n";
isError = true;
}
finally
{
//关闭WordDoc文档对象
MyDoc.Close(ref Nothing, ref Nothing, ref Nothing);
jobProc[thisThread].Text = Convert.ToString(100 * (i + 1) / jobListBox[thisThread].Items.Count) + "%";
} //终止操作
if (jobStop[thisThread].Checked)
{
i = jobListBox[thisThread].Items.Count;
}
}
}
catch (Exception Err)
{
MessageBox.Show(Err.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
//关闭模板文件
dotDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭WordApp组件对象
MyWord.Quit(ref Nothing, ref Nothing, ref Nothing);
} if (isError)
{
logFile(thisLog, thisThread, thisTime);
MessageBox.Show("处理完毕!\r\n\r\n有错误,请查看日志文件。\r\n于文件夹:" + thisTime, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
} //清理该线程,以便下次使用
jobProc[thisThread].Visible = false;
jobListBox[thisThread].Visible = false;
jobStop[thisThread].Visible = false;
}
解决方案 »
- 三层架构工程类中遇到的一个问题
- 请教ASP.net网站基本设计及架构思路
- 网上支付:www.authorize.net中网上支付不成功~急(在线等街)
- 我当初安装的时候的确是选择的混合验证空密码,为什么我一执行就告诉服务器没有设置成集成验证是怎么回事啊
- 请问如何在Asp.net中调用上一个页面
- 页面登录后的问题,如何用户点刷新不再出现重试对话框?
- asp.net如何连接Orcale数据库
- 在datagrid绑定CheckBox时,如果页刷新,我怎么让我刚才打钩的还在.
- 江雨大哥来救俺:怎么在客户端捕捉treeview结点的click事件呢?
- 小女生初学程序,请教一题~~!~(急..在线等)
- 高分求一份.NET项目需求或者意见,在线等
- frame的简单问题,请您帮忙!
我是说在安装OFFICE的过程中有一个高级选项,里面有个OFFICE共享功能组件和OFFICE工具组件。
有人跟我提过说是安装过程中的一个对》NET的支持组件,但我不知道是非曲直什么。测试了也没有结果。
所以请教看看如果的确是这个五环节的一个组件,那么可不可以在我的程序里对它进行控制安装呢?谢谢~
还有一个就是 FreeTextBox 也有word编辑功能。