I am writing a web service and would like to change the .docx or .doc to .xps. I'm using office com to help me to save as .xps format as following:
[WebMethod]
public string GetDocPreviewUrl(string m_userName, string m_orgFileName)
{
string m_returnUrl = "";
string m_orgFilePath = _currentDirectory + "" + m_userName + "" + m_orgFileName;
if (File.Exists(m_orgFilePath))
{
string m_xpsFilePath = _currentDirectory + "" + m_userName + "" +
Path.GetFileNameWithoutExtension(m_orgFileName) + ".xps";
OfficeToXpsConversionResult m_converstionResult = OfficeToXps.ConvertToXps(m_orgFilePath, ref m_xpsFilePath);
m_returnUrl = _baseUrl + m_userName + "/"+ Path.GetFileName(m_xpsFilePath);
}
return m_returnUrl;
}
private static OfficeToXpsConversionResult ConvertFromWord(string sourceFilePath, ref string resultFilePath)
{
object pSourceDocPath = sourceFilePath;
string pExportFilePath = string.IsNullOrWhiteSpace(resultFilePath) ? GetTempXpsFilePath() : resultFilePath;
Word.Application() wordApplication = new Word.Application();
//wordDocument = wordApplication.Documents.Open(ref pSourceDocPath);
dynamic wordDocument = wordApplication.Documents.Add(pSourceDocPath);
//return new OfficeToXpsConversionResult(ConversionResult.ErrorUnableToOpenOfficeFile, exc.Message, exc);
if (wordDocument != null)
{
wordDocument.SaveAs(pExportFilePath, WdSaveFormat.wdFormatXPS);
}
resultFilePath = pExportFilePath;
return new OfficeToXpsConversionResult(ConversionResult.OK, pExportFilePath);
}
However, I got exception when I try to call by the web method:
System.Runtime.InteropServices.COMException: Word 發生問題。
at System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult, ExcepInfo& excepInfo, UInt32 argErr, String message)
at CallSite.Target(Closure , CallSite , ComObject , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
at CallSite.Target(Closure , CallSite , Object , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
at DocProcessService.OfficeToXps.ConvertFromWord(String sourceFilePath, String& resultFilePath) in C:UsersIcicleDocumentsFotomax WP7DocProcessServiceDocProcessServiceOfficeHelperOfficeToXps.cs:line 145
at DocProcessService.OfficeToXps.ConvertToXps(String sourceFilePath, String& resultFilePath) in C:UsersIcicleDocumentsFotomax WP7DocProcessServiceDocProcessServiceOfficeHelperOfficeToXps.cs:line 63
at DocProcessService.DocDownload.GetDocPreviewUrl(String m_userName, String m_orgFileName) in C:UsersIcicleDocumentsFotomax WP7DocProcessServiceDocProcessServiceDocDownload.asmx.cs:line 90
The code of using office to save as xps was working well in my WPF project. How can I make use it in my asp.net 4.0 web service? Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…