Jump to content
Sign in to follow this  
Quinch

Creating formatted .doc files?

Recommended Posts

Quinch

I'm looking for a way to create MS Word files with specific margin and font size formatting into which I can insert text via the script, without actually opening the Word application in the process. How would I go about it - the help file seems to only contain UDFs that involve interaction with Word itself, and checking the UDF forum didn't produce much help in this particular scenario.

Share this post


Link to post
Share on other sites
water

Why do you want to do without opening Word?

Isn't Word installed on the PC you want to run the script on?

Do you want to do everything invisible to the user?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Quinch

I need it to run on a scheduler - basically I have a bunch of lists which another script processes which then in turn I want to send to the users in a printable format. Since the lists have a fixed number of lines per page, I only need to fix the margins and font size.

Share this post


Link to post
Share on other sites
water

If Word is installed I see no problem using the Word UDF.

Function _WordCreate has parameter $b_visible. If set to 0 all processing happens in the background.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Quinch

Would that work while logged off, though? I put together a simple

#include <Word.au3>
$oWordApp = _WordCreate(@ScriptDir & "Origin.doc", 0, 1)
Local $oDoc = _WordDocGetCollection($oWordApp, 0)
$oDoc.Range.insertAfter(FileRead("test.txt"))
_WordMacroRun($oWordApp, "Format")
_WordDocSaveAs($oDoc, @ScriptDir & "Test_1.doc")
_WordQuit($oWordApp)

- and while it works when run manually, when executed with the task scheduler while logged off doesn't seem to produce any results.

Share this post


Link to post
Share on other sites
water

Add some error checking and you will see where the proplem occurres.

#include <File.au3>
#include <Word.au3>
$oWordApp = _WordCreate(@ScriptDir & "\Origin.doc", 0, 1)
If @error then _FileWriteLog(@TempDir & "Example.log", "Error " & @error " & returned by _WordCreate")
Local $oDoc = _WordDocGetCollection($oWordApp, 0)
If @error then _FileWriteLog(@TempDir & "Example.log", "Error " & @error " & returned by _WordDocGetCollection")
$oDoc.Range.insertAfter(FileRead("test.txt"))
If @error then _FileWriteLog(@TempDir & "Example.log", "Error " & @error " & returned by Insert After")
_WordMacroRun($oWordApp, "Format")
If @error then _FileWriteLog(@TempDir & "Example.log", "Error " & @error " & returned by _WordMacroRun")
_WordDocSaveAs($oDoc, @ScriptDir & "\Test_1.doc")
If @error then _FileWriteLog(@TempDir & "Example.log", "Error " & @error " & returned by _WordDocSaveAs")
_WordQuit($oWordApp)
If @error then _FileWriteLog(@TempDir & "Example.log", "Error " & @error " & returned by _WordQuit")

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Quinch

No logfile {had to add a backslash there, BTW} - just an error sound and an unintelligible ~$Origin.doc temporary file, which implies that the template document is opened, but the macro isn't run. I'll start investigating the macro - in the meantime, is it possible to insert the text in a specific font and size via the script?

Share this post


Link to post
Share on other sites
water

here is an examples cript to open the document, insert the text file, set margins and font for the document.

#include <Word.au3>

$sFile = @ScriptDir & "Origin.doc"
Global $oWordApp = _WordCreate($sFile)
Global $oDoc = _WordDocGetCollection($oWordApp, 0)
$oDoc.Range.InsertAfter(FileRead("test.txt"))
; Set font for whole document
$oSelection = $oDoc.Range
$oSelection.Font.Name = "Courier New"
$oSelection.Font.Size = 12
; Set margin
With $oDoc.PageSetup
    .TopMargin = CentimetersToPoints(1.4)
    .BottomMargin = CentimetersToPoints(1.5)
    .LeftMargin = CentimetersToPoints(1.6)
    .RightMargin = CentimetersToPoints(1.7)
EndWith

Func CentimetersToPoints($i_Cent)
    Return ($i_Cent * 28.35)
EndFunc   ;==>CentimetersToPoints

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×