Category Archives: Uncategorized

Managing Office Douments with PHP

I decided to write a class to manage office documents with PHP.

Word:

function write_word() {
	$word = new COM("word.application");
	$word->Visible = 0;
	$word->Documents->Add();
	$word->Selection->PageSetup->LeftMargin = '2"';
	$word->Selection->PageSetup->RightMargin = '2"';

	//Setup the font
	$word->Selection->Font->Name = 'Verdana';
	$word->Selection->Font->Size = 8;

	//Write some text
	$word->Selection->TypeText("This is a test document");
        //Insert an image
        $word->Selection->InlineShapes->addPicture("C:\\temp\\test.png");

	//Save the document as DOC file
	$word->Documents[1]->SaveAs("c:\\docs\\test1.doc");

	//quit and release COM resources
	$word->quit();
	$word->Release();
	$word = null;
}

Excel:

function write_excel() {
	$excel = new COM("excel.application");
	$excel->Visible = 0;

	//Create a new workbook
	$wkb = $excel->Workbooks->Add();
	$sheet = $wkb->Worksheets(1);

	//This code adds the text 'myvalue' on row 2, column 4
	$sheet->activate;
	$cell = $sheet->Cells(2, 4);
	$cell->Activate;
	$cell->value = 'myvalue';

	$wkb->SaveAs("C:\docs\test.xls");

	//close and free resources
	$wkb>Close(false);
	$excel->Workbooks->Close();
	$excel->Quit();
}

PowerPoint:

function write_ppt() {

	$powerpnt = new COM("powerpoint.application");

	//Creating a new presentation
	$pres = $powerpnt->Presentations->Add();

	//Adds the first slide. "12" means blank slide
	$pres->Slides->Add(1, 12);

	//Adds another slide. "10" means a slide with a clipart and text
	$pres->Slides->Add(2, 10);

	//Adds a textbox
	$pres->Slides[1]->Shapes->AddTextbox(1, 20, 50, 300, 40);

	//Save the document as PPT file
	$powerpnt->Presentations[1]->SaveAs("C:\Docs\test1.ppt");

	//free resources and quit powerpoint
	$powerpnt->quit();
}

Get Competition Keywords

One thing I found useful when searching on keywords for SEO was to Google the keywords I wanted to “own”, then grab all the top 10 results from Google. Then I would go to the site and “View Source” to see what keywords they were using and then would manually go back and review their site with other hacks to see what actually made them get in the Google Top 10. A script I wrote to simplify this is below:

<?php
require_once 'httprequest.class.php';

class harvester {

	function harvester() {
	}

	function get_site_keywords($site) {
		$req = new httprequest();
		$html = $req->get($site);
		//echo $url;
		$html = strtolower($html);
		$a = split('<meta name="keywords" content="', $html);
		$b = split('"', $a[1]);
		return $b[0];
	}
}

$harvest = new harvester();
echo $harvest->get_site_keyword("codegin.com");
?>

This class uses the httprequest class.

Adding Text To Microsoft Word with Ruby

I actually created this to automate the cut and paste of text from the web to a word document for consumption by my “plain-text-challenged” customers.

require 'win32ole'
word = WIN32OLE.new('word.application')
word.visible = true
word.documents.add
word.selection.typetext("Hello World!\n")

Getting Data From MySQL Using Ruby.

I wanted a fast way to get data from mysql using ruby. after i installed the mysql gem it was really easy. This program gets all rows, but only displays the id,name and zip.

require "mysql"

dbh = Mysql.real_connect("localhost", "root", "password", "scottwork")
res = dbh.query("SELECT name,address,city,state,zip,email FROM contacts")

while row = res.fetch_row do
    id = row[0]
    name = row[1]
    address = row[2]
    city = row[3]
    state = row[4]
    zip = row[5]
    email = row[6]
    puts id+','+name+','+zip
end

res.free

Invoking Java From Ruby

I did this using the rubygem Java-Ruby-Bridge. I did this because I was having problems with JRuby and wanted to execute my class from within Ruby.

Here is a simple way to do it, as long as you remember the classpath.

require 'rjb'
Rjb::load(classpath = '../../TestJava/bin', jvmargs=[])
test = Rjb::import('Test')
c = test.new
c.foo3

This way I was able to invoke a web camera testing program written in java with a few environment variable calls

ENV['CLASSPATH'] = '../../JavaRobots/dist/javarobots.jar;../../JavaRobotsLib/jai_core.jar;../../JavaRobotsLib/jai_codec.jar'require 'rjb'test = Rjb::import('com.scottpreston.javarobot.vision.ImageViewer')
c = test.new("c:\\temp\\fcover.jpg")
#default_close_operation = JFRAME::EXIT_ON_CLOSE
sleep 5

Opening Internet Explorer with Ruby

I have been doing a lot of web automation these days for testing and automating web based applications without APIs.

To open Internet Explorer with Ruby just use OLE.

require 'win32ole'
ie = WIN32OLE.new('InternetExplorer.Application')
ie.visible = true
ie.gohome

An Easy Windows Backup Script.

I needed a way to backup all my important files to a server in case I had a hard disk failure. To do this I did not need any special software, just a spare machine (old machines are good for this) or a NAS (Network Attached Storage) or an external hard disk.

# If Network Attached Storage / Spare PC
# use this to create connection to this machine
###
net use * /d /y
net use z: \\{Server Ip Adderess}\{share name} /user:{username] {password}# use the xcopy command
# /e - copies directories
# /d - copies new files
# /y - suppresses yes/no prompt
# /c - continues even if errors occur
###
xcopy "{Source Directory}" "z:\{Target Directory}" /e /d /y /c

That’s it. I have been using this for years and it has never failed me. So just create a file called backup.bat someplace on your hard drive, then schedule a task, and that’s it.

Getting Text and Images From The Web with Ruby

Here I am going to talk about how to get images and text from the web with Ruby.

I have found a lot of useful informaiton on the web but sometimes it’s too hard to get it without some help. To help me out I created two little snips…

To get text:

require 'uri'
url = ARGV[0]  # for command line input
#url = "http://www.codegin.com" # for direct input
puts Net::HTTP.get(URI.parse(url))

To get an image and save it to disk:

require 'net/http'
url = ARGV[0]
img = Net::HTTP.get_response(URI.parse(url))
open("temp.jpg", "wb") { |file|
    file.write(img.body)
   }
puts "done downloading image"

Here you can replace the ARGV[0] with a hard coded link to get this image and save it directly to disk.

Using Ruby to Close Outlook

First you will need to Install Ruby. Make sure the {ruby install path}/bin is in your path. Then run this script.

require 'win32ole'
outlook = WIN32OLE.new('outlook.application')
outlook.quit

I used this in conjunction with my backup script by making sure to close Outlook before I started a backup, this way I was sure to get the Outlook files (.pst) backed up with everything else.