What is the correct syntax for save as active document in Word using Applescript? - ms-word

I am going nuts here. I have tried countless permutations/variations of "save as active document file format PDF" but none seem to work. I get AppleScript errors with all of them.
So can anyone tell me:
What is the exact syntax to save the active document as a PDF file in AppleScript, using Word?
It seems that there is no coherence whatsoever in the Office for Mac scripting, as I have this working for Excel and PowerPoint and even there the syntax is different:
excel
save active workbook in 'MyFile.pdf' as PDF file format
PowerPoint
save active presentation in 'MyFile.pdf' as save as PDF
What is the correct syntax for Word?
Thanks!

It seems I found it after all:
set myDoc to "/Users/X/Desktop/test.docx"
set pdfSavePath to "Users:X:Desktop:test.pdf"
tell application "Microsoft Word"
activate
open myDoc
set theActiveDoc to the active document
save as theActiveDoc file format format PDF file name pdfSavePath
end tell
I am no AppleScript expert. I had slashes instead of : as path separators. With : it works

Joris Mans script is pretty nice, but has a flaw. It does not ensure that Word has an active document ready (set theActiveDoc to the active document might return missing value)
I also improved the script to use the Finder selection as input, placing the PDFs into the same place as the word files. I did not test the file types, but word will complain about that.
tell application "Finder"
set input to selection
end tell
tell application id "com.microsoft.Word"
activate
repeat with aFile in input
open aFile
set theOutputPath to ((aFile as text) & ".pdf")
repeat while not (active document is not missing value)
delay 0.5
end repeat
set activeDoc to active document
save as activeDoc file name theOutputPath file format format PDF
close active document saving no
end repeat
end tell

Related

How to pass argument for the apple script

I have following script :)
property word_docs : {"org.openxmlformats.wordprocessingml.document", "com.microsoft.word.doc"}
property default_path : (path to desktop) as alias
property Delim : {".docx", ".doc"}
property PDF : ".pdf"
set outPDF to {}
set selected_files to (choose file of type word_docs default location default_path with multiple selections allowed without invisibles and showing package contents)
set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, Delim}
repeat with afile in selected_files
copy item 1 of text items of (afile as text) & PDF to the end of outPDF
end repeat
set AppleScript's text item delimiters to TID
tell application id "com.microsoft.Word"
activate
repeat with i from 1 to count of selected_files
open (item i of selected_files)
set theOutputPath to (item 1 of outPDF)
-- close access (open for access exportDocument)
tell active document
save as it file name theOutputPath file format format PDF
close saving no
end tell
end repeat
end tell
return
That helps me convert doc & docx files -> pdf, but it is too interactive.
And I have idea to run this script via terminal and i want to pass file path or directory as argument
for example:
$ script /Users/test/dest_dir/ /Users/test/out_dir/
will produce all pdf files into out_dir.
I saw this library also but it converts only docx files:
https://pypi.org/project/docx2pdf/
Is there anyone here who can help me rewrite this script .. I don't understand this language at all. or maybe someone will point to the finished tool. I need to do this on the mac os operating system.
see http://hints.macworld.com/article.php?story=20050523140439734
argv is an array of arguments in string format
on run argv
return item 1 of argv

How to add . to a name of a bookmark in Word

So i am using Word to create a template for the generation of a .pdf using XDocReport in Java. The problem i have encountered is that when i add a bookmark to an image in Word the allowed names cannot contain "." , for example i cannot name the bookmark estimate2.Photo , which is the name needed to correctly map the variable to the virtual object being passed. My question is the following, is there any way i can alter this default Word behaviour in order for it to allow me to add bookmarks that contain special characters like "." ?
I have found a way to rewire this default behaviour by modifying the configuration .xml files. In order to access the bookmarks file follow these steps:
Go to the .docx file right-click on it go to 7zip and open as archive.
Access the word folder and there you will find the document.xml file.
extract it
Open with notepad or any other text editor, find the bookmark start xml tag which contains the name property and modify it.
Once the file has been modified drag it back inside the archive and save.
Wow. I wouldn't have thought this would work... Nice find #Patratel
not withstanding the fact that it seems to work, I wouldn't recommend doing this for anything other than a temporary file or curiosity...
That said, here's the steps to do / test this:
The manual approach:
Add a regular bookmark (shortcut: Alt > I > K)
give it a name like dot_dot
save the file
change the extension from .docx to .zip
open the .zip folder
save document.xml to the desktop (or somewhere)
Find the text dot_dot and replace with dot.dot
save .xml file
copy .xml file back to .zip folder
rename .zip back to .docx
Open in MS Word
open bookmarks dialog (Alt > I > K)
select dot.dot from list
press Goto
A quick test of functionality
From there it is easy enough to test whether or not the bookmark can be used as normal... To that end I added a new paragraph and inserted a cross reference back to the bookmarked text. The cross reference worked.
The .InsertXML approach
Next I was also curious about #Cindy's comment about Range insert XML... It worked, to test this I used the following code in the immediate window:
' replace text in document with the equivalent XML (generates a few pages)
selection.Range.Text = thisdocument.Range.WordOpenXML
' replace the XML with the result of inserting itself into the document
thisdocument.Range.InsertXML thisdocument.Range.Text
An Automatic Approach
Sub dottyBM()
Dim newDoc As Word.Document
Set newDoc = Application.Documents.Add
newDoc.Range.Text = "Testing a dot bookmark"
Dim bmRange As Word.Range
Set bmRange = newDoc.Paragraphs(1).Range
bmRange.Start = bmRange.Start + InStr(bmRange.Text, " dot")
bmRange.End = bmRange.Start + 3
' bmRange.Bookmarks.Add "dot.dot" ' Err: 5828, Bad bookmark name
bmRange.Bookmarks.Add "dot_dot"
bmRange.InsertXML Replace$(bmRange.WordOpenXML, "dot_dot", "dot.dot")
Application.Dialogs(168).Show
newDoc.Close False
End Sub

Rename file with the information available in it

Issue - I have this large batch of pdf, word & notepad files which are required to be renamed according to the text available in each file.
For eg: I have to open file A, search the entire document for the string which will be used as its name, copy, close the file and rename the file.
After searching for some while I found that autohotkey can help me with this though I'm open to every solution. What I want to do is open the file, search for the name, select the text and use the keystroke to close and rename the file.
Could anyone help me with this?
I'm not going to give you a script but what you want can be done.
You need to collect various pieces of information when you rename a file. One of them being the current file name which you can often grab from the window title. You can use https://autohotkey.com/docs/commands/WinGetTitle.htm
If a filename is wrapped in [ ] you can remove those using https://autohotkey.com/docs/commands/StringReplace.htm
If you know the file path (folder where the file resides in) you can now use https://autohotkey.com/docs/commands/FileMove.htm to rename a file and use https://autohotkey.com/docs/misc/Clipboard.htm as a variable.
You would probably like to retain the extension (.doc, .txt, .pdf) so you can use https://autohotkey.com/docs/commands/SplitPath.htm to grab it and append that to the clipboard when you rename it.
Closing a file depends on your program, sometimes you can send ctrl+w or ctrl+w or you need to use the WinClose command so you need to add in that step before you use FileMove.

Automate a Grep Applescript to Word Document

I'm using a Mac and I'm preparing accounts for a company. Every payslip which I've made in Microsoft Word has a voucher number. Because a transaction was missed all voucher numbers are wrong so now there are hundreds of wrong payslips. I want to create a script that can find the following GREP (find beginning of paragraph, text:Vch, any character until \r):
^Vch.+\r
and replace it with nothing (thereby deleting the whole sentence).
I was thinking of using Applescript as it can open the document, perform the GREP find (tricky part), save the document and save it as a pdf (all which is needed).
But apparently my knowledge fails me. Commands from the dictionary like create range, execute find, all bring errors.
Somebody experienced in Applescript that could help me devise a script? Any suggestions? It should be something like:
Tell application "Microsoft Word"
tell active document
set myRange to create range start 0 end 0
tell myRange
execute find find "^Vch.+\r" replace with ""
end tell
end tell
end tell
Many thanks!
There are no special characters to indicate the beginning of a line.
To search at beginning of the paragraph, the script must use return & "some text"
You can use "^p" as paragraph mark, but it doesn't work when you set the match wildcards property to true
To match an entire paragraph, the script must use return & "some text" & return, and the script must use replace with return to delete one paragraph mark instead of two.
Because the first paragraph does not begin with a paragraph mark, the script must use two execute find commands.
The wildcard is *
tell application "Microsoft Word" -- (tested on version 15.25, Microsoft Office 2016)
-- check the first paragraph
select (characters of paragraph 1 of active document)
execute find (find object of selection) find text ("Vch*" & return) replace with "" replace replace one wrap find find stop with match wildcards and match case without match forward and find format
--to search forward toward the end of the document.
execute find (find object of selection) find text (return & "Vch*" & return) replace with return replace replace all wrap find find continue with match wildcards, match case and match forward without find format
save active document
-- export to PDF in the same directory as the active document
set pdfPath to path of active document & ":" & (get name of active window) & ".pdf"
set pdfPath to my createFile(pdfPath) -- create an empty file if not exists, the handler return a path of type alias (to avoid grant access issue)
save as active document file name pdfPath file format format PDF
end tell
on createFile(f)
do shell script "touch " & quoted form of POSIX path of f
return f as alias
end createFile

Preserve Word list numbering when importing file into DOORS

I am importing a requirements document in created in Word into a DOORS module. I also want to be able to export the DOORS module back into a Word document and have them look mostly-similar. What is the easiest way to do this?
There are two options to do this that I can see:
In Word, use the DOORS add-in to "Export to DOORS"
In DOORS, use "Import -> Rich Text Format" (on the Word file saved as a RTF)
In Option 1, the numbering for the items in a list are not preserved. In Option 2, the line numbering is preserved, but upon exporting back to Word, the list Word styles are all "normal" and I lose all the formatting and indenting.
In Option 1, I can take advantage of the "Paragraph Style" attribute that is populated when the initial export is performed to make the resulting Word document look like the original. In Option 2, I do not see this option/attribute available.
There does not appear to be a way in DOORS/Word to handle this natively. The best solution I could find is to first convert the Word auto-numbering to actual text using the VBA method ConvertNumbersToText(). Doing this allows me to keep the formatting/indentation in DOORS and also have the ability to export the database back to Word and have it look the same/similar.
The only minor issue is that the resulting document no longer uses the Word auto-numbering. This is not an issue for my purposes.