Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature request] Ability to close a specific open word document from R #617

Open
kendonB opened this issue Oct 1, 2024 · 1 comment
Open

Comments

@kendonB
Copy link

kendonB commented Oct 1, 2024

A common development flow for tables is to look at the final product in Word, then iterate the code. An annoying thing about this process is having to manually close the open document before saving. browseURL() can automatically open a file, but we have no way to automatically close a file. o1-preview offered this solution for Windows which may provide some inspiration:

# Replace with the name of your Word document
docName <- "YourDocumentName.docx"

# Create the PowerShell script as a string
psScript <- sprintf('
$word = [Runtime.InteropServices.Marshal]::GetActiveObject("Word.Application")
foreach ($doc in $word.Documents) {
    if ($doc.Name -eq "%s") {
        $doc.Close()
    }
}
', docName)

# Write the PowerShell script to a temporary file
psFile <- tempfile(fileext = ".ps1")
writeLines(psScript, psFile)

# Execute the PowerShell script
system2("powershell", args = c("-ExecutionPolicy", "Bypass", "-File", psFile))

# Optionally, delete the temporary PowerShell script file
file.remove(psFile)
@davidgohel
Copy link
Owner

davidgohel commented Oct 2, 2024

This would be more for package doconv

However while iterating on a document I prefer to use tempfile so that there is no clash with edited documents

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants