Jump to content

Creating formatted .doc files?


Recommended Posts

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.

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...