Word 2010中的SaveAs2无法与安装了Word 2007的客户端电脑配合使用。

5
我已经使用VB.Net (VS2010)开发了一个WinForm应用程序,并安装了Office 2010专业版,它运行在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

在开发和运行时一切顺利,但是当我将其发布为离线安装,并在安装了Windows XP和Office 2007的客户端PC上部署时,它在oDoc.SaveAs2行上报错并崩溃。我已经谷歌了很多,但找不到解决方法。请有人尽快帮助我。

1个回答

4

MSDN上的内容:

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

顺便说一句,如果您在此网站上搜索,则可以在这里找到解决您问题的答案。

您可以使用以下代码检查用户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#代码,但很容易翻译。


1
感谢对基于Office版本的“Save”方法进行澄清!我之前一直使用错误的“SaveAs”方法,导致出现了“RPC_E_SERVERFAULT”的错误。 - SliverNinja - MSFT

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接