There is a workflow I’m working on where an application generates a word.doc from a given ID and opens up a new Word instance with sad document. Now It works using Attach Window Activity to save it as a PDF.
Is there a way to catch that Word instance and work with it within an Word Application Scope?
There are tons of documents it needs to save and i figured if it doesn’t have to click through the Word’s UI every time , insted using UIpath.Word.Activities it could be faster.
Do you know the file name and the path to the generated document? If, yes, you can just input it in the Word Application Scope activity. It will attach to the Word instance if the file is already open.
I don’t know the path to the file, thats part of the problem. I don’t even know if it is saved anywhere at that time. The program i work with has an option to generate this document, but the only way to receive it is that it open Word up, and you can save it where ever you like. That’s why I’m looking for an other way to interact with it within a Word Application Scope, but without using the path.
If you are interested, here’s C# code to get the active Word document and export it as PDF. Warning!The code will close Word without saving after the export. Don’t run the workflow if you have an important and unsaved document.
// Remember to install the Microsoft.Office.Interop.Word package.
// Attach to a running Word instance, save the active document as PDF and close Word.
// Warning: It will overwrite the PDF file if it already exists
try {
Microsoft.Office.Interop.Word.Application wordApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") as Microsoft.Office.Interop.Word.Application;
wordApp.ActiveDocument.ExportAsFixedFormat(in_PdfPath, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
//Close document
wordApp.ActiveDocument.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges,
Microsoft.Office.Interop.Word.WdOriginalFormat.wdOriginalDocumentFormat, false);
// Close application
wordApp.Quit();
out_Success = true;
}
catch (Exception e)
{
// Either no Word instance running or the export to PDF failed.
System.Console.WriteLine("Error: " + e.ToString());
out_Success = false;
}