Error throw whilst saving programmatically created document containing image

1

I have C# function that saves a supplied image into a PDF file using VSTO. But it is throwing:

System.AccessViolationException HResult=0x80004003
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Source= StackTrace:

I have tried saving in various ways: see commented code:

public static Boolean ConvertImageFileToPDF(string imageFileName, string outputFileName, string attachmentName)
{
    try
    {
        Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
        Microsoft.Office.Interop.Word.Document wordDoc;

        wordDoc = wordApp.Documents.Add();

        wordDoc.Content.Font.Name = "Arial";
        wordDoc.Content.Font.Size = 12;
        wordDoc.Content.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
        wordDoc.Content.Text = attachmentName;

        wordDoc.Content.InsertParagraphAfter();

        wordDoc.InlineShapes.AddPicture(imageFileName, false, true);

        wordDoc.Application.ActiveDocument.SaveAs2(outputFileName, WdSaveFormat.wdFormatPDF);
        //wordDoc.SaveAs2(outputFileName, WdSaveFormat.wdFormatPDF);
        //wordDoc.SaveAs(outputFileName, WdSaveFormat.wdFormatPDF);
        wordDoc.Close();
        wordApp.Application.Quit(false);
        return true;
    }
    catch
    {
        return false;
    }

The save location is valid (a simple temporary directory that is used elsewhere within the code without problems).

I've searched for solutions without luck, can anyone help please?

c#
ms-word
vsto
asked on Stack Overflow Oct 27, 2019 by Mark • edited Oct 28, 2019 by Cindy Meister

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0