Adding Opend Word instance to Word Application Scope

Hi Everyone,

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.

Thanks in advance for any suggestions.

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.

Thanks for your response.

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 the generated document is not saved anywhere, then it won’t work with the Word Application Scope since it requires a file path.

Your other option is to use Invoke Code and interact with Word directly via Microsoft.Office.Interop.Word.

Thanks, I’m going to look into it.

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;
}

WordExportAsPDF.xaml (6.3 KB)

1 Like

Thank you very much, you helped me out big time!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.