ardata-fr / doconv

Other
13 stars 1 forks source link

detect when running scripts is disabled on Windows #2

Open gfspiteri opened 1 year ago

gfspiteri commented 1 year ago

I am trying to used the to_pdf() function to convert a word document to pdf. I kept getting an error with my docx file so tested with the examples from the documentation:

docx_file <- system.file(package = "doconv", "doc-examples/example.docx") to_pdf(docx_file, output = "docx_example.pdf") Error: could not convert C:/Users/gspiteri/AppData/Local/R/win-library/4.2/doconv/doc-examples/example.docx

doconv::msoffice_available() returns TRUE

I am using this version of Word: Microsoft® Word for Microsoft 365 MSO (Version 2210 Build 16.0.15726.20188) 64-bit

Appreciate any help. Thanks and Merry Christmas!

davidgohel commented 1 year ago

Hello

Thanks and happy new year :)

Sorry, I only have one clue for now:

I remember a colleague had to switch on a permission to execute PowerShell. On Windows, a PowerShell script is launched and it uses Component Object Model from Microsoft.

Could you check your permissions if any relative to that?

davidgohel commented 1 year ago

when you "thumb up", does that mean this was the problem and it worked or do that mean "I'll check"? :)

gfspiteri commented 1 year ago

Means I'll check!

Do you have some a sample PowerShell script I could test?

davidgohel commented 1 year ago

here is a sample https://powershellbyexample.dev/post/hello-world/

gfspiteri commented 1 year ago

Oh yes, sorry wasn't clear. I can use PowerShell and the examples you linked work (I tested a few different commands). I meant, do you have a PowerShell script using the Component Object Model I could test.

davidgohel commented 1 year ago

(Not tested as I don't have Windows for now), change $Filename to an existing name, it should be opened by the script

$Filename='C:\anypath\doc.docx'
$Word=NEW-Object –comobject Word.Application
$Document=$Word.documents.open($Filename)
gfspiteri commented 1 year ago

Yes this works.

davidgohel commented 1 year ago

ok, sorry, I have no more clue for now

gfspiteri commented 1 year ago

I will try to look at the code later this week and see if I figure out where the problem is.

gfspiteri commented 1 year ago

It was a problem with permissions. Running this adapted from docx2pdf_win(): system2("powershell", args = c("-file", script_path)) gave `File C:\Users\xxxx\AppData\Local\Temp\RtmpEHDrN9\file718416e4da3.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.

davidgohel commented 1 year ago

Haaaa! perfect, thanks for the feedback

I'll try to add a function to test this case

FGL-Linear commented 1 year ago

I had the same problem. My execution policy was set to "RemoteSigned". To try it out, I set it to "Unrestricted" and it worked.

I'm not well versed in PowerShell or IT security, so I'd like to ask if what I did is too risky and if there's a better way to enable the scripts used by doconv while keeping "RemoteSigned" policy.

davidgohel commented 1 year ago

Thanks for your comment

I would say, if you use the CRAN version, the risk of getting a hacked version is low, specially if you are in a managed and administrated environment. But if you switch that thing off, then you're exposed to other vulnerabilities. Your IT may recommend you not to do so if you ask them. I don't think my script is dangerous but some others may be.

I use the product to help me with doc and visual testing. I am not really able to advise on this subject.

FGL-Linear commented 1 year ago

Thank you very much for your quick reply. I'll ask IT then. I guess I could keep that policy as it was and switch it only when I need to run doconv.

Anyways, thank you for this and your other packages. They are really helpful!