How to run Word macro in Ui path Studio

Hello Friends,

I have one macro in word, that needs to run in one of my workflow, can anyone help me to run Word macro.

Or My original issue is , I need to set default printer in windows to Microsoft Print to PDF using ui path, I am able to do this using Word Macro.

Your suggestion is needed in both.

Note : Please don’t suggest Bala Reva activities for this.

@Pramod_Mangale

You can try this in invoke code

Dim defaultPrinter As String = "Microsoft Print to PDF"
Dim printerQuery As System.Management.ManagementObjectSearcher = New System.Management.ManagementObjectSearcher("SELECT * FROM Win32_Printer")
For Each printer As System.Management.ManagementObject In printerQuery.Get()
    If printer("Name").ToString().Equals(defaultPrinter) Then
        printer.InvokeMethod("SetDefaultPrinter", Nothing)
        Exit For
    End If
Next

cheers

Getting error like, system.management.management.objectsearcher not defined

@Pramod_Mangale

Did you copy paste the code?

I see a dot after second management
cheers

See this

I have also installed system,management.automation pakage

@Pramod_Mangale

Can you show how you are using it…because I just ran and it threw no errors

image

Are you using vb.net? or c#

Can you try upgrading system package and check…and also is system present in imports?

cheers

I am using vb.net and using Invoke code activity, let me know if I need to install any specific package

@Pramod_Mangale

Can you search like this and let me know if you are able to see like this…this has to be done in imports at the bottom of your studio beside arguments

If not can you try upgrading the system packages and check again

cheers

Yes I am able to search, which one need to select from list

@Pramod_Mangale

System management only you need to select and also system alone

Try removing invoke code and add again

cheers

Okay, twist is, It is working in my local machine, but not in virtual machine

@Pramod_Mangale

Are printer drivers configured there?

any if its cloud VM then may be the way might be little different…

If you have the mord macro…you can actually convert it into a vb code by including full name spaces…and then use invoke code itself to run the macro on word

cheers

cheers

Yes printers configured there, But we are getting error on on below class, I have imported necessary namespaces, still it is not working, I have macro on word which is working fine on my VDI, but again, how we can call it using Ui path is the issue

@Pramod_Mangale

If you can share the macro I can you you an equivalent vb code which can be used in invoke code…

Or other option is call word macro from excel vba

https://answers.microsoft.com/en-us/msoffice/forum/all/how-to-run-word-macro-from-excel-vba/3c62c856-2a1b-4500-af35-8a8ff1f2fefa

cheers

In word, I am using below macro code

Sub SetPrinter()
Application.ActivePrinter = “Microsoft Print to PDF”
End Sub

If I am trying to run same code in excel macro then it is showing error over there

@Pramod_Mangale

Please use excel macro method as shown above

cheers