none
ExportAsFixedFormat, SaveAs, SaveAs2 all fail on one Word document to convert to PDF RRS feed

  • Question

  • I have this code to open a document and do a exportasfixedformat and it hangs Word.

    I have tried SaveAs, SaveAs2 and they both have the same results.

    The document can be saved as a pdf if I physically use Word through the UI.

    Here is the doc

    http://cl.ly/2e2O2q3b3m41

    Please help


    Word.Application wordApp = new Word.Application();
    wordApp.Visible = false;
    Object missing = System.Type.Missing;
    Word.Document document = wordApp.Documents.Open(@"C:\Test\bad5.doc", ref missing, ref missing, false, ref missing, ref missing, true, ref missing, ref missing, ref missing, ref missing, false, true, ref missing, false, ref missing);
    
    document.Activate();
    try
    {                document.ExportAsFixedFormat(@"C:\Test\p.pdf", Word.WdExportFormat.wdExportFormatPDF,false, Word.WdExportOptimizeFor.wdExportOptimizeForPrint,Word.WdExportRange.wdExportAllDocument, 0, 0,Word.WdExportItem.wdExportDocumentWithMarkup, true, false, Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks, false, false, false, ref missing);   
    }
    catch (Exception e)
    {
         Console.WriteLine(e);
    }



    • Edited by milk242 Saturday, June 29, 2013 3:02 AM
    Saturday, June 29, 2013 3:02 AM

Answers

  • Hi milk

    OK, I get an error as well with VBA:
    "The export failed due to an unexpected error."

    The textbox is the problem, and it's because it's anchored to the first character. This is a known issue with earlier versions of Word, especially, when an object anchor is the first character in the document. Good detective work, narrowing this down, by the way!

    If you anchor the object to a later paragraph, the problem goes away. You can see the object anchor if you activate the option (File/Options/Display), then click on the edge of the text box (so that the handles are visible). Drag the anchor down to another paragraph, save the result and test your code.

    Unfortunately, there's no way to emulate this anchor drag using the object model. The only way to change an object anchor programmatically is to delete (or CUT) the object then recreate it targeting a different Range in the Add method (or Paste method).

    Or remove that space at the beginning of the textbox content.

    But I'm guessing your problem is that you have no control of the content of the documents your solution encounters? Then about the best you can do is trap the exception and inform someone that the document is "damaged" and cannot be processed without first being corrected. And document this problem somewhere.


    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, June 30, 2013 7:12 PM
    Moderator
  • Hi Cindy,

    Thank you for explaining the exact problem to me. I did find a work around, apparently using Office 2013's Object model library, it starts working, so that might be the path I go.

    Tuesday, July 2, 2013 4:06 PM

All replies

  • Which version of Word are you using?

    IF you manually open and save the *.doc file to *.docx then try your code, does it work with no problems?

    Is the problem with only the one file?


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, June 29, 2013 6:51 AM
    Moderator
  • Hi Cindy,

    I have tried to convert it to docx manually and ran it through my code again and it is the same error. It is for only this one document. The document I attached, I removed all extraneous information and narrowed it down to the combination of the text box, space character and the 1 character. I have even replaced the text box and copied the content of the old one into it and it still fails, but for some magical reason manually converting it into PDF (by opening up Word) works fine.

    Oh and I'm using Microsoft Office 2010 with v14 of the Microsoft Object Library


    • Edited by milk242 Saturday, June 29, 2013 6:46 PM adding version
    Saturday, June 29, 2013 6:40 PM
  • Hi milk

    OK, I get an error as well with VBA:
    "The export failed due to an unexpected error."

    The textbox is the problem, and it's because it's anchored to the first character. This is a known issue with earlier versions of Word, especially, when an object anchor is the first character in the document. Good detective work, narrowing this down, by the way!

    If you anchor the object to a later paragraph, the problem goes away. You can see the object anchor if you activate the option (File/Options/Display), then click on the edge of the text box (so that the handles are visible). Drag the anchor down to another paragraph, save the result and test your code.

    Unfortunately, there's no way to emulate this anchor drag using the object model. The only way to change an object anchor programmatically is to delete (or CUT) the object then recreate it targeting a different Range in the Add method (or Paste method).

    Or remove that space at the beginning of the textbox content.

    But I'm guessing your problem is that you have no control of the content of the documents your solution encounters? Then about the best you can do is trap the exception and inform someone that the document is "damaged" and cannot be processed without first being corrected. And document this problem somewhere.


    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, June 30, 2013 7:12 PM
    Moderator
  • Hi Cindy,

    Thank you for explaining the exact problem to me. I did find a work around, apparently using Office 2013's Object model library, it starts working, so that might be the path I go.

    Tuesday, July 2, 2013 4:06 PM
  • OK, sounds as if the problem may have been fixed in that version, then.

    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, July 2, 2013 4:21 PM
    Moderator