none
Creating/saving a hidden Document from my AddIn RRS feed

  • Question

  • Hi all;

    I have an AddIn on Word. I need to, with the user not seeing it, open a document and then do a SaveAs on it. (I'm converting it from DOCX to PDF.)

    What is the best way to do this? What I'm doing, and it seems to work is:

    			Object filename = inputFile;
    			Boolean readOnly = true;
    			Boolean visible = false;
    
    			Document doc = documents.OpenNoRepairDialog(ref filename, ref oMissing,
    														 readOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    														 ref oMissing, ref oMissing, ref oMissing, ref oMissing, visible,
    														 true, ref oMissing, ref oMissing, ref oMissing);
    			doc.Activate();
    
    			var activeWin = word.ActiveWindow;
    			activeWin.Visible = false;
    			activeWin.WindowState = WdWindowState.wdWindowStateMinimize;
    
    			WdExportOptimizeFor quality = WdExportOptimizeFor.wdExportOptimizeForPrint;
    			WdExportItem showMarkup = WdExportItem.wdExportDocumentContent;
    			Boolean includeProps = true;
    			WdExportCreateBookmarks bookmarks = WdExportCreateBookmarks.wdExportCreateHeadingBookmarks;
    			Boolean includeTags = false;
    			Boolean bitmapMissingFonts = false;
    			bool pdfa = false;
    
    			doc.ExportAsFixedFormat(outputFile, WdExportFormat.wdExportFormatPDF, false,
    									 quality, WdExportRange.wdExportAllDocument,
    									 1, 1, showMarkup, includeProps, true, bookmarks, includeTags, bitmapMissingFonts, pdfa);
    
    			doc.Saved = true;
    			normalTemplate.Saved = true;
    			object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
    			doc.Close(ref saveChanges, ref oMissing, ref oMissing);
    

    Is this the best way to do this?

    thanks - dave


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    Sunday, October 9, 2016 11:36 PM

Answers

All replies

  • Hello David,

    You can use the ScreenUpdating property of the Application class which returns true  if screen updating is turned on.

    The ScreenUpdating property controls most display changes on the monitor while a procedure is running. When screen updating is turned off, toolbars remain visible and Word still allows the procedure to display or retrieve information using status bar prompts, input boxes, dialog boxes, and message boxes. You can increase the speed of some procedures by keeping screen updating turned off. You must set the ScreenUpdating property to True when the procedure finishes or when it stops after an error. For example:

    Application.ScreenUpdating = False 
    Documents.Add 
    For x = 1 To 500 
     With ActiveDocument.Content 
     .InsertAfter "This is line " & x & "." 
     .InsertParagraphAfter 
     End With 
    If x Mod 50 = 0 Then 
     ActiveDocument.Paragraphs(x).Range.Select 
     Application.ScreenRefresh 
    End If 
    Next x 
    Application.ScreenUpdating = True

    BTW You may also consider using Open XML SDK, see Welcome to the Open XML SDK 2.5 for Office for more information.


    [custom.development]

    Monday, October 10, 2016 2:47 PM
  • Have you tried changing the Visible property on the documents.open method to False? It works for me.

    Kind Regards, Rich ... http://greatcirclelearning.com

    Monday, October 10, 2016 7:38 PM
  • Hi;

    Do you mean in the call to OpenNoRepairDialog()? I am doing that.

    thanks - dave


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    Monday, October 10, 2016 8:44 PM
  • Hi;

    The document I create never displays so I think I'm ok without turning ScreenUpdating off.

    ??? - thanks - dave


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    Monday, October 10, 2016 8:46 PM
  • My bad... I didn't look at the Boolean you set.

    So even with that set to False, the source document still displays? What version of Word? I did a test with VBA in Word 2013 and it worked fine.

    Maybe I don't understand the question.


    Kind Regards, Rich ... http://greatcirclelearning.com

    Monday, October 10, 2016 9:32 PM
  • Sorry, no. It all works fine for me. I'm just asking to see if there's anything else I should be doing.

    thanks - dave


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    • Proposed as answer by David_JunFeng Friday, October 14, 2016 9:45 AM
    • Marked as answer by David_JunFeng Monday, October 17, 2016 2:41 PM
    Monday, October 10, 2016 9:37 PM
  • Hi DavidThi808,

    I think that you need not to do anything.

    Thanks for your understanding.
    Tuesday, October 11, 2016 6:57 AM