2013-08-05 2 views
1

Wordform을 Winforms 앱에서 프로그래밍 방식으로 PDF로 변환하는 테스트를하고 있습니다. 내 컴퓨터에서 잘 작동하는 다음 코드를 작성했습니다. 앱은 2 개의 버튼과 텍스트 상자가있는 양식으로 구성되어 있습니다. 첫 번째 단추를 클릭하면 사용자가 폴더로 이동할 수있는 대화 상자가 열립니다. 폴더 주소가 텍스트 상자에 저장됩니다. 두 번째 단추는 지정된 폴더에서 각 Word 문서를 가져와 각각에 대해 동일한 이름의 PDF를 만듭니다.내 개발 PC가 아닌 다른 컴퓨터에서 Word to PDF 변환이 실패합니다.

namespace PDFTesting 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void btnFind_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       FolderBrowserDialog folder = new FolderBrowserDialog(); 
       DialogResult result = folder.ShowDialog(); 
       if (result == DialogResult.OK) 
       { 
        string[] files = Directory.GetFiles(folder.SelectedPath); 
        txtLocation.Text = folder.SelectedPath.ToString(); 
       } 
       else 
       { 
        txtLocation.Text = null; 
       } 
      } 
      catch (Exception eX) 
      { 
       throw new Exception("cDocument: Error atempting to GetPath()" + Environment.NewLine + eX.Message); 
      } 
     } 

     private void btnConvert_Click(object sender, EventArgs e) 
     { 
      // Create a new Microsoft Word application object 
      Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); 

      // C# doesn't have optional arguments so we'll need a dummy value 
      object oMissing = System.Reflection.Missing.Value; 

      // Get list of Word files in specified directory 
      string docPath = txtLocation.Text; 
      DirectoryInfo dirInfo = new DirectoryInfo(docPath); 
      FileInfo[] wordFiles = dirInfo.GetFiles("*.doc"); 

      word.Visible = false; 
      word.ScreenUpdating = false; 

      foreach (FileInfo wordFile in wordFiles) 
      { 
       // Cast as Object for word Open method 
       Object filename = (Object)wordFile.FullName; 

       // Use the dummy value as a placeholder for optional arguments 
       Document 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 = wordFile.FullName.Replace(".docx", ".pdf"); 
       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; 
      } 

      // 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; 
     } 
    } 
} 

이 내 개발 PC에 구성,하지만 난 솔루션을 구축하고 다른 컴퓨터에 배포 할 때 (윈도우 7, 64 비트의 동일한 버전을 실행) 및 Word가 설치되어, 나는 다음과 같은 오류가 정확히 작동 -

Error

내 미니 PDF의 앱이 다른 컴퓨터에서 작동하지 않는 이유

사람이 어떤 아이디어가 있습니까?

+2

Word의 COM 참조에 뭔가 빠져있는 것처럼 보입니다. 두 컴퓨터에 동일한 버전의 Word가 설치되어 있습니까? 두 컴퓨터에서 같은 경로에 있습니까? – danielunderwood

+0

내 개발 PC에는 Office 2010 정식 버전이 있지만 테스트 컴퓨터에는 Word와 Excel 만 포함 된 (시작) 버전이 있지만 모두 2010 버전입니다. – PJW

+1

흠. 나는 스타터 에디션이 애드 인을 지원하지 않는다는 것을 알고 있지만 COM 상호 작용을 지원하지 않는다는 것을 명시 적으로 밝히지는 않았다. 다른 버전의 Office가있는 다른 컴퓨터에서 실행하거나 필요할 경우 전체 버전의 평가판을 다운로드 할 수 있습니다. 설치 위치의 차이 일 수도 있지만 그 점은 의심 스럽습니다. – danielunderwood

답변

1

을 나는 오피스 365 무료 30 일 평가판을 설치하고 그것으로 Word 문서를 만들었습니다. 그런 다음 폴더에 응용 프로그램이 있음을 지적하고 성공적으로 문서를 PDF로 변환했습니다. 다른 버전의 Word를 사용해 보도록 해주신 덕분에 danielu에게 감사드립니다.

다시 보니 이제 Office의 초보자 용 버전은 .docx 확장명으로 저장되는 Word 버전이 아니라 .doc 확장명으로 문서를 저장합니다. 따라서 문제는 다음 코드 줄이 확장을 올바르게 대체하지 못했다는 것입니다. -

object outputFileName = wordFile.FullName.Replace(".docx", ".pdf"); 

모두 괜찮습니다.

관련 문제