#include #include #include #include ; Create application object Local $oRange, $oWord = _Word_Create() If @error Then Exit MsgBox($MB_SYSTEMMODAL, @ScriptLineNumber, _ "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ; ***************************************************************************** ; Add a new empty document ; ***************************************************************************** $oDoc = _Word_DocAdd($oWord) If @error Then Exit MsgBox($MB_SYSTEMMODAL, @ScriptLineNumber, "Error creating a new Word document." _ & @CRLF & "@error = " & @error & ", @extended = " & @extended) MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocAdd Example", "A new empty document has successfully been added.") ; Open the style name file for reading and store the handle to a variable. Local $hFileOpen = FileOpen("C:\Users\bob\Documents\temp\shorter-edited.txt", $FO_READ) If $hFileOpen = -1 Then MsgBox($MB_SYSTEMMODAL, "", "An error occurred when opening the file.") Return False EndIf While 1 ;read next style name Local $sFileRead = FileReadLine ($hFileOpen) If @error Then Exit MsgBox($MB_SYSTEMMODAL, @ScriptLineNumber, "Error reading file/EOF." & @CRLF & "@error = " & @error & ", @extended = " & @extended) MsgBox($MB_SYSTEMMODAL, @ScriptLineNumber, "Next line of the file:" & @CRLF & $sFileRead, 1) ; Move the range to the end of the document $oRange = _Word_DocRangeSet($oDoc, -2) If @error Then Exit MsgBox($MB_SYSTEMMODAL, @ScriptLineNumber, _ "Error setting/expanding range." & @CRLF & "@error = " & @error & ", @extended = " & @extended, 1) $oRange.Select MsgBox($MB_SYSTEMMODAL, @ScriptLineNumber, _ "Range has been successfully set to the end of the document.", 1) ; Insert style name read from the file as text $oRange.InsertAfter($sFileRead) $oRange.Select MsgBox($MB_SYSTEMMODAL, @ScriptLineNumber, "Inserted text at the end of the range.", 1) ; set range to the last paragraph (which should include the text just inserted) $oRange = _Word_DocRangeSet($oDoc, -2, $wdParagraph, -1) If @error Then Exit MsgBox($MB_SYSTEMMODAL, @ScriptLineNumber, _ "Error setting/expanding range." & @CRLF & "@error = " & @error & ", @extended = " & @extended, 1) ; set the style to the style name read from the file $oRange.Style=$sFileRead if @error then MsgBox($MB_SYSTEMMODAL, @ScriptLineNumber, "failure to set style" & @CRLF & "@error = " & @error & ", @extended = " & @extended, 3) $oRange.InsertAfter("*") EndIf MsgBox($MB_SYSTEMMODAL, @ScriptLineNumber, "Added some formatting.", 1) ; read back styles just set $oRangeChar = $oRange.CharacterStyle $oRangePara = $oRange.ParagraphStyle $oRangeStyle = $oRange.Style $oRange.InsertParagraphAfter if @error then MsgBox($MB_SYSTEMMODAL, @ScriptLineNumber, "oRange error" & @CRLF & "@error = " & @error & ", @extended = " & @extended, 3) ;add the read-back styles into the document $oRange.InsertAfter( "|" & $oRangeChar & "|" & $oRangePara & "|" & $oRangeStyle & "|") if @error then MsgBox($MB_SYSTEMMODAL, @ScriptLineNumber, "oRange error" & @CRLF & "@error = " & @error & ", @extended = " & @extended, 3) $oRange.InsertParagraphAfter if @error then MsgBox($MB_SYSTEMMODAL, @ScriptLineNumber, "oRange error" & @CRLF & "@error = " & @error & ", @extended = " & @extended, 3) MsgBox($MB_SYSTEMMODAL, @ScriptLineNumber, "Inserted a paragraph.", 1) WEnd