Author Topic: Automatically convert word to PDF  (Read 10042 times)

umang@guixt.com

  • Administrator
  • Newbie
  • *****
  • Posts: 32
    • View Profile
Automatically convert word to PDF
« on: January 21, 2016, 10:42:50 AM »
Liquid UI: Converting Word to PDF


Step 1: User Interface [ Create on Screen of your choice ]

//Button to initiate the process from SAP Transaction
// Code Sample: User Interface

pushbutton([1,1], 'Convert Word to PDF','?',{'process':convertWordToPDF});

Step 2: File Selection Dialog: Select File for PDF Conversion

// Code Sample: Open Windows File Selection Dialog
// Function to Show File Selection Dialog

function selectFileDialog(szPrompt) { 
    if(szPrompt==void 0)
        szPrompt = 'Select Valid Word Documnet';
    var dialog = new ActiveXObject('MsComDlg.CommonDialog'); 
    dialog.Filter='All Files(*.*)|*.*'; 
    dialog.MaxFileSize=32767; 
    dialog.DialogTitle=szPrompt;
    dialog.Flags=0x200|0x80000|0x800|0x4|0x200000 
    dialog.ShowOpen(); 
    var ret = dialog.FileName;
    dialog = void 0;
    return ret;


// Calling the function
    if(myWord == void 0) {
        wordFileName = selectFileDialog('Select Word File for PDF Conversion');

 
Step 3: Successful PDF Conversion after file selection

 
Liquid UI Code [Script]

 
// Convert Word Document to PDF User Interface

function convertWordToPDF() {

    if(myWord == void 0) {
        wordFileName = selectFileDialog('Select Word File for PDF Conversion');
      
        if(wordFileName.length) {
         var wordFilePath = wordFileName.substr(0, wordFileName.lastIndexOf('\\'));
         var wordFileNameWithExtension = wordFileName.substr(wordFileName.lastIndexOf('\\'),wordFileName.length);
         var wordFileNameWithoutExtension = wordFileNameWithExtension.substr(0, wordFileNameWithExtension.lastIndexOf('.'));

         var PDFFileName = wordFilePath + wordFileNameWithoutExtension + '.pdf';
   
         wordToPDF(wordFileName,PDFFileName);
         message('S: Successfully Converted to PDF File: ' + PDFFileName);
        }
   }
}

 
// Function to convert contents of word file to PDF Format

function wordToPDF(wordFile, pdfFile) {
   myWord = new ActiveXObject('Word.Application');
//   myWord.Visible = true;    // if set, processes in foreground; otherwise the conversion is processed in the background
   myWord.DisplayAlerts = 0;
   
   myDoc = myWord.Documents.Open(wordFile);
   
   var pdfFormat = 17;
   myDoc.SaveAs(pdfFile,pdfFormat);
   
   myDoc.Close();
   myWord.Quit();
   delete myWord;
}

See attachments for code samples