使用Outlook Interop加载Office程序集失败问题求助
Fix Outlook Interop Issues in .NET Core WinForms
Step 1: Use the Official NuGet Package
- Remove any existing COM references to Outlook from your project.
- Uninstall outdated
Microsoft.Office.Interop.OutlookNuGet packages if present. - Install the latest
Microsoft.Office.Interop.Outlookpackage via NuGet Package Manager. This package provides pre-built interop assemblies optimized for .NET Core, avoiding the compilation errors that come with direct COM references.
Step 2: Resolve Assembly Version Mismatch
If you still encounter the Could not load file or assembly 'office, Version=15.0.0.0' error, add an assembly binding redirect to your app.config file to map the requested version to the one installed on your machine:
<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="office" publicKeyToken="71e9bce111e9429c" culture="neutral" /> <!-- Replace "16.0.0.0" with your installed Office version (16 for 2016/365, 15 for 2013) --> <bindingRedirect oldVersion="0.0.0.0-16.0.0.0" newVersion="16.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime>
Step 3: Match Platform Target to Office Installation
Office is often installed as 32-bit even on 64-bit systems. Align your project's platform target to avoid runtime conflicts:
- Right-click your project → Properties → Build.
- Under "Platform target", select
x86if Office is 32-bit, orx64if it's 64-bit. - Uncheck "Prefer 32-bit" if enabled.
Step 4: Fix Code & Clean Up COM Objects
Adjust your VB code to replace placeholder variables and properly release COM objects (to prevent lingering Outlook processes):
Imports Microsoft.Office.Interop.Outlook ' ... Dim outlookApp As Outlook.Application = Nothing Dim mail As Outlook.MailItem = Nothing Try outlookApp = New Outlook.Application() mail = CType(outlookApp.CreateItem(OlItemType.olMailItem), MailItem) ' Replace strExportFileName with your actual file name variable mail.Subject = $"Envoi du fichier '{strExportFileName}'" mail.Body = "" mail.[To] = "" ' Replace strExportFilePath with your actual file path variable Dim attachmentPath As String = strExportFilePath mail.Attachments.Add(attachmentPath, OlAttachmentType.olByValue, Type.Missing, Type.Missing) mail.Display(True) Finally ' Clean up COM objects to avoid memory leaks If mail IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(mail) End If If outlookApp IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(outlookApp) End If End Try
Note: Ensure Microsoft Outlook is installed on the target machine, and the user running the app has permissions to launch and access Outlook.
内容的提问来源于stack exchange,提问作者8oris
相关产品推荐
相关产品推荐

