안녕하세요 테스트 프로그램으로 오피스 문서들을 pdf로 변환하는 코드를 테스트 하고 있는데 알 수 없는 오류가 출력 됩니다. 이 부분에 대해서 어떻게 처리해야 하는지 문의 드립니다. (vs2012, 4.8.1 입니다.)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
namespace TestProject
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// .doc 파일 경로
string docFilePath = @"d:\input.doc";
// PDF로 저장할 경로 및 파일명
string pdfFilePath = @"d:\output.pdf";
ConvertDocToPdf(docFilePath, pdfFilePath);
MessageBox.Show("변환 완료");
}
private void ConvertDocToPdf(string docFilePath, string pdfFilePath)
{
try
{
// Microsoft Word Application 인스턴스 생성
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
// .doc 파일 열기
//Document doc = wordApp.Documents.Open(docFilePath);
Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(docFilePath);
// PDF로 저장
doc.SaveAs2(pdfFilePath, WdSaveFormat.wdFormatPDF);
// .doc 파일 및 Word 인스턴스 닫기
doc.Close();
wordApp.Quit();
// 사용이 끝난 Word 인스턴스 정리
System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
}
catch (Exception ex)
{
MessageBox.Show("오류 발생: " + ex.Message);
}
}
}
}