Word Document Visibility

Dec 13, 2012 at 7:19 AM

Hi,

I am currently buidling a LS application which requires a simple one page report.

I have built this using your tool and it is working well. The only problem I am having is that I just want to output the PDF. Is there any way I can do all of the generating in memory so that the user is not presented with the word document?

The end goal is to just have the pdf print automatically when the user clicks "Generate Report". But I will tackle this end goal once I have managed to supress the word document.

Here is my curent method for testing:

note: I am taking temporary copies of the template file as the user may need to run the report multiple times before closing the original instance. If there is a better way of doing this with your tool, please let me know. Thanks

        Private Sub GenerateReport_Execute()

            Dim MyDocs = ManufacturingData.DocumentTemplateFolder
            Dim WordFile = MyDocs & "OTM.docx"
            Dim TempWordFile As FileInfo

            TempWordFile = ManufacturingData.CreateTimeStampCopy(WordFile, "OTM")

            If TempWordFile IsNot Nothing AndAlso Me.SearchItems.SelectedItem IsNot Nothing Then
                If File.Exists(WordFile) Then
                    'Map the content control tag names in the word document to the entity field names
                    Dim custFields As New List(Of OfficeIntegration.ColumnMapping)
                    custFields.Add(New OfficeIntegration.ColumnMapping("PartNumber", "ItemNumber"))

                    If SearchItems.SelectedItem.ToolingInvestigationImage IsNot Nothing Then    'Optional parameter
                        custFields.Add(New OfficeIntegration.ColumnMapping("Image1", "ToolingInvestigationImage"))
                    End If

                    Dim doc As Object = OfficeIntegration.Word.GenerateDocument(TempWordFile.FullName, Me.SearchItems.SelectedItem, custFields)

                    Dim pdfDocName As String = Replace(TempWordFile.FullName, ".docx", ".pdf")
                    OfficeIntegration.Word.SaveAsPDF(doc, pdfDocName, True)
                Else
                    ShowMessageBox(WordFile)
                End If
            End If
        End Sub

Mar 13, 2013 at 4:35 PM
Hi,

Just wondering if anyone from the OfficeIntegration team have a reply with regards to NOT opening the Word document.

In my particular case I have very thick users who keep thinking they need to save the Word document, even though I am automatically opening up the PDF created with SavePDF method.

Any feedback would be appreciated.

Thanks
Apr 23, 2013 at 4:46 PM
Hello,
I have the same problem!
Did you find a solution?

Thanks
Apr 23, 2013 at 11:56 PM
Using the above code as a starting point, add the lines beneath the generatedocument method. Note that there'll still be momentary "flash" of Word.
                Dim doc As Object = OfficeIntegration.Word.GenerateDocument(TempWordFile.FullName, Me.SearchItems.SelectedItem, custFields)
                Dim wordApp = OfficeIntegration.Word.GetWord
                wordApp.visible = False

'and then to quit Word without saving--otherwise you're left with pesky Winword.exe still running
               
                wordApp.quit(False)
Jun 24, 2013 at 4:25 PM
The following does not work for me. Word is visible and while the doc is close the application does not quit.
dynamic word = OfficeIntegration.Word.GetWord();
            if (word != null)
            {
                word.Application.Visible = false;
                
                List<OfficeIntegration.ColumnMapping> custFields = new List<OfficeIntegration.ColumnMapping>();
                //mapping

                dynamic doc = OfficeIntegration.Word.GenerateDocument(WordFile, this.VentesPro.SelectedItem, custFields);

                if (doc != null)
                {
                    //CODE 
                    doc.Close(false);
                }

                word.Application.Quit(false);
            }
Did anyone make it quit?
Tks.
Jun 25, 2013 at 11:39 PM
Edited Jun 25, 2013 at 11:39 PM
I'm not a C# guy because brackets and semi-colons give me heahaches and eye strain ;) ...
but try moving the GetWord and word.Application.Visible = false code so that it follows GenerateDocument:


``
            dynamic doc = OfficeIntegration.Word.GenerateDocument(WordFile, this.VentesPro.SelectedItem, custFields);
            dynamic word = OfficeIntegration.Word.GetWord();
             word.Application.Visible = false;
            if (doc != null)
            {
                //CODE 
                doc.Close(false);
            }

            word.Application.Quit(false);
        }`
```
Jun 26, 2013 at 1:20 PM
It did not work :-(
Jun 26, 2013 at 4:22 PM
Bummer. The following VB code I just tested successfully
    Private Sub TestForfm1236Method_Execute()
        ' Write your code here.
        Dim wordFile As Object = New Object
        Dim myDocs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        wordFile = myDocs & "\TestTemplateFolder\TestWordTemplate.docx"

        Dim custFields As New List(Of OfficeIntegration.ColumnMapping)
        custFields.Add(New OfficeIntegration.ColumnMapping("Own", "Owner"))

        Dim doc As Object = OfficeIntegration.Word.GenerateDocument(wordFile, Me.Visit, custFields)
        Dim word1 = OfficeIntegration.Word.GetWord
        word1.visible = False
        doc.saveas(myDocs & "\TestTemplateFolder\" & "NewTemplateCreated")
        doc.close(False)
       ' ShowMessageBox("Check if Word is still running")
        word1.quit(False)

    End Sub
Jun 26, 2013 at 4:25 PM
I don't know why mine did not work but in the meantime I ended up giving up office integration pack and using interop directly (as I had other issues as well).
Jun 28, 2013 at 9:57 AM
My interop code is very similar to office integration pack however mine takes ages to run while the pack is quite quick. Any idea why?
Jun 28, 2013 at 3:30 PM
My experience is similar to yours. Don't know why such a noticeable difference in performance. It's why I'm now looking into transitioning into using Open XML to create Word documents. Office Integration Pack is very good at what it does, but its limitations become apparent when creating more complicated Word documents.
Jun 28, 2013 at 3:34 PM
I'm just using it to fill in a word Template so I managed to make it quick just using a Dictionary<String,String> and the below kind of code:
//....... code ......
dynamic wordApp = AutomationFactory.CreateObject("Word.Application");
dynamic nvDoc = wordApp.Documents.Open(templateFile, ref missing, ref missing, ref missing);

//....... code ......

nvDoc.ExportAsFixedFormat(factureFile, 17, true, 0, missing, missing, missing, missing, missing, missing, missing, false, true, missing, missing);

//....... code ......
private static void PopulateContentControls(dynamic doc, Dictionary<String, String> mappings)
{
            foreach(var sr in doc.StoryRanges)
                foreach (var cc in sr.ContentControls)
                 cc.Range.Text = mappings[cc.Title];
        }
Jul 8, 2013 at 11:01 AM
JBeit wrote:
Bummer. The following VB code I just tested successfully
    Private Sub TestForfm1236Method_Execute()
        ' Write your code here.
        Dim wordFile As Object = New Object
        Dim myDocs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        wordFile = myDocs & "\TestTemplateFolder\TestWordTemplate.docx"

        Dim custFields As New List(Of OfficeIntegration.ColumnMapping)
        custFields.Add(New OfficeIntegration.ColumnMapping("Own", "Owner"))

        Dim doc As Object = OfficeIntegration.Word.GenerateDocument(wordFile, Me.Visit, custFields)
        Dim word1 = OfficeIntegration.Word.GetWord
        word1.visible = False
        doc.saveas(myDocs & "\TestTemplateFolder\" & "NewTemplateCreated")
        doc.close(False)
       ' ShowMessageBox("Check if Word is still running")
        word1.quit(False)

    End Sub
Hi JBeit,

I just tried to use the doc.saveas method but before I even debug I get the following error:
> 'object' does not contain a definition for 'saveas' and no extension method 'saveas' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
The doc.saveas method seems like an ideal solution but clearly isn't supported. How did you manage to get it working correctly? (I assume by creating your own override/extension method?)


Thanks in advance,

Alex.
Jul 8, 2013 at 9:39 PM
Hi Alex,

No extension or override involved. I just cut and pasted that exact code into my Lightswitch vb project and it works fine. The GenerateDocument function returns a Word Document object. So I'm just calling Word's Document.SaveAs method via late binding. I'm not a C# guy and can't really help you other than to suggest that you should be using the dynamic type instead of object. There is an example of this earlier in this thread. Here's a relevant quote from Microsoft on the dynamic type:
The dynamic type enables the operations in which it occurs to bypass compile-time type checking. Instead, these operations are resolved at run time. The dynamic type simplifies access to COM APIs such as the Office Automation APIs, and also to dynamic APIs such as IronPython libraries, and to the HTML Document Object Model (DOM).
dynamic (C# Reference)
Mar 5, 2014 at 1:50 PM
Edited Mar 5, 2014 at 2:13 PM
Have any of u guys has taken an actual look at the code?
It's quite simple to tweak it... the only thing that keeps me from doing that it's any posible updates...
but seeing as the last commited patch was back on 2012 i wonder if this is still being maintained...
i'll try uploading a patch, meanwhile u can try downloading the code and patching it yourself...
the part u should be looking for it's in the word file...
the method looks like this:
  Public Function GenerateDocument(Template As String, Item As IEntityObject, ColumnMappings As List(Of ColumnMapping)) As Object
        Dim doc As Object = Nothing
        Dim wordProxy As New WordHelper()

        wordProxy.CreateWord()
        wordProxy.OpenDocument(Template)
        PopulateContentControls(ColumnMappings, Item, wordProxy)
        doc = wordProxy.Document
        wordProxy.ShowDocument() <--- this is the line displaying the word application

        GenerateDocument = doc
    End Function
from here you can:
1) comment the line
2) create a new SilentGenerateDocument function
3) change the PopulateContentControls function to be public ( u can do everything else right now )

i really don't want to change the function signature as that would break existing client code...

Which option do you like the best?