类型不匹配。 (HRESULT异常:0x80020005(DISP_E_TYPEMISMATCH))在参数中添加utf8后,在saveAS方法中出现请帮助解决此问题

问题描述:

object FileName = fileName;
                object utf8 = Encoding.UTF8;
                wrdDoc = wrdApp.Documents.Open(ref FileName, 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);
                wrdDoc.Activate();
              
                wrdDoc.SaveAs2(ref fileNameProcessed, ref fileFormat, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref utf8,
                    ref missing, ref missing, ref missing, ref missing);
                object saveChanges = WdSaveOptions.wdDoNotSaveChanges;

根据 https://msdn.microsoft.com/en-us/library/office/ff836084.aspx [ ^ ]编码参数是第12个(从一开始计数)但你把它作为第13个传递:

According to https://msdn.microsoft.com/en-us/library/office/ff836084.aspx[^] the encoding parameter is the 12th (starting counting with one) but you passed it as 13th:
Quote:

SaveAs2 (FileName,FileFormat,LockComments,Password,AddToRecentFiles,WritePassword,ReadOnlyRecommended,EmbedTrueTypeFonts,SaveNativePictureFormat,SaveFormsData,SaveAsAOCELetter,Encoding,InsertLineBreaks,AllowSubstitutions,LineEnding,AddBiDiMarks,CompatibilityMode)

SaveAs2(FileName, FileFormat, LockComments, Password, AddToRecentFiles, WritePassword, ReadOnlyRecommended, EmbedTrueTypeFonts, SaveNativePictureFormat, SaveFormsData, SaveAsAOCELetter, Encoding, InsertLineBreaks, AllowSubstitutions, LineEnding, AddBiDiMarks, CompatibilityMode)


继Jochen的回答之后, Encoding 参数需要一个代码页或c haracter设置,但你试图传入编码对象。



Word不知道.NET的编码类型。您需要传入 CodePage 属性:

Further to Jochen's answer, the Encoding parameter expects a code page or character set, but you're trying to pass in an Encoding object.

Word doesn't know about .NET's Encoding type. You need to pass in the CodePage property instead:
object utf8 = Encoding.UTF8.CodePage;