【发布时间】:2023-02-05 17:03:21
【问题描述】:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Word;
namespace Word
{
class Program
{
static void Main(string[] args)
{
Word.Application wordApp = new Word.Application();
Word.Document wordDoc = wordApp.Documents.Open(@"C:\document.docx");
foreach (Word.Paragraph paragraph in wordDoc.Paragraphs)
{
// Get the style name of the paragraph
string styleName = paragraph.get_Style().NameLocal;
// Add the XML tag based on the style name
switch (styleName)
{
case "Heading 1":
paragraph.Range.Text = "<h1>" + paragraph.Range.Text + "</hs1>";
break;
case "Heading 2":
paragraph.Range.Text = "<h2>" + paragraph.Range.Text + "</h2>";
break;
case "Heading 3":
paragraph.Range.Text = "<h3>" + paragraph.Range.Text + "</h3>";
break;
default:
paragraph.Range.Text = "<p>" + paragraph.Range.Text + "</p>";
break;
}
}
wordDoc.Save();
wordApp.Quit();
}
}
}
1>C:\Users\Pavithra\source\repos\WordProcessing\WordProcessing\Program.cs(14,13,14,17): error CS0246: 找不到类型或命名空间名称“Word”(你是否缺少一个使用指令或程序集引用?) 1>C:\Users\Pavithra\source\repos\WordProcessing\WordProcessing\Program.cs(14,44,14,48): error CS0246: 找不到类型或命名空间名称“Word”(你是否缺少一个使用指令或程序集引用?) 1>C:\Users\Pavithra\source\repos\WordProcessing\WordProcessing\Program.cs(15,13,15,17): error CS0246: 找不到类型或命名空间名称“Word”(你是否缺少一个使用指令或程序集引用?) 1>C:\Users\Pavithra\source\repos\WordProcessing\WordProcessing\Program.cs(16,22,16,26): error CS0246: 找不到类型或命名空间名称“Word”(你是否缺少一个使用指令或程序集引用?)
任何人都可以帮助我克服这个错误,因为它对于 C# 来说是非常新的。
我包含了 Microsoft.Office.Interop.Word 和 Microsoft.Office.Core 但仍然显示验证错误
【问题讨论】:
-
你的
using指令可能是using Word = Microsoft.Office.Interop.Word;(注意“Word =”部分),见documentation
标签: c#