I'm encountering an issue deploying my .NET Core application that uses Microsoft.Office.Interop.Word via an Azure DevOps pipeline. Locally, I managed to compare Word documents using Interop Word through COMReference, resolving a previous NuGet package issue. However, during deployment through Azure DevOps, I'm facing errors.
Here's the context:
Word.Application wordApp = new Word.Application();
wordApp.Visible = false;
object wordTrue = (object)true;
object wordFalse = (object)false;
object fileToOpen = @"";
object missing = Type.Missing;
Word.Document doc1 = wordApp.Documents.Open(ref fileToOpen,
ref missing, ref wordFalse, ref wordFalse, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref wordTrue, ref missing,
ref missing, ref missing, ref missing);
object fileToOpen1 = @"";
Word.Document doc2 = wordApp.Documents.Open(ref fileToOpen1,
ref missing, ref wordFalse, ref wordFalse, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
Word.Document doc = wordApp.CompareDocuments(doc1, doc2, Word.WdCompareDestination.wdCompareDestinationNew, Word.WdGranularity.wdGranularityWordLevel,
true, true, true, true, true, true, true, true, true, true, "", true);
doc1.Close(ref missing, ref missing, ref missing);
doc2.Close(ref missing, ref missing, ref missing);
// Hides both original and revised documents
wordApp.ActiveWindow.ShowSourceDocuments = WdShowSourceDocuments.wdShowSourceDocumentsNone;
wordApp.Visible = true;
Initially, I faced a NuGet package error when using the Microsoft.Office.Interop.Word package, which led me to utilize COMReference as a workaround. While this solution worked locally, the Azure DevOps pipeline fails during the build process with the following error:
Error MSB4803: The task "ResolveComReference" is not supported on the .NET Core version of MSBuild.
Error CS0234: The type or namespace name 'Office' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
Is there an alternative approach or solution to successfully deploy a .NET Core application using Microsoft.Office.Interop.Word or any workaround to resolve this issue with Azure DevOps? Any guidance on resolving this deployment error would be greatly appreciated.
Check this Github Issue comments by quicoli and xDaevax.
You can also make use of other libraries like OpenXml
instead of OfficeIntercop
that will work well with Azure DevOps pipeline. Here's an example of OpenXml
library for comparing two Word Documents.
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
namespace DocumentComparison
{
class Program
{
static void Main(string[] args)
{
string originalFilePath = "path/to/original.docx";
string revisedFilePath = "path/to/revised.docx";
try
{
using (WordprocessingDocument originalDoc = WordprocessingDocument.Open(originalFilePath, false))
using (WordprocessingDocument revisedDoc = WordprocessingDocument.Open(revisedFilePath, false))
{
MainDocumentPart mainPartOriginal = originalDoc.MainDocumentPart;
MainDocumentPart mainPartRevised = revisedDoc.MainDocumentPart;
var altChunkId = "AltChunkId" + DateTime.Now.Ticks.ToString();
AlternativeFormatImportPart chunkOriginal = mainPartOriginal.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML, altChunkId);
chunkOriginal.FeedData(System.IO.File.Open(originalFilePath, System.IO.FileMode.Open));
AlternativeFormatImportPart chunkRevised = mainPartRevised.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML, altChunkId);
chunkRevised.FeedData(System.IO.File.Open(revisedFilePath, System.IO.FileMode.Open));
var docComparer = new DocumentComparer(mainPartOriginal, mainPartRevised);
bool isEqual = docComparer.CompareDocuments();
Console.WriteLine($"Documents are {(isEqual ? "equal" : "not equal")}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
public class DocumentComparer
{
private readonly MainDocumentPart _originalDoc;
private readonly MainDocumentPart _revisedDoc;
public DocumentComparer(MainDocumentPart originalDoc, MainDocumentPart revisedDoc)
{
_originalDoc = originalDoc;
_revisedDoc = revisedDoc;
}
public bool CompareDocuments()
{
// Perform comparison logic here
// You might traverse through paragraphs, runs, etc., comparing content
// Example: Comparing the number of paragraphs
var originalParagraphsCount = _originalDoc.Document.Body.Elements<Paragraph>().Count();
var revisedParagraphsCount = _revisedDoc.Document.Body.Elements<Paragraph>().Count();
return originalParagraphsCount == revisedParagraphsCount;
}
}
}
.NET 6.0 Web App
and the build Pipeline with VsBuild
was successful:-My Controller Code:-
using Microsoft.AspNetCore.Mvc;
using Word = Microsoft.Office.Interop.Word;
namespace YourNamespace.Controllers
{
public class WordController : Controller
{
public IActionResult CreateWordDocument()
{
Word.Application wordApp = new Word.Application();
Word.Document doc = wordApp.Documents.Add();
// Add content to the document
Word.Paragraph para1 = doc.Content.Paragraphs.Add();
para1.Range.Text = "Hello, this is a sample document.";
// Save the document
string filePath = @"C:Document.docx";
doc.SaveAs2(filePath);
// Close the document and the Word application
doc.Close();
wordApp.Quit();
// Return a response to indicate successful document creation
return Content("Word document created successfully!");
}
}
}
My Devops Build pipeline:-
trigger:
- master
pool:
vmImage: 'windows-latest'
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
steps:
- task: NuGetToolInstaller@1
- task: NuGetCommand@2
inputs:
restoreSolution: '$(solution)'
- task: VSBuild@1
inputs:
solution: '$(solution)'
msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
- task: VSTest@2
inputs:
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
Output:-