C#操作Word辅助类(word2003)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
/******************************************************************** *******
* word辅助类
* 作者:chengfellow
* 日期:2008.8.18
* 注意事项:
* 1、开发环境居于office 2003;
* 2、需要添加Com引用:Microsoft Office 11.0 Object Library和
* Microsoft Word 11.0 Object Library。
*
******************************************************************** ********/
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
namespace WordAddinSample
{
public class WordHelp
{
private Microsoft.Office.Interop.Word.ApplicationClass oWordA pplic; // a reference to Word application
private Microsoft.Office.Interop.Word.Document oDoc;
// a reference to the document
object missing = System.Reflection.Missing.Value;
public Microsoft.Office.Interop.Word.ApplicationClass WordApp lication
{
get { return oWordApplic; }
}
public WordHelp()
{
// activate the interface with the COM object of Microsof t Word
oWordApplic = new Microsoft.Office.Interop.Word.Applicati onClass();
}
public WordHelp(Microsoft.Office.Interop.Word.ApplicationClas s wordapp)
{
oWordApplic = wordapp;
}
#region文件操作
// Open a file (the file must exists) and activate it
public void Open(string strFileName)
{
object fileName = strFileName;
object readOnly = false;
object isVisible = true;
oDoc = oWordApplic.Documents.Open(ref fileName, ref missi ng, ref readOnly,
ref missing, ref missing, ref missing, ref missing, r ef missing, ref missing,
ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
oDoc.Activate();
}
// Open a new document
public void Open()
{
oDoc = oWordApplic.Documents.Add(ref missing, ref missing , ref missing, ref missing);
oDoc.Activate();
}
public void Quit()
{
oWordApplic.Application.Quit(ref missing, ref missing, re f missing);
}
///<summary>
///附加dot模版文件
///</summary>
private void LoadDotFile(string strDotFile)
{
if (!string.IsNullOrEmpty(strDotFile))
{
Microsoft.Office.Interop.Word.Document wDot = null;
if (oWordApplic != null)
{
oDoc = oWordApplic.ActiveDocument;
oWordApplic.Selection.WholeStory();
//string strContent = oWordApplic.Selection.Text;
oWordApplic.Selection.Copy();
wDot = CreateWordDocument(strDotFile, true);
object bkmC = "Content";
if (oWordApplic.ActiveDocument.Bookmarks.Exists(" Content") == true)
{
oWordApplic.ActiveDocument.Bookmarks.get_Item (ref bkmC).Select();
}
//对标签"Content"进行填充
//直接写入内容不能识别表格什么的
//oWordApplic.Selection.TypeText(strContent);
oWordApplic.Selection.Paste();
oWordApplic.Selection.WholeStory();
oWordApplic.Selection.Copy();
wDot.Close(ref missing, ref missing, ref missing);
oDoc.Activate();
oWordApplic.Selection.Paste();
}
}
}
///
///打开Word文档,并且返回对象oDoc
///完整Word文件路径+名称
///返回的Word.Document oDoc对象
public Microsoft.Office.Interop.Word.Document CreateWordDocum ent(string FileName, bool HideWin)
{
if (FileName == "") return null;
oWordApplic.Visible = HideWin;
oWordApplic.Caption = "";
oWordApplic.Options.CheckSpellingAsYouType = false;
oWordApplic.Options.CheckGrammarAsYouType = false;
Object filename = FileName;
Object ConfirmConversions = false;
Object ReadOnly = true;
Object AddToRecentFiles = false;
Object PasswordDocument = System.Type.Missing;
Object PasswordTemplate = System.Type.Missing;
Object Revert = System.Type.Missing;
Object WritePasswordDocument = System.Type.Missing;
Object WritePasswordTemplate = System.Type.Missing;
Object Format = System.Type.Missing;
Object Encoding = System.Type.Missing;
Object Visible = System.Type.Missing;
Object OpenAndRepair = System.Type.Missing;
Object DocumentDirection = System.Type.Missing;
Object NoEncodingDialog = System.Type.Missing;
Object XMLTransform = System.Type.Missing;
try
{
Microsoft.Office.Interop.Word.Document wordDoc = oWor dApplic.Documents.Open(ref filename, ref ConfirmConversions,
ref ReadOnly, ref AddToRecentFiles, ref PasswordDocum ent, ref PasswordTemplate,
ref Revert, ref WritePasswordDocument, ref WritePassw ordTemplate, ref Format,
ref Encoding, ref Visible, ref OpenAndRepair, ref Doc umentDirection,
ref NoEncodingDialog, ref XMLTransform);
return wordDoc;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}
public void SaveAs(Microsoft.Office.Interop.Word.Document oDo c, string strFileName)
{
object fileName = strFileName;
if (File.Exists(strFileName))
{
if (MessageBox.Show("文件'" + strFileName + "'已经存在,选确定覆盖原文件,选取消退出操作!", "警告", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
oDoc.SaveAs(ref fileName, ref missing, ref missin g, 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);
}
else
{
Clipboard.Clear();
}
}
else
{
oDoc.SaveAs(ref fileName, ref missing, ref missing, r ef missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref mi ssing, ref missing, ref missing, ref missing, ref missing, ref missin g);
}
}
public void SaveAsHtml(Microsoft.Office.Interop.Word.Document oDoc, string strFileName)
{
object fileName = strFileName;
//wdFormatWebArchive保存为单个网页文件
//wdFormatFilteredHTML保存为过滤掉word标签的htm文件,缺点是有图片的话会产生网页文件夹
if (File.Exists(strFileName))
{
if (MessageBox.Show("文件'" + strFileName + "'已经存在,选确定覆盖原文件,选取消退出操作!", "警告", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
object Format = (int)Microsoft.Office.Interop.Wor d.WdSaveFormat.wdFormatWebArchive;
oDoc.SaveAs(ref fileName, ref Format, ref missing , ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref mi ssing, ref missing, ref missing, ref missing, ref missing, ref missin g);
}
else
{
Clipboard.Clear();
}
}
else
{
object Format = (int)Microsoft.Office.Interop.Word.Wd SaveFormat.wdFormatWebArchive;
oDoc.SaveAs(ref fileName, ref Format, ref missing, re f missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missin g, ref missing, ref missing, ref missing, ref missing, ref missing); }
}
public void Save()
{
oDoc.Save();
}
public void SaveAs(string strFileName)
{
object fileName = strFileName;
oDoc.SaveAs(ref fileName, ref missing, ref missing, ref m issing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, r ef missing, ref missing, ref missing, ref missing, ref missing);
}
// Save the document in HTML format
public void SaveAsHtml(string strFileName)
{
object fileName = strFileName;
object Format = (int)Microsoft.Office.Interop.Word.WdSave Format.wdFormatHTML;
oDoc.SaveAs(ref fileName, ref Format, ref missing, ref mi ssing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, r ef missing, ref missing, ref missing, ref missing, ref missing);
}
#endregion
#region添加菜单(工具栏)项
//添加单独的菜单项
public void AddMenu(mandBarPopup pop uBar)
{
mandBar menuBar = null;
menuBar = mandBars["Menu Bar"];
popuBar = (mandBarPopup)this.oWo mandBars.FindControl(Microsoft.Office.Core.MsoControlType .msoControlPopup, missing, popuBar.Tag, true);
if (popuBar == null)
{
popuBar = (mandBarPopup)menu Bar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlPopup , missing, missing, missing, missing);
}
}
//添加单独工具栏
public void AddToolItem(string strBarName,string strBtnName) {
mandBar toolBar = null;
toolBar = (mandBar)this.oWordApp mandBars.FindControl(Microsoft.Office.Core.MsoControlType.msoC ontrolButton, missing, strBarName, true);
if (toolBar == null)
{
toolBar = (mandBar)this.oWor mandBars.Add(
Microsoft.Office.Core.MsoControlType.msoControlB utton,
missing, missing, missing);
= strBtnName;
toolBar.Visible = true;
}
}
#endregion
#region移动光标位置
// Go to a predefined bookmark, if the bookmark doesn't exist s the application will raise an error
public void GotoBookMark(string strBookMarkName)
{
// VB : Selection.GoTo What:=wdGoToBookmark, Name:="nome "
object Bookmark = (int)Microsoft.Office.Interop.Word.WdGo ToItem.wdGoToBookmark;
object NameBookMark = strBookMarkName;
oWordApplic.Selection.GoTo(ref Bookmark, ref missing, ref missing, ref NameBookMark);
}
public void GoToTheEnd()
{
// VB : Selection.EndKey Unit:=wdStory
object unit;
unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
oWordApplic.Selection.EndKey(ref unit, ref missing);
}
public void GoToLineEnd()
{
object unit = Microsoft.Office.Interop.Word.WdUnits.wdLin e;
object ext = Microsoft.Office.Interop.Word.WdMovementType .wdExtend;
oWordApplic.Selection.EndKey(ref unit, ref ext);
}
public void GoToTheBeginning()
{
// VB : Selection.HomeKey Unit:=wdStory
object unit;
unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
oWordApplic.Selection.HomeKey(ref unit, ref missing);
}
public void GoToTheTable(int ntable)
{
// Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirs t, Count:=1, Name:=""
// Selection.Find.ClearFormatting
// With Selection.Find
// .Text = ""
// .Replacement.Text = ""
// .Forward = True
// .Wrap = wdFindContinue
// .Format = False
// .MatchCase = False
// .MatchWholeWord = False
// .MatchWildcards = False
// .MatchSoundsLike = False
// .MatchAllWordForms = False
// End With
object what;
what = Microsoft.Office.Interop.Word.WdUnits.wdTable;
object which;
which = Microsoft.Office.Interop.Word.WdGoToDirection.wdG oToFirst;
object count;
count = 1;
oWordApplic.Selection.GoTo(ref what, ref which, ref count , ref missing);
oWordApplic.Selection.Find.ClearFormatting();
oWordApplic.Selection.Text = "";
}
public void GoToRightCell()
{
// Selection.MoveRight Unit:=wdCell
object direction;
direction = Microsoft.Office.Interop.Word.WdUnits.wdCell; oWordApplic.Selection.MoveRight(ref direction, ref missin
g, ref missing);
}
public void GoToLeftCell()
{
// Selection.MoveRight Unit:=wdCell
object direction;
direction = Microsoft.Office.Interop.Word.WdUnits.wdCell; oWordApplic.Selection.MoveLeft(ref direction, ref missing , ref missing);
}
public void GoToDownCell()
{
// Selection.MoveRight Unit:=wdCell
object direction;
direction = Microsoft.Office.Interop.Word.WdUnits.wdLine; oWordApplic.Selection.MoveDown(ref direction, ref missing , ref missing);
}
public void GoToUpCell()
{
// Selection.MoveRight Unit:=wdCell
object direction;
direction = Microsoft.Office.Interop.Word.WdUnits.wdLine; oWordApplic.Selection.MoveUp(ref direction, ref missing, ref missing);
}
#endregion
#region插入操作
public void InsertText(string strText)
{
oWordApplic.Selection.TypeText(strText);
}
public void InsertLineBreak()
{
oWordApplic.Selection.TypeParagraph();
}
///<summary>
///插入多个空行
///</summary>
///<param name="nline"></param>
public void InsertLineBreak(int nline)
{
for (int i = 0; i < nline; i++)
oWordApplic.Selection.TypeParagraph();
}
public void InsertPagebreak()
{
// VB : Selection.InsertBreak Type:=wdPageBreak
object pBreak = (int)Microsoft.Office.Interop.Word.WdBrea kType.wdPageBreak;
oWordApplic.Selection.InsertBreak(ref pBreak);
}
// 插入页码
public void InsertPageNumber()
{
object wdFieldPage = Microsoft.Office.Interop.Word.WdFiel dType.wdFieldPage;
object preserveFormatting = true;
oWordApplic.Selection.Fields.Add(oWordApplic.Selection.Ra nge, ref wdFieldPage, ref missing, ref preserveFormatting);
}
// 插入页码
public void InsertPageNumber(string strAlign)
{
object wdFieldPage = Microsoft.Office.Interop.Word.WdFiel dType.wdFieldPage;
object preserveFormatting = true;
oWordApplic.Selection.Fields.Add(oWordApplic.Selection.Ra nge, ref wdFieldPage, ref missing, ref preserveFormatting);
SetAlignment(strAlign);
}
public void InsertImage(string strPicPath, float picWidth, fl oat picHeight)
{
string FileName = strPicPath;
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = oWordApplic.Selection.Range;
oWordApplic.ActiveDocument.InlineShapes.AddPicture(FileNa me, ref LinkToFile, ref SaveWithDocument, ref Anchor).Select();
oWordApplic.Selection.InlineShapes[1].Width = picWidth; /
/ 图片宽度
oWordApplic.Selection.InlineShapes[1].Height = picHeight;
// 图片高度
// 将图片设置为四面环绕型
Microsoft.Office.Interop.Word.Shape s = oWordApplic.Selec tion.InlineShapes[1].ConvertToShape();
s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapT ype.wdWrapSquare;
}
public void InsertLine(float left, float top, float width, fl oat weight, int r, int g, int b)
{
//SetFontColor("red");
//SetAlignment("Center");
object Anchor = oWordApplic.Selection.Range;
//int pLeft = 0, pTop = 0, pWidth = 0, pHeight = 0;
//oWordApplic.ActiveWindow.GetPoint(out pLeft, out pTop, out pWidth, out pHeight,missing);
//MessageBox.Show(pLeft + "," + pTop + "," + pWidth + ","
+ pHeight);
object rep = false;
//left += oWordApplic.ActiveDocument.PageSetup.LeftMargin; left = oWordApplic.CentimetersToPoints(left);
top = oWordApplic.CentimetersToPoints(top);
width = oWordApplic.CentimetersToPoints(width);
Microsoft.Office.Interop.Word.Shape s = oWordApplic.Activ eDocument.Shapes.AddLine(0, top, width, top, ref Anchor);
s.Line.ForeColor.RGB = RGB(r, g, b);
s.Line.Visible = Microsoft.Office.Core.MsoTriState.msoTru e;
s.Line.Style = Microsoft.Office.Core.MsoLineStyle.msoLine Single;
s.Line.Weight = weight;
}
#endregion
#region设置样式
///<summary>
/// Change the paragraph alignement
///</summary>
///<param name="strType"></param>
public void SetAlignment(string strType)
{
switch (strType.ToLower())
{
case"center":
oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphC enter;
break;
case"left":
oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphL eft;
break;
case"right":
oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphR ight;
break;
case"justify":
oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJ ustify;
break;
}
}
// if you use thif function to change the font you should cal l it again with
// no parameter in order to set the font without a particular format
public void SetFont(string strType)
{
switch (strType)
{
case"Bold":
oWordApplic.Selection.Font.Bold = 1;
break;
case"Italic":
oWordApplic.Selection.Font.Italic = 1;
break;
case"Underlined":
oWordApplic.Selection.Font.Subscript = 0;
break;
}
}
// disable all the style
public void SetFont()
{
oWordApplic.Selection.Font.Bold = 0;
oWordApplic.Selection.Font.Italic = 0;
oWordApplic.Selection.Font.Subscript = 0;
}
public void SetFontName(string strType)
{
= strType;
}
public void SetFontSize(float nSize)
{
SetFontSize(nSize, 100);
}
public void SetFontSize(float nSize, int scaling)
{
if (nSize > 0f)
oWordApplic.Selection.Font.Size = nSize;
if (scaling > 0)
oWordApplic.Selection.Font.Scaling = scaling;
}
public void SetFontColor(string strFontColor)
{
switch (strFontColor.ToLower())
{
case"blue":
oWordApplic.Selection.Font.Color = Microsoft.Offi
ce.Interop.Word.WdColor.wdColorBlue;
break;
case"gold":
oWordApplic.Selection.Font.Color = Microsoft.Offi ce.Interop.Word.WdColor.wdColorGold;
break;
case"gray":
oWordApplic.Selection.Font.Color = Microsoft.Offi ce.Interop.Word.WdColor.wdColorGray875;
break;
case"green":
oWordApplic.Selection.Font.Color = Microsoft.Offi ce.Interop.Word.WdColor.wdColorGreen;
break;
case"lightblue":
oWordApplic.Selection.Font.Color = Microsoft.Offi ce.Interop.Word.WdColor.wdColorLightBlue;
break;
case"orange":
oWordApplic.Selection.Font.Color = Microsoft.Offi ce.Interop.Word.WdColor.wdColorOrange;
break;
case"pink":
oWordApplic.Selection.Font.Color = Microsoft.Offi ce.Interop.Word.WdColor.wdColorPink;
break;
case"red":
oWordApplic.Selection.Font.Color = Microsoft.Offi ce.Interop.Word.WdColor.wdColorRed;
break;
case"yellow":
oWordApplic.Selection.Font.Color = Microsoft.Offi ce.Interop.Word.WdColor.wdColorYellow;
break;
}
}
public void SetPageNumberAlign(string strType, bool bHeader) {
object alignment;
object bFirstPage = false;
object bF = true;
//if (bHeader == true)
//WordApplic.Selection.HeaderFooter.PageNumbers.ShowFirst
PageNumber = bF;
switch (strType)
{
case"Center":
alignment = Microsoft.Office.Interop.Word.WdPageN umberAlignment.wdAlignPageNumberCenter;
//WordApplic.Selection.HeaderFooter.PageNumbers.A dd(ref alignment,ref bFirstPage);
//Microsoft.Office.Interop.Word.Selection objSele ction = WordApplic.pSelection;
oWordApplic.Selection.HeaderFooter.PageNumbers[1] .Alignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAl ignPageNumberCenter;
break;
case"Right":
alignment = Microsoft.Office.Interop.Word.WdPageN umberAlignment.wdAlignPageNumberRight;
oWordApplic.Selection.HeaderFooter.PageNumbers[1] .Alignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAl ignPageNumberRight;
break;
case"Left":
alignment = Microsoft.Office.Interop.Word.WdPageN umberAlignment.wdAlignPageNumberLeft;
oWordApplic.Selection.HeaderFooter.PageNumbers.Ad d(ref alignment, ref bFirstPage);
break;
}
}
///<summary>
///设置页面为标准A4公文样式
///</summary>
private void SetA4PageSetup()
{
oWordApplic.ActiveDocument.PageSetup.TopMargin = oWordApp lic.CentimetersToPoints(3.7f);
//oWordApplic.ActiveDocument.PageSetup.BottomMargin = oWo rdApplic.CentimetersToPoints(1f);
oWordApplic.ActiveDocument.PageSetup.LeftMargin = oWordAp plic.CentimetersToPoints(2.8f);
oWordApplic.ActiveDocument.PageSetup.RightMargin = oWordA pplic.CentimetersToPoints(2.6f);
//oWordApplic.ActiveDocument.PageSetup.HeaderDistance = o
WordApplic.CentimetersToPoints(2.5f);
//oWordApplic.ActiveDocument.PageSetup.FooterDistance = o WordApplic.CentimetersToPoints(1f);
oWordApplic.ActiveDocument.PageSetup.PageWidth = oWordApp lic.CentimetersToPoints(21f);
oWordApplic.ActiveDocument.PageSetup.PageHeight = oWordAp plic.CentimetersToPoints(29.7f);
}
#endregion
#region替换
///<summary>
///在word 中查找一个字符串直接替换所需要的文本
///</summary>
///<param name="strOldText">原文本</param>
///<param name="strNewText">新文本</param>
///<returns></returns>
public bool Replace(string strOldText, string strNewText)
{
if (oDoc == null)
oDoc = oWordApplic.ActiveDocument;
this.oDoc.Content.Find.Text = strOldText;
object FindText, ReplaceWith, Replace;//
FindText = strOldText;//要查找的文本
ReplaceWith = strNewText;//替换文本
Replace = Microsoft.Office.Interop.Word.WdReplace.wdRepla ceAll;/**//*wdReplaceAll - 替换找到的所有项。
* wdReplaceNone - 不替换找到的任何项。
* wdReplaceOne - 替换找到的第一项。
* */
oDoc.Content.Find.ClearFormatting();//移除Find的搜索文本和段落格式设置
if (oDoc.Content.Find.Execute(
ref FindText, ref missing,
ref missing, ref missing,
ref missing, ref missing,
ref missing, ref missing, ref missing,
ref ReplaceWith, ref Replace,
ref missing, ref missing,
ref missing, ref missing))
{
return true;
}
return false;
}
public bool SearchReplace(string strOldText, string strNewTex t)
{
object replaceAll = Microsoft.Office.Interop.Word.WdRepla ce.wdReplaceAll;
//首先清除任何现有的格式设置选项,然后设置搜索字符串 strOldText。
oWordApplic.Selection.Find.ClearFormatting();
oWordApplic.Selection.Find.Text = strOldText;
oWordApplic.Selection.Find.Replacement.ClearFormatting(); oWordApplic.Selection.Find.Replacement.Text = strNewText;
if (oWordApplic.Selection.Find.Execute(
ref missing, ref missing, ref missing, ref missing, r ef missing,
ref missing, ref missing, ref missing, ref missing, r ef missing,
ref replaceAll, ref missing, ref missing, ref missing , ref missing))
{
return true;
}
return false;
}
#endregion
///<summary>
/// rgb转换函数
///</summary>
///<param name="r"></param>
///<param name="g"></param>
///<param name="b"></param>
///<returns></returns>
int RGB(int r, int g, int b)
{
return ((b << 16) | (ushort)(((ushort)g << 8) | r));
}
Color RGBToColor(int color)
{
int r = 0xFF & color;
int g = 0xFF00 & color;
g >>= 8;
int b = 0xFF0000 & color;
b >>= 16;
return Color.FromArgb(r, g, b); }
}
}。