office文件转pdf出现的奇怪异常,忘大侠帮帮我

g394594141 2009-02-07 05:24:03
各位大侠,你们好,我目前用微软的offce2007的组件转换office文件到pdf,其中出现下面的异常,忘高手帮帮偶.谢谢!
以下是转换代码:
word:
Microsoft.Office.Interop.Word._Application oWord;
Microsoft.Office.Interop.Word._Document oDoc;
oWord = new Microsoft.Office.Interop.Word.Application();
//oWord.Visible = true;
oDoc = oWord.Documents.Open(ref tempFileName,
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);
try
{
savePath = driverPath + "\\" + mediaName + "\\" + pathName + "\\";
if (!Directory.Exists(savePath.ToString()))
{
Directory.CreateDirectory(savePath.ToString());
}
savePath = savePath + strFileName + "." + astdt;
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
oDoc.SaveAs(ref savePath, ref format,
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);
oDoc.Close(ref flg, ref oMissing, ref oMissing);
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
}
excel:
book.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF,
savePath, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
(限于长度,只贴出出错的那一句,前面代码的跟转换word的类似。)
powerpoint:
ppApp.Presentations[ppApp.Presentations.Count].SaveAs(savePath.ToString(), Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF, Microsoft.Office.Core.MsoTriState.msoTrue);
(限于长度,只贴出出错的那一句,前面代码的跟转换word的类似。)

以下是异常信息:
转换Word:
System.Runtime.InteropServices.COMException (0x800A1066): Command failed。
转换Excel:
它先弹出一对话框,对话框内容是“Document not saved. The document may be open, or an error may have been encountered when saving.”。
异常是:System.Runtime.InteropServices.COMException (0x800A03EC): HRESULT からの例外: 0x800A03EC。
转换Powerpoint:
System.Runtime.InteropServices.COMException (0x80004005): Presentation (unknown member) : An error occurred while PowerPoint was saving the file.

程序是B/S的,转换的代码是在WebService上执行的。在以前转换失败也就是网络超时或者其他的,没出现过这种错误。
这个错在我本机执行源代码时才出现。如果用发布的版本执行,目前没有出现此类错误。(以前在我本机上也没出现错误)
我的环境。VS2005。安装了SaveAsPdf和office2007。

我在网上搜索了两天,还没找到解决办法,望高手救救我。谢谢
我只有20分,我是新人,希望大侠们不要看中分数,谢谢
...全文
1066 点赞 收藏 5
写回复
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jinhq1987 2011-06-22
你的excel文件是空的
回复
losingrose 2009-04-13
http://www.microsoft.com/downloads/thankyou.aspx?familyId=4d951911-3e7e-4ae6-b059-a2e79ed87041&displayLang=en
机器只要安装这个插件就行了。
回复
g394594141 2009-02-13
看来是没有人来了,结贴了
回复
g394594141 2009-02-10
怎么没人帮帮我啊,大侠们,请指教啊
回复
g394594141 2009-02-07
晕,我还以为长度有限制呢,还这么空,下面贴出全部代码:
string tempPath = System.IO.Path.GetTempPath();
if (!Directory.Exists(tempPath))
{
Directory.CreateDirectory(tempPath);
}
tempFileName = tempPath + "\\" + strFileName + "." + astdt;
File.WriteAllBytes(tempFileName.ToString(), imageinfo.Image);
object savePath = null;
object flg = false;
object oMissing = System.Reflection.Missing.Value;
switch (astdt.ToLower())
{
case "doc":
case "docx":
astdt = "pdf";
Microsoft.Office.Interop.Word._Application oWord;
Microsoft.Office.Interop.Word._Document oDoc;
oWord = new Microsoft.Office.Interop.Word.Application();
//oWord.Visible = true;
oDoc = oWord.Documents.Open(ref tempFileName,
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);
try
{
savePath = driverPath + "\\" + mediaName + "\\" + pathName + "\\";
if (!Directory.Exists(savePath.ToString()))
{
Directory.CreateDirectory(savePath.ToString());
}
savePath = savePath + strFileName + "." + astdt;
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
oDoc.SaveAs(ref savePath, ref format,
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);
oDoc.Close(ref flg, ref oMissing, ref oMissing);
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
}
catch (Exception ex)
{
oDoc.Close(ref flg, ref oMissing, ref oMissing);
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
throw (ex);
}
break;
case "xls":
case "xlsx":
astdt = "pdf";
Microsoft.Office.Interop.Excel._Application oExcel;
oExcel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook book = oExcel.Workbooks.Open(tempFileName.ToString(),
oMissing, true, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, true, oMissing,
oMissing, oMissing, oMissing, oMissing);
try
{
//oExcel.Visible = true;
Microsoft.Office.Interop.Excel.Sheets m_objSheets = (Microsoft.Office.Interop.Excel.Sheets)book.Worksheets;
Microsoft.Office.Interop.Excel.Worksheet m_objSheet = (Microsoft.Office.Interop.Excel.Worksheet)(m_objSheets.get_Item(1));

Microsoft.Office.Interop.Excel.Range range = m_objSheet.get_Range("A1", oMissing);

range = range.get_Resize(1, 1);
object objValue = range.get_Value(oMissing);

if (objValue == null)
{
m_objSheet.Cells[1, 1] = " ";
}
savePath = driverPath + "\\" + mediaName + "\\" + pathName + "\\";
if (!Directory.Exists(savePath.ToString()))
{
Directory.CreateDirectory(savePath.ToString());
}
savePath = savePath + strFileName + "." + astdt;
book.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF,
savePath, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
book.Close(flg, oMissing, oMissing);
oExcel.Quit();
}
catch (Exception ex)
{
book.Close(flg, oMissing, oMissing);
oExcel.Quit();
throw (ex);
}
break;
case "ppt":
case "pptx":
astdt = "pdf";
Microsoft.Office.Interop.PowerPoint.Application ppApp = new Microsoft.Office.Interop.PowerPoint.Application();
try
{
Microsoft.Office.Interop.PowerPoint.Presentation presentation = ppApp.Presentations.Open(tempFileName.ToString(),
Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoFalse);
if (presentation.Slides.Count < 1)
{
presentation.Slides.Add(1, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutBlank);
}
if (ppApp.Presentations.Count < 0)
{
ppApp.Quit();
return -1;
}
if (ppApp.Presentations[ppApp.Presentations.Count].Slides.Count < 1)
{
return -1;
}
savePath = driverPath + "\\" + mediaName + "\\" + pathName + "\\";
if (!Directory.Exists(savePath.ToString()))
{
Directory.CreateDirectory(savePath.ToString());
}
savePath = savePath + strFileName + "." + astdt;
ppApp.Presentations[ppApp.Presentations.Count].SaveAs(savePath.ToString(), Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF, Microsoft.Office.Core.MsoTriState.msoTrue);
ppApp.Presentations[ppApp.Presentations.Count].Close();
ppApp.Quit();
}
catch (Exception ex)
{
ppApp.Presentations[ppApp.Presentations.Count].Close();
ppApp.Quit();
throw (ex);
}
break;
default:
break;
}
回复
发动态
发帖子
组件/控件开发
创建于2007-09-28

8833

社区成员

.NET技术 组件/控件开发
申请成为版主
社区公告
暂无公告