program.cs
来自「[Visual C# 2005程序设计基础教程] 全部的源码!非常经典」· CS 代码 · 共 39 行
CS
39 行
using System;
using System.Collections.Generic;
using System.Text;
using word = Microsoft.Office.Interop.Word;
namespace VSTOWord
{
class Program
{
static void Main(string[] args)
{
//新建文档
object oMissing = System.Reflection.Missing.Value;
//实例化一个word应用程序Application对象oWord
Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = true;
//定义一个文档对象oDoc
Microsoft.Office.Interop.Word.Document oDoc;
//使用word应用程序对象oWord对象Documents集合的Add方法实例化一个文档
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
//向文档中添加文本
object start = 0;
object end = 0;
word.Range rng = oDoc.Range(ref start, ref end);
rng.Text = "Hello Word";
//保存文档
object fileName = @"C:\\NewDocument.doc";
oWord.ActiveDocument.SaveAs(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);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?