@ -20,6 +20,7 @@ using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS ;
using System.IO ;
using Microsoft.VisualBasic ;
using System.Net.NetworkInformation ;
namespace OnDocOffice
{
@ -275,9 +276,14 @@ namespace OnDocOffice
{
try
{
word = new Microsoft . Office . Interop . Word . Application ( ) ;
word . Run ( "Autoexec" ) ;
word . NormalTemplate . Saved = true ;
//Type WordType = Type.GetTypeFromProgID("Word.Application");
//word = Activator.CreateInstance(WordType);
////ExcelInst.Visible = true;
word = ( Microsoft . Office . Interop . Word . Application ) Interaction . CreateObject ( "Word.Application" ) ;
//word = new Microsoft.Office.Interop.Word.Application();
word . Run ( "Autoexec" ) ;
word . NormalTemplate . Saved = true ;
return true ;
}
catch
@ -286,6 +292,7 @@ namespace OnDocOffice
}
}
public void Edit_Document ( bool runmacros )
{
Start_Application ( ) ;
@ -350,6 +357,53 @@ namespace OnDocOffice
}
}
public void Word_Compatibility_Mode ( ref WordDocument wd )
{
CompatibilityMode compatibilityMode = wd . Settings . CompatibilityMode ;
var formattype_original = wd . ActualFormatType ;
switch ( compatibilityMode )
{
case CompatibilityMode . Word2010 :
wd . Settings . CompatibilityMode = CompatibilityMode . Word2010 ;
break ;
case CompatibilityMode . Word2013 :
wd . Settings . CompatibilityMode = CompatibilityMode . Word2013 ;
break ;
default :
// wd.Settings.CompatibilityMode = CompatibilityMode.Word2003;
break ;
}
}
private string set_cmode ( string file )
{
string formattype = "" ;
MemoryStream ms = new MemoryStream ( Helper . EncodeExtensions . DecodeBase642ByteArray ( file ) ) ;
WordDocument document = new WordDocument ( ms , FormatType . Automatic ) ;
CompatibilityMode compatibilityMode = document . Settings . CompatibilityMode ;
formattype = document . ActualFormatType . ToString ( ) ;
var formattype_original = document . ActualFormatType ;
Word_Compatibility_Mode ( ref document ) ;
//switch (compatibilityMode)
//{
// case CompatibilityMode.Word2010:
// document.Settings.CompatibilityMode = CompatibilityMode.Word2010;
// break;
// case CompatibilityMode.Word2013:
// document.Settings.CompatibilityMode = CompatibilityMode.Word2010;
// break;
// default:
// document.Settings.CompatibilityMode = CompatibilityMode.Word2003;
// break;
//}
MemoryStream destms = new MemoryStream ( ) ;
document . Save ( destms , formattype_original ) ;
document . Close ( ) ;
document . Dispose ( ) ;
byte [ ] imageArray = destms . ToArray ( ) ;
return Convert . ToBase64String ( imageArray ) ;
}
public string Generate_Word_in_Office ( ref clsDocData docdata , ref clsdok dok , string vorlage , string connectionstring , string tempdir , string dokumentid , string apptype , string extension , int OfficeSleep , int bookmarks_docio )
{
this . dokumentid = docdata . Dokumentid ;
@ -357,85 +411,94 @@ namespace OnDocOffice
int pos = 0 ;
int pos2 = 0 ;
VBFileManagement . VBFileManagement vb = new VBFileManagement . VBFileManagement ( ) ;
vb . Get_From_DB ( Convert . ToInt32 ( docdata . VorlageNr ) , filename , connectionstring , false ) ;
Helper . clsFileHelper fh = new Helper . clsFileHelper ( ) ;
vorlage = fh . Base64FromFile ( filename ) ;
vorlage = set_cmode ( vorlage ) ;
fh . SaveBase64ToFile ( vorlage , filename ) ;
Logging . Logging . Debug ( "Generate_Word_in_Office" , "clsOffice" , dokumentid ) ;
Start_Application ( ) ;
Logging . Logging . Debug ( "Ende Start_Application" , "clsOffice" , dokumentid ) ;
// Thread.Sleep(OfficeSleep);
worddoc = word . Documents . Open ( filename ) ;
Thread . Sleep ( OfficeSleep ) ;
string ext = System . IO . Path . GetExtension ( filename ) . ToUpper ( ) ;
switch ( word . ActiveDocument . CompatibilityMode )
{
case 14 :
switch ( ext )
{
case ".DOCX" :
word . ActiveDocument . SaveAs2 ( filename , CompatibilityMode : 14 , FileFormat : Microsoft . Office . Interop . Word . WdSaveFormat . wdFormatXMLDocument ) ;
break ;
case ".DOCM" :
word . ActiveDocument . SaveAs2 ( filename , CompatibilityMode : 14 , FileFormat : Microsoft . Office . Interop . Word . WdSaveFormat . wdFormatXMLDocumentMacroEnabled ) ;
break ;
case ".DOTX" :
word . ActiveDocument . SaveAs2 ( filename , FileFormat : Microsoft . Office . Interop . Word . WdSaveFormat . wdFormatXMLTemplate , CompatibilityMode : 14 ) ;
break ;
case "DOTM" :
word . ActiveDocument . SaveAs2 ( filename , FileFormat : Microsoft . Office . Interop . Word . WdSaveFormat . wdFormatXMLTemplateMacroEnabled , CompatibilityMode : 14 ) ;
break ;
}
break ;
case 15 :
case 16 :
case 17 :
case 18 :
switch ( ext )
{
case ".DOCX" :
word . ActiveDocument . SaveAs2 ( filename , FileFormat : Microsoft . Office . Interop . Word . WdSaveFormat . wdFormatXMLDocument ) ;
break ;
case ".DOCM" :
word . ActiveDocument . SaveAs2 ( filename , FileFormat : Microsoft . Office . Interop . Word . WdSaveFormat . wdFormatXMLDocumentMacroEnabled ) ;
break ;
case ".DOTX" :
word . ActiveDocument . SaveAs2 ( filename , FileFormat : Microsoft . Office . Interop . Word . WdSaveFormat . wdFormatXMLTemplate ) ;
break ;
case "DOTM" :
word . ActiveDocument . SaveAs2 ( filename , FileFormat : Microsoft . Office . Interop . Word . WdSaveFormat . wdFormatXMLTemplateMacroEnabled ) ;
break ;
}
break ;
default :
switch ( ext )
{
case ".DOCX" :
word . ActiveDocument . SaveAs2 ( filename , CompatibilityMode : 11 , FileFormat : Microsoft . Office . Interop . Word . WdSaveFormat . wdFormatXMLDocument ) ;
break ;
case ".DOCM" :
word . ActiveDocument . SaveAs2 ( filename , CompatibilityMode : 11 , FileFormat : Microsoft . Office . Interop . Word . WdSaveFormat . wdFormatXMLDocumentMacroEnabled ) ;
break ;
case ".DOTX" :
word . ActiveDocument . SaveAs2 ( filename , FileFormat : Microsoft . Office . Interop . Word . WdSaveFormat . wdFormatXMLTemplate , CompatibilityMode : 11 ) ;
break ;
case "DOTM" :
word . ActiveDocument . SaveAs2 ( filename , FileFormat : Microsoft . Office . Interop . Word . WdSaveFormat . wdFormatXMLTemplateMacroEnabled , CompatibilityMode : 11 ) ;
break ;
}
break ;
}
Logging. Logging . Debug ( "Word Open" , "clsOffice" , dokumentid ) ;
Logging . Logging . Debug ( word . ActiveDocument . CompatibilityMode .ToString ( ), ext , dokumentid ) ;
//switch (word.ActiveDocument.CompatibilityMode)
//{
// case 14:
// switch (ext)
// {
// case ".DOCX":
// word.ActiveDocument.SaveAs2(filename, CompatibilityMode:14, FileFormat: Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXMLDocument) ;
// break;
// case ".DOCM":
// word.ActiveDocument.SaveAs2(filename, CompatibilityMode:14, FileFormat: Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXMLDocumentMacroEnabled) ;
// break;
// case ".DOTX":
// word.ActiveDocument.SaveAs2(filename, FileFormat: Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXMLTemplate, CompatibilityMode: 14) ;
// break;
// case "DOTM":
// word.ActiveDocument.SaveAs2(filename, FileFormat: Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXMLTemplateMacroEnabled, CompatibilityMode: 14) ;
// break;
// }
// break;
// case 15 :
// case 16 :
// case 17 :
// case 18:
// switch (ext)
// {
// case ".DOCX":
// word.ActiveDocument.SaveAs2(filename, FileFormat: Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXMLDocument) ;
// break;
// case ".DOCM":
// word.ActiveDocument.SaveAs2(filename, FileFormat: Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXMLDocumentMacroEnabled) ;
// break;
// case ".DOTX":
// word.ActiveDocument.SaveAs2(filename, FileFormat: Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXMLTemplate) ;
// break;
// case "DOTM":
// word.ActiveDocument.SaveAs2(filename, FileFormat: Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXMLTemplateMacroEnabled) ;
// break;
// }
// break;
// default:
// switch (ext)
// {
// case ".DOCX":
// word.ActiveDocument.SaveAs2(filename, CompatibilityMode: 11, FileFormat: Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXMLDocument) ;
// break;
// case ".DOCM":
// word.ActiveDocument.SaveAs2(filename, CompatibilityMode: 11, FileFormat: Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXMLDocumentMacroEnabled) ;
// break;
// case ".DOTX":
// word.ActiveDocument.SaveAs2(filename, FileFormat: Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXMLTemplate, CompatibilityMode: 11) ;
// break;
// case "DOTM":
// word.ActiveDocument.SaveAs2(filename, FileFormat: Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXMLTemplateMacroEnabled, CompatibilityMode: 11) ;
// break;
// }
// break;
//}
word. NormalTemplate . Saved = true ;
//word.ActiveDocument.Close(false);
Thread . Sleep ( OfficeSleep ) ;
//worddoc = word.Documents.Open(filename);
Logging . Logging . Debug ( "Word Open" , "clsOffice" , dokumentid ) ;
Logging . Logging . Debug ( "Word befüllen" , "clsOffice" , dokumentid ) ;
Thread . Sleep ( OfficeSleep ) ;
if ( worddoc . ProtectionType ! = Microsoft . Office . Interop . Word . WdProtectionType . wdNoProtection )
{
worddoc . Unprotect ( Password : "Australia" ) ;
@ -573,12 +636,17 @@ namespace OnDocOffice
if ( is_protected )
{
worddoc . Protect ( Type : Microsoft . Office . Interop . Word . WdProtectionType . wdAllowOnlyFormFields , NoReset : true , Password : "Australia" ) ;
try
{
worddoc . Protect ( Type : Microsoft . Office . Interop . Word . WdProtectionType . wdAllowOnlyFormFields , NoReset : true , Password : "Australia" ) ;
}
catch { }
}
//word.Visible = true;
Logging . Logging . Debug ( "Start Macros" , "clsOffice" , dokumentid ) ;
run_macros ( ref docdata , connectionstring ) ;
worddoc . Save ( ) ;
Logging . Logging . Debug ( "Word Saved" , "clsOffice" , dokumentid ) ;
@ -932,6 +1000,22 @@ namespace OnDocOffice
using ( FileStream inputFileStream = new FileStream ( filename , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) ) ;
WordDocument document = new WordDocument ( ) ;
document . Open ( filename , FormatType . Automatic ) ;
Word_Compatibility_Mode ( ref document ) ;
//CompatibilityMode compatibilityMode = document.Settings.CompatibilityMode;
//var formattype_original = document.ActualFormatType;
//switch (compatibilityMode)
//{
// case CompatibilityMode.Word2010:
// document.Settings.CompatibilityMode = CompatibilityMode.Word2010;
// break;
// case CompatibilityMode.Word2013:
// document.Settings.CompatibilityMode = CompatibilityMode.Word2010;
// break;
// default:
// document.Settings.CompatibilityMode = CompatibilityMode.Word2003;
// break;
//}
foreach ( clsDocValue dv in docdata . DocValues )
{
if ( docdata . Form_ohne_Unterschrift = = "True" )
@ -1025,8 +1109,18 @@ namespace OnDocOffice
}
else
{
bookmarkNavigator . MoveToBookmark ( dv . TMBeginn . ToString ( ) ) ;
bookmarkNavigator . InsertText ( dv . Value . ToString ( ) ) ;
Syncfusion . DocIO . DLS . Bookmark bookmark ;
bookmark = document . Bookmarks . FindByName ( dv . TMBeginn . ToString ( ) ) ;
if ( bookmark ! = null )
{
bookmarkNavigator . MoveToBookmark ( dv . TMBeginn . ToString ( ) , true , false ) ;
bookmarkNavigator . MoveToBookmark ( dv . TMBeginn . ToString ( ) ) ;
bookmarkNavigator . InsertText ( dv . Value . ToString ( ) ) ;
bookmarkNavigator . ReplaceBookmarkContent ( dv . Value . ToString ( ) , true ) ;
//TextBodyPart bookmarkContent = bookmarkNavigator.GetBookmarkContent();
//string bookmarkText = ((Syncfusion.DocIO.DLS.WParagraph)bookmarkContent.BodyItems.FirstItem).Text;
}
}
}