Skip to content

Instantly share code, notes, and snippets.

@ildar-shaimordanov
Last active May 15, 2018 05:42
Show Gist options
  • Save ildar-shaimordanov/71355be11e53136df959be89128f18b4 to your computer and use it in GitHub Desktop.
Save ildar-shaimordanov/71355be11e53136df959be89128f18b4 to your computer and use it in GitHub Desktop.
unknown runtime error

For some reason JScript is not able to handle with the code converted from WinWord VBA.

Sub Macro1()
'
' Macro1 Macro
'
'
    With ActiveDocument
        .XMLSaveDataOnly = False
        .XMLUseXSLTWhenSaving = True
        .XMLSaveThroughXSLT = "file:///C:\z.xsl"
        .XMLHideNamespaces = False
        .XMLShowAdvancedErrors = True
        .XMLSchemaReferences.HideValidationErrors = False
        .XMLSchemaReferences.IgnoreMixedContent = False
        .XMLSchemaReferences.ShowPlaceholderText = False
    End With
    ChangeFileOpenDirectory "C:\"
    ActiveDocument.SaveAs2 _
        FileName:="z.xml", _
        FileFormat:=wdFormatXML, _
        LockComments:=False, _
        Password:="", _
        AddToRecentFiles:=True, _
        WritePassword:="", _
        ReadOnlyRecommended:=False, _
        EmbedTrueTypeFonts:=False, _
        SaveNativePictureFormat:=False, _
        SaveFormsData:=False, _
        SaveAsAOCELetter:=False, _
        CompatibilityMode:=0
End Sub

There are two files:

  • demo-not-work.js
  • demo-works.js

Only one difference between both of them: some settings crashing the first script are commented (with //?) in the second script to enable processing the script..

var docFile = 'C:\\z.docx';
var xmlFile = 'C:\\z.xml';
var xslFile = 'C:\\z.xsl';
var word = new ActiveXObject("Word.Application");
word.Visible = true;
word.Activate();
var doc = word.Documents.Open(docFile);
try {
WScript.Echo("Before setting : XMLSaveDataOnly")
doc.XMLSaveDataOnly = false;
WScript.Echo("Before setting : XMLUseXSLTWhenSaving")
doc.XMLUseXSLTWhenSaving = true;
WScript.Echo("Before setting : XMLSaveThroughXSLT")
doc.XMLSaveThroughXSLT = xslFile;
WScript.Echo("Before setting : XMLHideNamespaces")
doc.XMLHideNamespaces = true;
WScript.Echo("Before setting : XMLShowAdvancedErrors")
doc.XMLShowAdvancedErrors = true;
WScript.Echo("Before setting : XMLSchemaReferences.HideValidationErrors")
doc.XMLSchemaReferences.HideValidationErrors = false;
WScript.Echo("Before setting : XMLSchemaReferences.AutomaticValidation")
doc.XMLSchemaReferences.AutomaticValidation = true;
WScript.Echo("Before setting : XMLSchemaReferences.IgnoreMixedContent")
doc.XMLSchemaReferences.IgnoreMixedContent = false;
WScript.Echo("Before setting : XMLSchemaReferences.AllowSaveAsXMLWithoutValidation")
doc.XMLSchemaReferences.AllowSaveAsXMLWithoutValidation = true;
WScript.Echo("Before setting : XMLSchemaReferences.ShowPlaceholderText")
doc.XMLSchemaReferences.ShowPlaceholderText = false;
doc.SaveAs(
// FileName
xmlFile,
// WinWord 2003 XML Document
11,
// LockComments
false,
// Password
'',
// AddToRecentFiles
false,
// Write Password
'',
// ReadOnlyRecommended
false,
// EmbedTrueTypeFonts
false,
// SaveNativePictureFormat
false,
// SaveFormsData
false,
// SaveAsAOCELetter
false,
// Encoding
0,
// InsertLineBreaks
true,
// AllowSubstitutions
false,
// LineEnding
0);
doc.Close();
} catch(e) {
WScript.Echo(e.number);
WScript.Echo(e.description);
} finally {
word.Quit();
}
var docFile = 'C:\\z.docx';
var xmlFile = 'C:\\z.xml';
var xslFile = 'C:\\z.xsl';
var word = new ActiveXObject("Word.Application");
word.Visible = true;
word.Activate();
var doc = word.Documents.Open(docFile);
try {
WScript.Echo("Before setting : XMLSaveDataOnly")
//? doc.XMLSaveDataOnly = false;
WScript.Echo("Before setting : XMLUseXSLTWhenSaving")
doc.XMLUseXSLTWhenSaving = true;
WScript.Echo("Before setting : XMLSaveThroughXSLT")
doc.XMLSaveThroughXSLT = xslFile;
WScript.Echo("Before setting : XMLHideNamespaces")
//? doc.XMLHideNamespaces = true;
WScript.Echo("Before setting : XMLShowAdvancedErrors")
doc.XMLShowAdvancedErrors = true;
WScript.Echo("Before setting : XMLSchemaReferences.HideValidationErrors")
doc.XMLSchemaReferences.HideValidationErrors = false;
WScript.Echo("Before setting : XMLSchemaReferences.AutomaticValidation")
//? doc.XMLSchemaReferences.AutomaticValidation = true;
WScript.Echo("Before setting : XMLSchemaReferences.IgnoreMixedContent")
doc.XMLSchemaReferences.IgnoreMixedContent = false;
WScript.Echo("Before setting : XMLSchemaReferences.AllowSaveAsXMLWithoutValidation")
//? doc.XMLSchemaReferences.AllowSaveAsXMLWithoutValidation = true;
WScript.Echo("Before setting : XMLSchemaReferences.ShowPlaceholderText")
doc.XMLSchemaReferences.ShowPlaceholderText = false;
doc.SaveAs(
// FileName
xmlFile,
// WinWord 2003 XML Document
11,
// LockComments
false,
// Password
'',
// AddToRecentFiles
false,
// Write Password
'',
// ReadOnlyRecommended
false,
// EmbedTrueTypeFonts
false,
// SaveNativePictureFormat
false,
// SaveFormsData
false,
// SaveAsAOCELetter
false,
// Encoding
0,
// InsertLineBreaks
true,
// AllowSubstitutions
false,
// LineEnding
0);
doc.Close();
} catch(e) {
WScript.Echo(e.number);
WScript.Echo(e.description);
} finally {
word.Quit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment