📄 program.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
namespace VSTOWord2
{
class Program
{
static void Main(string[] args)
{
object oMissing = System.Reflection.Missing.Value;
Word.Application oWrod = new Microsoft.Office.Interop.Word.Application();
object filename = @"C:\\sample.doc";
Word.Document oDoc = oWrod.Documents.Open(ref filename, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
oWrod.Visible = true;
//选择目标文档的"第三段"作为目标范围
Word.Range rng = oDoc.Paragraphs[3].Range;
//设置(更改)目标范围的格式
rng.Font.Size = 20; //设置大小
rng.Font.Name = "黑体"; //设置字体
//对齐方式
rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
rng.Select();
MessageBox.Show("格式已经更改!");
// 撤销更改
object numTimes3 = 3;
oDoc.Undo(ref numTimes3);
rng.Select();
MessageBox.Show("撤销格式更改");
//重新设定目标范围
rng.SetRange(10, 30);
//更新目标范围内的数据
rng.Text = "Hello word!";
//设置新的格式
rng.Bold = 2;
rng.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;
rng.Select();
MessageBox.Show("撤销格式更改");
// 执行撤销操作
object numTimes1 = 3;
oDoc.Undo(ref numTimes1);
rng.Select();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -