C#操作office时报异常。代码如下,恳请大神指教
报异常,说什么未处理comexception,数值超范围!!!恳请大神指教!!!!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MSWord = Microsoft.Office.Interop.Word;
using
using System.Reflection;
namespace Addgeshihuawenben
{
class Program
{
static void Main(string[] args)
{
object path;
string strContent;
MSWord.Application wordApp;
MSWord.Document wordDoc;
path = @"C:\Users\Administrator\Desktop\MyWord2.doc";
wordApp = new MSWord.ApplicationClass();
if (File.Exists((string)path))
{
File.Delete((string)path);
}
Object Nothing = Missing.Value;
wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
strContent = "普通文本普通文本普通文本普通文本普通文本\n";
wordDoc.Paragraphs.Last.Range.Text = strContent;
strContent = "黑体文本黑体文本黑体文本黑体文本黑体文本\n";
wordDoc.Paragraphs.Last.Range.Font.Name = "黑体";
wordDoc.Paragraphs.Last.Range.Text = strContent;
strContent = "加粗文本加粗文本加粗文本加粗文本加粗文本\n";
wordDoc.Paragraphs.Last.Range.Font.Bold = 1;
wordDoc.Paragraphs.Last.Range.Text = strContent;
wordDoc.Paragraphs.Last.Range.Font.Size = 15;
wordDoc.Paragraphs.Last.Range.Text = strContent;
strContent = "斜体文本斜体文本斜体文本斜体文本斜体文本\n";
wordDoc.Paragraphs.Last.Range.Font.Italic = 1;
strContent = "蓝色文本蓝色文本蓝色文本蓝色文本蓝色文本\n";
wordDoc.Paragraphs.Last.Range.Font.Color = MSWord.WdColor.wdColorBlue;
wordDoc.Paragraphs.Last.Range.Text = strContent;
strContent = "下划线文本下划线文本下划线文本下划线文本下划线文本\n";
wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineThick;
wordDoc.Paragraphs.Last.Range.Text = strContent;
strContent = "红色下划线文本红色下划线文本红色下划线文本红色下划线文本红色下划线文本\n";
wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineThick;
wordDoc.Paragraphs.Last.Range.Text = strContent;
object format = MSWord.WdSaveFormat.wdFormatDocumentDefault;
wordDoc.SaveAs(ref path, ref format, 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);
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
Console.WriteLine(path + "创建完毕!");
Console.ReadKey();
}
}
}