Document? document = null; Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'type')
As the message says, type Document can't be nullable/null.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
how to run a .net command programme on ubuntu 20.04
the programme is using to convert word to pdf
i tried follow:
dependence COM: Interop.Microsoft.Office.Interop.Word
using Microsoft.Office.Interop.Word;
using System;
namespace Word2Pdf
{
internal class Program
{
static void Main(string[] args)
{
for (int i = 0; i < args.Length; i++)
{
Console.WriteLine(i + " : " + args[i]);
}
if (args.Length != 2)
{
Console.WriteLine("参数错误!,请输入\"exe xxx.docx xxx.pdf\"");
return;
}
Console.WriteLine("转换开始");
string sourcePath = args[0];
string targetPath = args[1];
WordToPDFWithOffice(sourcePath, targetPath);
Console.WriteLine("转换结束");
}
/// <summary>
/// 将word转成PDF office
/// </summary>
/// <param name="sourcePath"></param>
/// <param name="targetPath"></param>
/// <returns></returns>
public static bool WordToPDFWithOffice(string sourcePath, string targetPath)
{
bool result = false;
Application application = new();
Document? document = null;
try
{
application.Visible = false;
document = application.Documents.Open(sourcePath);
/*
参数参考 https://docs.microsoft.com/zh-cn/office/vba/api/visio.document.exportasfixedformat
*/
document.ExportAsFixedFormat(targetPath, WdExportFormat.wdExportFormatPDF, false);
result = true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
result = false;
}
finally
{
document?.Close();
}
return result;
}
}
}
api install aspnetcore-runtime-6.0 (done)
dotnet Word2Pdf.dll xxx.docx xxx.pdf
but got error:
Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'type')
at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
at System.Activator.CreateInstance(Type type)
at Word2Pdf.Program.WordToPDFWithOffice(String sourcePath, String targetPath) in D:\sharpProjects\Word2Pdf\Word2Pdf\Program.cs:line 40
at Word2Pdf.Program.Main(String[] args) in D:\sharpProjects\Word2Pdf\Word2Pdf\Program.cs:line 23
Aborted
Document? document = null; Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'type')
As the message says, type Document can't be nullable/null.