Answered by:
Word interop: how to specify "Windows encoding" in the document.Save( .....) method

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
-
Hi,
You can use Document.TextEncoding Property to set the code page, or character set.
See this MSDN Library:
http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.textencoding(v=vs.80).aspx
As this issue is more related to VSTO, I would suggest you open a new thread on Visual Studio Tools for Office Forum below for further support:
http://social.msdn.microsoft.com/Forums/en-US/vsto/
Best Regards- Marked as answer by Cristin Yan Thursday, October 11, 2012 7:53 AM
Friday, September 28, 2012 4:35 AM -
Hi,
The MsoEncoding is an enumeration in Microsoft.Office.Core namespace.
For more information about MsoEncoding Enumeration, please refer to:
http://msdn.microsoft.com/en-us/library/microsoft.office.core.msoencoding(v=office.14).aspx
Best Regards- Edited by Cristin Yan Friday, October 5, 2012 2:26 PM
- Marked as answer by Cristin Yan Thursday, October 11, 2012 7:54 AM
Friday, October 5, 2012 2:24 PM
All replies
-
Hi,
You can use Document.TextEncoding Property to set the code page, or character set.
See this MSDN Library:
http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.textencoding(v=vs.80).aspx
As this issue is more related to VSTO, I would suggest you open a new thread on Visual Studio Tools for Office Forum below for further support:
http://social.msdn.microsoft.com/Forums/en-US/vsto/
Best Regards- Marked as answer by Cristin Yan Thursday, October 11, 2012 7:53 AM
Friday, September 28, 2012 4:35 AM -
Thank you for the response. I will ask in VSTO. Microsoft.Office.Tools.Word namespace does not exist, according to VS2010, and I cannot find the MsoEncoding in Microsoft.Office.Interop.Word.
Tuesday, October 2, 2012 11:27 AM -
Hi,
The MsoEncoding is an enumeration in Microsoft.Office.Core namespace.
For more information about MsoEncoding Enumeration, please refer to:
http://msdn.microsoft.com/en-us/library/microsoft.office.core.msoencoding(v=office.14).aspx
Best Regards- Edited by Cristin Yan Friday, October 5, 2012 2:26 PM
- Marked as answer by Cristin Yan Thursday, October 11, 2012 7:54 AM
Friday, October 5, 2012 2:24 PM