我試圖使用此代碼將doc文件轉換爲pdf。我得到這個錯誤「不包含適用於入口點的靜態'主'方法」 我不確定這段代碼有什麼問題。感謝您的任何幫助。將DOC轉換爲PDF,錯誤:「不包含適用於入口點的靜態'Main'方法」
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text;
using Microsoft.Office.Interop.Word;
namespace WindowsFormsApplication4
{
public class translate
{
private static void ConvertWordFileToPdf(string WordFilesLocation, string PdfFilesLocation)
{
Document doc = null;
// C# doesn't have optional arguments so we'll need a dummy value
object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application word = null;
try
{
// Create a new Microsoft Word application object
word = new Microsoft.Office.Interop.Word.Application();
// Get list of Word files in specified directory
DirectoryInfo dirInfo = new DirectoryInfo(WordFilesLocation);
FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");
if (wordFiles.Length > 0)
{
word.Visible = false;
word.ScreenUpdating = false;
string sourceFile = "";
string destinationFile = "";
try
{
foreach (FileInfo wordFile in wordFiles)
{
// Cast as Object for word Open method
Object filename = (Object)wordFile.FullName;
sourceFile = wordFile.Name;
destinationFile = "";
// Use the dummy value as a placeholder for optional arguments
doc = word.Documents.Open(ref filename, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
doc.Activate();
object outputFileName = null;
if (wordFile.FullName.ToUpper().Contains(".DOCX"))
{
outputFileName = wordFile.FullName.Replace(".docx", ".pdf");
destinationFile = sourceFile.Replace(".docx", ".pdf");
}
else
{
outputFileName = wordFile.FullName.Replace(".doc", ".pdf");
destinationFile = sourceFile.Replace(".doc", ".pdf");
}
sourceFile = WordFilesLocation + "\\" + destinationFile;
destinationFile = PdfFilesLocation + "\\" + destinationFile;
object fileFormat = WdSaveFormat.wdFormatPDF;
// Save document into PDF Format
doc.SaveAs(ref outputFileName,
ref fileFormat, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
// Close the Word document, but leave the Word application open.
// doc has to be cast to type _Document so that it will find the
// correct Close method.
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
doc = null;
//there is options to save file in particular location, default is the current folder.
// So move or replace a file to a new location explicitly
if (System.IO.File.Exists(destinationFile))
{
System.IO.File.Replace(sourceFile, destinationFile, null);
}
else
{
System.IO.File.Move(sourceFile, destinationFile);
}
Console.WriteLine("Success:" + "SourceFile-" + outputFileName.ToString() + " DestinationFile-" + destinationFile);
}
// word has to be cast to type _Application so that it will find
// the correct Quit method.
((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
word = null;
}
catch (Exception ex)
{
//individual file exception, do not stop but display the error
//Log this if needed
Console.WriteLine("Fail:" + "SourceFile-" + sourceFile + " DestinationFile-" + destinationFile + "#Error-" + ex.Message);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error occured while processing");
Console.WriteLine(ex.Message);
}
finally
{
if (doc != null)
{
((_Document)doc).Close(ref oMissing, ref oMissing, ref oMissing);
doc = null;
}
if (word != null)
{
((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
word = null;
}
}
}
}
}
你的*應用程序中只有*這個代碼嗎?從命名空間聽起來像一個WinForm項目。您應該在您的應用程序中使用此代碼的表單。確保您的項目啓動對象設置爲使用該表單。右鍵單擊項目並轉到屬性,在構建下您將找到啓動項目。 .Net中的每個可執行文件都需要一個Main方法,如果是WinForm,它將位於program.cs文件中,而您的Main將調用Form類。 – Habib 2014-10-07 15:21:20