public static void insertFileAtTag(Document doc, dataStructures.Parameters parameters, dataStructures.row row, string fileExt)
        {
            // select empty range in document
            Range rng = doc.Range(0, 0);
            Range tmp = doc.Range(0, 0);

            // Find location to insert images
            if (rng.Find.Execute("<" + row.param + ">"))
            {
                // range is now set to bounds of the word "<" + parameter name + ">"

                // loop through file list and output to word document
                for (int k = 0; k < row.oneDimArray.GetLength(0); k++)
                {
                    string filename = row.oneDimArray[k].ToString();

                    // insert a new text value for the image to be pasted over
                    rng.InsertBefore("<" + filename + ">");

                    // define temporary range as inserted tag
                    tmp = doc.Range(0, 0);
                    tmp.Find.Execute("<" + filename + ">");

                    // insert File into document
                    insertFile(doc, tmp, filename, fileExt, row, parameters);
                }

                // clean up document tags
                rng.Find.Execute("<" + row.param + ">");
                rng.Delete();
            }
        }
Example #2
0
        // Main method of application
        // Entry point of code
        // static = do not need to construct an instance of the parent class to run this method.
        // eg. Program.Main() works
        // void = method does not return any results
        // string[] args = arguements passed to application from shell console.
        static void Main(string[] args)
        {
            // Ensure json arguements have been passed to the application
            if (args.Length != 0)
            {
                var json = args[0];
                //var json = testCases(1);

                // Convert JSON text string to C# object
                dataStructures.Parameters parameters = JsonConvert.DeserializeObject <dataStructures.Parameters>(json);

                // Call main loop for creating word document
                createWordDocument(parameters);
            }
        }
        public static void insertFileAtTable(Document doc, dataStructures.Parameters parameters, dataStructures.row row, string fileExt)
        {
            var    count    = doc.Tables.Count;
            object oMissing = System.Reflection.Missing.Value;

            // loop through all tables in word document
            for (var i = 1; i <= count; i++)
            {
                // Find table specific to current variable to be inserted into document
                if (doc.Tables[i].Title == row.insertTableName)
                {
                    var tbl            = doc.Tables[i];
                    var rowInsertpoint = row.insertPoint[0];
                    var colInsertPoint = row.insertPoint[1];

                    // loop through file list and output to word document
                    for (int k = 0; k < row.oneDimArray.GetLength(0); k++)
                    {
                        // add new row to table if applicable
                        if (tbl.Rows.Count - (rowInsertpoint - 1) < k + 1)
                        {
                            tbl.Rows.Add(ref oMissing);
                        }

                        // get filename
                        string filename = row.oneDimArray[k].ToString();

                        // define insert range
                        Range rng = doc.Range(0, 0);
                        rng = tbl.Cell(k + rowInsertpoint, colInsertPoint).Range;

                        insertFile(doc, rng, filename, fileExt, row, parameters);
                    }
                }
            }
        }
Example #4
0
        // Main loop to construct the word document from a template based on the passed JSON
        public static void createWordDocument(dataStructures.Parameters parameters)
        {
            Microsoft.Office.Interop.Word.Application app;

            // Create word application
            if (appRunning("winword") == true)
            {
                app = (Microsoft.Office.Interop.Word.Application)getApplication <Microsoft.Office.Interop.Word.Application>("Word.Application");
            }
            else
            {
                app = new Microsoft.Office.Interop.Word.Application();
            }

            // Configure app for improved processing speed
            app.DisplayAlerts  = WdAlertLevel.wdAlertsNone;
            app.ScreenUpdating = false;

            // set parameters for more reliable code execution
            // setting app.Visible = true in the past has resulted in more reliable generation of charts
            // however this can result in the c# app becoming stuck when word attempts to close the document if it is not focused
            // by the operating system.
            app.Visible = false;

            // Open Template
            Document doc = app.Documents.Open(parameters.template);

            // loop through all rows in the inputs object and perform operations
            foreach (var row in parameters.inputs)
            {
                if (row.type == "value")
                {
                    wordFunctions.updateParameter(doc, row);
                }
                else if (row.type == "table")
                {
                    wordFunctions.updateTable(doc, row);
                }
                else if (row.type == "photo")
                {
                    if (row.insertTableName != null)
                    {
                        wordFunctions.insertFileAtTable(doc, parameters, row, ".jpg");
                    }
                    else
                    {
                        wordFunctions.insertFileAtTag(doc, parameters, row, ".jpg");
                    }
                }
                else if (row.type == "pdf")
                {
                    if (row.insertTableName != null)
                    {
                        wordFunctions.insertFileAtTable(doc, parameters, row, ".pdf");
                    }
                    else
                    {
                        wordFunctions.insertFileAtTag(doc, parameters, row, ".pdf");
                    }
                }
                else if (row.type == "chart")
                {
                    wordFunctions.updateChartData(doc, row);
                }
            }

            // Copy new item to clipboard to supress clipboard message
            // doc.Paragraphs[doc.Paragraphs.Count].Range.Copy();
            doc.Characters[doc.Characters.Count].Copy();

            // Create folder to store document
            // will do nothing if the directory already exists
            System.IO.Directory.CreateDirectory(parameters.documentationAssetsPath);

            string wordPath = parameters.documentationAssetsPath + @"\" + parameters.documentNumber + "-" + parameters.revision + ".docm";
            string pdfPath  = parameters.documentationAssetsPath + @"\" + parameters.documentNumber + "-" + parameters.revision + ".pdf";

            // delete files if they already exist
            generalFunctions.deleteFile(wordPath);
            generalFunctions.deleteFile(pdfPath);

            // Save changes
            doc.SaveAs(wordPath);
            doc.SaveAs2(pdfPath, WdSaveFormat.wdFormatPDF);
            doc.Close();

            // return performance defaults
            app.DisplayAlerts  = WdAlertLevel.wdAlertsAll;
            app.ScreenUpdating = true;

            // Close word.
            // app.Quit();
        }
        public static void insertFile(Document doc, Range rng, string filename, string fileExt, dataStructures.row row, dataStructures.Parameters parameters)
        {
            string path = null;

            // define file path
            if (fileExt == ".jpg")
            {
                path = row.photoAssetsPath + @"\" + filename + ".jpg";
            }
            else if (fileExt == ".pdf")
            {
                path = row.pdfAssetsPath + @"\" + filename + ".pdf";
            }

            // confirm file exists before attempting to do any operations
            if (File.Exists(path) == true)
            {
                if (fileExt == ".jpg")
                {
                    insertImage(doc, rng, path);
                }
                else if (fileExt == ".pdf")
                {
                    insertPdf(doc, rng, path, parameters.temporaryFilesPath);
                }
            }
            else
            {
                // insert text indicating no file was found
                rng.Text = filename + fileExt + " was not found";
            }
        }