标题:C#操作office时报异常。代码如下,恳请大神指教
取消只看楼主
米粒大小3
Rank: 1
等 级:新手上路
帖 子:40
专家分:7
注 册:2015-10-25
结帖率:66.67%
 问题点数:0 回复次数:0 
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();
        }
    }
}
搜索更多相关主题的帖子: office 
2016-09-23 11:44



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-469093-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016618 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved