locked
Word interop: how to specify "Windows encoding" in the document.Save( .....) method RRS feed

  • Question

  • I have  a DOTX with some ContentControls.  A C# program opens the DOTX, changes the Text property of the ContentControls, and saves document under a new name as a DOC, let's say FOO.DOC.

    When FOO.DOC is opened a File Conversion dialog appears, telling the user: Select the encoding that makes your document readable. Text encoding:

    (X) Windows (Default)      ( ) MS-DOS            ( ) Other encoding

    How should Windows encoding be specified in the Save method of the document so that this dialog does not appear?

    Further, I would really like to save the document as a PDF, but when I make this change to the fileFormat:

    ObjectfileFormat = (Object)WORD.WdSaveFormat.wdFormatPDF;

    and change the savedocname to "FOO.PDF", the document is saved with a default generic name using the first couple of words in the document, and with the DOCX extension.

    WORD._Application word = null; WORD._Document document = null; Object fileName = (Object) docpath; Object fileFormat = (Object)WORD.WdSaveFormat.wdFormatDocument; Object missing = (Object)Type.Missing; try { word = new WORD.Application(); document = word.Documents.Add(fileName, ///* ref object Template */ ref missing, /* ref object NewTemplate */ ref missing, /* ref object DocumentType */ ref missing,

    /* ref object Visible */ ref missing); // SNIP SNIP SNIP: `d` below is a dictionary of values whose key corresponds

    // to the ContentControl's Tag value

    foreach (WORD.ContentControl cc in document.ContentControls) { if (d.ContainsKey(cc.Tag)) { cc.LockContents = false; cc.Range.Text = d[cc.Tag]; cc.LockContents = true; } } object savedocname = (object)"FOO"]; document.SaveAs( /* ref object FileName */ ref savedocname, /* ref object FileFormat */ ref fileFormat, /* ref object LockComments */ ref missing, /* ref object Password */ ref missing, /* ref object AddToRecentFiles */ ref missing, /* ref object WritePassword */ ref missing, /* ref object ReadOnlyRecommended */ ref missing, /* ref object EmbedTrueTypeFonts */ ref missing, /* ref object SaveNativePictureFormat */ ref missing, /* ref object SaveFormsData */ ref missing, /* ref object SaveAsAOCELetter */ ref missing, /* ref object Encoding */ ref missing, /* ref object InsertLineBreaks */ ref missing, /* ref object AllowSubstitutions */ ref missing, /* ref object LineEncoding */ ref missing, /* ref object AddBiDiMarks */ ref missing); }

    Thursday, September 27, 2012 5:54 PM

Answers

All replies