SaveAs2 For Word 2010无法与使用Word 2007的客户端PC一起使用

时间:2012-03-28 21:04:28

标签: vb.net visual-studio-2010 ms-word ms-office

我开发了一个安装了Office 2010 Professional的VB.Net(VS2010)的WinForm应用程序,它是64位Windows 7平台。该程序打开.doc和.rtf格式文档,并尝试以htm格式保存它。我正在使用以下命令:

Dim sFilePath as String =“C:\ ABC \ file.doc”

        Dim oApp As New Microsoft.Office.Interop.Word.Application
        Dim oDoc As New Microsoft.Office.Interop.Word.Document
        Dim sTempFileName As String = System.IO.Path.GetTempFileName()
        oDoc = oApp.Documents.Open(sFilePath)
        oApp.Visible = False
        oDoc = oApp.ActiveDocument
        oDoc.SaveAs2(sTempFileName, FileFormat:=WdSaveFormat.wdFormatHTML,CompatibilityMode:=Microsoft.Office.Interop.Word.WdCompatibilityMode.wdWord2007)
        oDoc.Close()
        oApp.Quit()
        oDoc = Nothing
        oApp = Nothing

开发和运行在开发PC上一切顺利,但是当我将其发布用于离线安装,并将其部署在具有带Office 2007的Windows XP的客户端PC上时,它会在oDoc.SaveAs2行上出错,并且程序崩溃。我已经google了足够但无法找到解决方案。有人请尽快帮助我

1 个答案:

答案 0 :(得分:3)

From MSDN

<强> SaveAs2
此方法出现在针对.NET Framework 4的Word 2007项目中的IntelliSense中。但是,此属性不能在Word 2007项目中使用

顺便说一句,如果您在此网站上搜索,则会在问题here

中找到答案

您可以使用以下代码检查用户PC上安装的当前Word的版本:

string v = _myWordApp.Version;
switch(v)
{
    case "7.0":
    case "8.0":
    case "9.0":
    case "10.0":
    _myWordDoc.SaveAs2000(ref _documentFile, ref _nothing, ref _nothing, ref _nothing, 
        ref _nothing, ref _nothing, ref _nothing, ref _nothing, 
        ref _nothing, ref _nothing, ref _nothing);
      break; 
    case "11.0":
    case "12.0"
    _myWordDoc.SaveAs(ref _documentFile, ref _nothing, ref _nothing, ref _nothing, 
        ref _nothing, ref _nothing, ref _nothing, ref _nothing, 
        ref _nothing, ref _nothing, ref _nothing, ref _nothing,
        ref _nothing, ref _nothing, ref _nothing, ref _nothing);
    case "14.0"
    _myWordDoc.SaveAs2(ref _documentFile, ref WdSaveFormat.wdFormatHTML, 
                ref _nothing, ref _nothing, ref _nothing, 
        ref _nothing, ref _nothing, ref _nothing, ref _nothing, 
        ref _nothing, ref _nothing, ref _nothing, ref _nothing,
        ref _nothing, ref _nothing, ref _nothing, 
                ref Microsoft.Office.Interop.Word.WdCompatibilityMode.wdWord2007);
      break;
    default:
      errorText = "Not able to get Word Version"
      break;
} 

对不起C#代码,但很容易翻译。