【问题标题】:Getting the validation error while running the below script, can anyone help me out to overcome this运行以下脚本时出现验证错误,谁能帮我解决这个问题
【发布时间】: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#


【解决方案1】:

将您的命名空间(第 8 行)更改为别的东西.如果您还想与外部Word 命名空间或类型通信,拥有自己的命名空间Word 不是一个好主意。称它为WordHelperMyProgramBill;或者什么都没有(只是完全删除命名空间行)。只是不是Word

如果需要,可以解决这些问题,但在这里真的不值得。

【讨论】:

  • 我刚刚删除了整个命名空间,但仍然出现同样的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 2020-10-05
  • 1970-01-01
相关资源
最近更新 更多