<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: word code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 08 Aug 2008 17:41:55 GMT</pubDate>
    <description>DZone Snippets: word code</description>
    <item>
      <title>Groovy - Plain Text Word Wrap method</title>
      <link>http://snippets.dzone.com/posts/show/4839</link>
      <description>// Groovy Method to perform word-wrap to a specified length.&lt;br /&gt;// Returns a List of strings representing the wrapped text&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// Quick and Dirty method for plain text word-wrap to a specified width&lt;br /&gt;static class TextUtils {&lt;br /&gt;	&lt;br /&gt;	static String[] wrapntab(input, linewidth = 70, indent = 0) throws IllegalArgumentException {&lt;br /&gt;		if(input == null)&lt;br /&gt;			throw new IllegalArgumentException("Input String must be non-null")&lt;br /&gt;		if(linewidth &lt;= 1)&lt;br /&gt;			throw new IllegalArgumentException("Line Width must be greater than 1")&lt;br /&gt;		if(indent &lt;= 0)&lt;br /&gt;			throw new IllegalArgumentException("Indent must be greater than 0")&lt;br /&gt;		&lt;br /&gt;		def olines = []&lt;br /&gt;		def oline = " " * indent&lt;br /&gt;		&lt;br /&gt;		input.split(" ").each() { wrd -&gt;&lt;br /&gt;			if( (oline.size() + wrd.size()) &lt;= linewidth ) {&lt;br /&gt;				oline &lt;&lt;= wrd &lt;&lt;= " "&lt;br /&gt;			}else{&lt;br /&gt;				olines += oline&lt;br /&gt;				oline = " " * indent&lt;br /&gt;			}&lt;br /&gt;		}&lt;br /&gt;		olines += oline&lt;br /&gt;		return olines&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// TEST&lt;br /&gt;// the input String&lt;br /&gt;input = "Note From SUPPLIER: Booking confirmed by fax.  4 standard rooms - 3 twin shared, 1 single room, please advise if guests require meals.. "&lt;br /&gt;&lt;br /&gt;// call static wrapntab method to break the input string into 70 char wide lines with a 4 char initial indent&lt;br /&gt;olines = TextUtils.wrapntab(input,70,4)&lt;br /&gt;&lt;br /&gt;// print the output&lt;br /&gt;olines.each() {&lt;br /&gt;	println it&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 04 Dec 2007 00:43:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4839</guid>
      <author>dwp (dave pugh)</author>
    </item>
    <item>
      <title>Find &amp; Replace in Word Document with Ruby</title>
      <link>http://snippets.dzone.com/posts/show/4747</link>
      <description>I use this to open a "template" (really just a plain Word document with [text to replace] inside), do the substitutions, and save as a new filename.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'win32ole'&lt;br /&gt;&lt;br /&gt;word = WIN32OLE.new('Word.Application')&lt;br /&gt;#word.Visible = true # uncomment if you want to see it happen&lt;br /&gt;doc = word.Documents.Open('c:\file_to_open.doc')&lt;br /&gt;{&lt;br /&gt;  'name' =&gt; 'Tim Morgan',&lt;br /&gt;  'date' =&gt; Date.today.strftime('%B %d, %Y'),&lt;br /&gt;  ...&lt;br /&gt;}.each do |key, value|&lt;br /&gt;  word.Selection.HomeKey(unit=6) # start at beginning&lt;br /&gt;  find = word.Selection.Find&lt;br /&gt;  find.Text = "[#{key}]" # text must be in square brackets&lt;br /&gt;  while word.Selection.Find.Execute&lt;br /&gt;    word.Selection.TypeText(text=value)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;doc.SaveAs('c:\output_file.doc')&lt;br /&gt;doc.Close&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 12 Nov 2007 14:48:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4747</guid>
      <author>timmorgan (Tim Morgan)</author>
    </item>
    <item>
      <title>Elegant way of shorten a text string</title>
      <link>http://snippets.dzone.com/posts/show/4578</link>
      <description>this method shortens a plain text string down to complete words contained in given scope (count)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def shorten (string, count = 30)&lt;br /&gt;	if string.length &gt;= count &lt;br /&gt;		shortened = string[0, count]&lt;br /&gt;		splitted = shortened.split(/\s/)&lt;br /&gt;		words = splitted.length&lt;br /&gt;		splitted[0, words-1].join(" ") + ' ...'&lt;br /&gt;	else &lt;br /&gt;		string&lt;br /&gt;	end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 27 Sep 2007 11:47:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4578</guid>
      <author>labuschin (Martin Labuschin)</author>
    </item>
    <item>
      <title>Ruby dictionary username generation</title>
      <link>http://snippets.dzone.com/posts/show/4536</link>
      <description>Generate a new random name from dictionary words.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;DICT_PATH = '/usr/share/dict/words'&lt;br /&gt;DICT_SIZE = 234936&lt;br /&gt;&lt;br /&gt;def self.generated_name words = 2, length = 23&lt;br /&gt;  name = 'a'*(length+1)&lt;br /&gt;  while name.length &gt; length&lt;br /&gt;    name = (1..words).map{%x[sed -n '#{rand(DICT_SIZE)} {p;q;}' '#{DICT_PATH}'].chomp.capitalize}.join&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 13 Sep 2007 13:57:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4536</guid>
      <author>elliottcable (elliott cable)</author>
    </item>
    <item>
      <title>Javascript: convert integer to word (e.g. 18 -&gt; "eighteen")</title>
      <link>http://snippets.dzone.com/posts/show/3710</link>
      <description>// Convert a positive integer less than 1000 to its word representation.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;var units = new Array ("Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen");&lt;br /&gt;var tens = new Array ("Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety");&lt;br /&gt;&lt;br /&gt;function num(it) {&lt;br /&gt;	var theword = "";&lt;br /&gt;	var started;&lt;br /&gt;	if (it&gt;999) return "Lots";&lt;br /&gt;	if (it==0) return units[0];&lt;br /&gt;	for (var i = 9; i &gt;= 1; i--){&lt;br /&gt;		if (it&gt;=i*100) {&lt;br /&gt;			theword += units[i];&lt;br /&gt;			started = 1;&lt;br /&gt;			theword += " hundred";&lt;br /&gt;			if (it!=i*100) theword += " and ";&lt;br /&gt;			it -= i*100;&lt;br /&gt;			i=0;&lt;br /&gt;		}&lt;br /&gt;	};&lt;br /&gt;	&lt;br /&gt;	for (var i = 9; i &gt;= 2; i--){&lt;br /&gt;		if (it&gt;=i*10) {&lt;br /&gt;			theword += (started?tens[i-2].toLowerCase():tens[i-2]);&lt;br /&gt;			started = 1;&lt;br /&gt;			if (it!=i*10) theword += "-";&lt;br /&gt;			it -= i*10;&lt;br /&gt;			i=0&lt;br /&gt;		}&lt;br /&gt;	};&lt;br /&gt;	&lt;br /&gt;	for (var i=1; i &lt; 20; i++) {&lt;br /&gt;		if (it==i) {&lt;br /&gt;			theword += (started?units[i].toLowerCase():units[i]);&lt;br /&gt;		}&lt;br /&gt;	};&lt;br /&gt;	return theword;&lt;br /&gt;}&lt;/code&gt;</description>
      <pubDate>Tue, 20 Mar 2007 22:15:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3710</guid>
      <author>maximile (Max Williams)</author>
    </item>
    <item>
      <title>VBA DDE WORD EXCEL</title>
      <link>http://snippets.dzone.com/posts/show/3112</link>
      <description>// This is just sample code of some dde commands&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Sub RUNEXCELMACRO()&lt;br /&gt;'RUN MACRO&lt;br /&gt;aChan = DDEInitiate(App:="Excel", Topic:="System")&lt;br /&gt;DDEExecute Channel:=aChan, Command:="[Run(" &amp; Chr(34) &amp; _&lt;br /&gt;    "Personal.xls!Macro1" &amp; Chr(34) &amp; ")]"&lt;br /&gt;DDETerminate Channel:=aChan&lt;br /&gt;'POKE&lt;br /&gt;Chan = DDEInitiate(App:="Excel", Topic:="System")&lt;br /&gt;DDEExecute Channel:=Chan, Command:="[OPEN(" &amp; Chr(34) _&lt;br /&gt;    &amp; "C:\Sales.xls" &amp; Chr(34) &amp; ")]"&lt;br /&gt;DDETerminate Channel:=Chan&lt;br /&gt;Chan = DDEInitiate(App:="Excel", Topic:="Sales.xls")&lt;br /&gt;DDEPoke Channel:=Chan, Item:="R1C1", Data:="1996 Sales"&lt;br /&gt;DDETerminate Channel:=Chan&lt;br /&gt;'This example opens the Microsoft Excel workbook Book1.xls and retrieves the contents of cell R1C1.&lt;br /&gt;Chan = DDEInitiate(App:="Excel", Topic:="System")&lt;br /&gt;DDEExecute Channel:=Chan, Command:="[OPEN(" &amp; Chr(34) _&lt;br /&gt;    &amp; "C:\My Documents\Book1.xls" &amp; Chr(34) &amp; ")]"&lt;br /&gt;DDETerminate Channel:=Chan&lt;br /&gt;Chan = DDEInitiate(App:="Excel", Topic:="C:\DATA\SBS.xls")&lt;br /&gt;msg = DDERequest(Channel:=Chan, Item:="R2C1")&lt;br /&gt;msg = msg &amp; " " &amp; DDERequest(Channel:=Chan, Item:="R2C2")&lt;br /&gt;MsgBox msg&lt;br /&gt;DDETerminateAll&lt;br /&gt;'This example opens a channel to the System topic in Microsoft Excel and then uses the Topics item to return a list of available topics. The example inserts the topic list, which includes all open workbooks, after the selection.&lt;br /&gt;aChan = DDEInitiate(App:="Excel", Topic:="System")&lt;br /&gt;TOPICLIST = DDERequest(Channel:=aChan, Item:="Topics")&lt;br /&gt;Selection.InsertAfter TOPICLIST&lt;br /&gt;DDETerminate Channel:=aChan&lt;br /&gt;&lt;br /&gt;End Sub&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 08 Dec 2006 22:50:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3112</guid>
      <author>millerjohneric (John Miller)</author>
    </item>
    <item>
      <title>ALL-WORDS? - returns true if all values in a block are word! values.</title>
      <link>http://snippets.dzone.com/posts/show/3004</link>
      <description>&lt;code&gt;&lt;br /&gt;all-words?: func [&lt;br /&gt;	"Returns true if all values in a block are word! values."&lt;br /&gt;	block [block!]&lt;br /&gt;] [&lt;br /&gt;	parse block [some word!]&lt;br /&gt;]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 16 Nov 2006 05:06:40 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3004</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
    <item>
      <title>words-like - Returns a block of words in the object that match the given pattern.</title>
      <link>http://snippets.dzone.com/posts/show/2752</link>
      <description>&lt;code&gt;&lt;br /&gt;words-like: func [&lt;br /&gt;    "Returns a block of words in the object that match the given pattern."&lt;br /&gt;    object  [object!]&lt;br /&gt;    pattern [word! any-string!]&lt;br /&gt;    ;??? Add an /unbound refinement&lt;br /&gt;][&lt;br /&gt;    pattern: join form pattern "*"&lt;br /&gt;    collect w [&lt;br /&gt;        foreach word next first object [&lt;br /&gt;            if find/match/any form word pattern [w: bind word in object 'self]&lt;br /&gt;        ]&lt;br /&gt;    ]&lt;br /&gt;]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 01 Oct 2006 22:25:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2752</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
    <item>
      <title>Script Word from Python</title>
      <link>http://snippets.dzone.com/posts/show/2037</link>
      <description>A quick and dirty class for working with Word via COM in Python. By far, not all of the power of Word is available here, but it's a good start for simple tasks.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from win32com.client import constants, Dispatch&lt;br /&gt;&lt;br /&gt;import pythoncom&lt;br /&gt;&lt;br /&gt;wdStory = 6&lt;br /&gt;&lt;br /&gt;class WordDocument(object):&lt;br /&gt;  """&lt;br /&gt;  Some convenience methods for Word documents accessed&lt;br /&gt;  through COM.&lt;br /&gt;  """&lt;br /&gt;  &lt;br /&gt;  def __init__(self, visible=False):&lt;br /&gt;    self.app = Dispatch("Word.Application")&lt;br /&gt;    self.app.Visible = visible&lt;br /&gt;  &lt;br /&gt;  def new(self, filename=None):&lt;br /&gt;    """&lt;br /&gt;    Create a new Word document. If 'filename' specified,&lt;br /&gt;    use the file as a template.&lt;br /&gt;    """&lt;br /&gt;    self.app.Documents.Add(filename)&lt;br /&gt;  &lt;br /&gt;  def open(self, filename):&lt;br /&gt;    """&lt;br /&gt;    Open an existing Word document for editing.&lt;br /&gt;    """&lt;br /&gt;    self.app.Documents.Open(filename)&lt;br /&gt;  &lt;br /&gt;  def save(self, filename=None):&lt;br /&gt;    """&lt;br /&gt;    Save the active document. If 'filename' is given,&lt;br /&gt;    do a Save As.&lt;br /&gt;    """&lt;br /&gt;    if filename:&lt;br /&gt;      self.app.ActiveDocument.SaveAs(filename)&lt;br /&gt;    else:&lt;br /&gt;      self.app.ActiveDocument.Save()&lt;br /&gt;  &lt;br /&gt;  def save_as(self, filename):&lt;br /&gt;    return self.save(filename)&lt;br /&gt;  &lt;br /&gt;  def print_out(self):&lt;br /&gt;    """&lt;br /&gt;    Print the active document.&lt;br /&gt;    """&lt;br /&gt;    self.app.Application.PrintOut()&lt;br /&gt;  &lt;br /&gt;  def close(self):&lt;br /&gt;    """&lt;br /&gt;    Close the active document.&lt;br /&gt;    """&lt;br /&gt;    self.app.ActiveDocument.Close()&lt;br /&gt;  &lt;br /&gt;  def quit(self):&lt;br /&gt;    """&lt;br /&gt;    Quit Word.&lt;br /&gt;    """&lt;br /&gt;    return self.app.Quit()&lt;br /&gt;  &lt;br /&gt;  def find_and_replace(self, find_str, replace_str):&lt;br /&gt;    """&lt;br /&gt;    Find all occurances of 'find_str' and replace with 'replace_str'&lt;br /&gt;    in the active document.&lt;br /&gt;    """&lt;br /&gt;    self.app.Selection.HomeKey(Unit=wdStory)&lt;br /&gt;    find = self.app.Selection.Find&lt;br /&gt;    find.Text = find_str&lt;br /&gt;    while self.app.Selection.Find.Execute():&lt;br /&gt;      self.app.Selection.TypeText(Text=replace_str)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 16 May 2006 00:10:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2037</guid>
      <author>timmorgan (Tim Morgan)</author>
    </item>
    <item>
      <title>Find datatype words</title>
      <link>http://snippets.dzone.com/posts/show/1190</link>
      <description>&lt;code&gt;&lt;br /&gt;datatypes: has [types attrs value word] [&lt;br /&gt;	word: 'datatype!&lt;br /&gt;	types: copy []&lt;br /&gt;	attrs: second system/words&lt;br /&gt;	foreach item first system/words [&lt;br /&gt;		if all [&lt;br /&gt;			not unset? first attrs&lt;br /&gt;			any [&lt;br /&gt;				all [string? :word find value word]&lt;br /&gt;				all [&lt;br /&gt;					not string? :word&lt;br /&gt;					datatype? get :word&lt;br /&gt;					(get :word) = type? first attrs&lt;br /&gt;				]&lt;br /&gt;			]&lt;br /&gt;		] [&lt;br /&gt;			append types item&lt;br /&gt;		]&lt;br /&gt;		attrs: next attrs&lt;br /&gt;	]&lt;br /&gt;	sort types&lt;br /&gt;]&lt;br /&gt;print mold datatypes&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 19 Jan 2006 03:00:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1190</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
  </channel>
</rss>
