Thursday, February 18, 2010

Using Excel VBA to Open and Autofill Web Pages

This post is more about manipulating web pages than creating them.

My employer had a 2007 excel spreadsheet that they used to track several hundred projects. They would open a browser then copy/paste the information into a modified Google map. My boss asked me if there was a way I could automate this process.

I found parts to the solution here and there. I hope this will help someone else. It was a fun challenge and led to other similiar projects. Please remember, I am not a professional programmer so feel free to add better suggestions to comments.

Since the actual project cannot be duplicated here let’s just say we have a spreadsheet with a customer’s Name, Address, City, State and Zip. We want to send that information to Google and see the map.

Click here to download the example created in 2007 and saved as xls: http://www.webdebris.com/test/GetMapExample.zip

First thing I will need is a button. Switching over to the developer tab I add an active X button and call it GetMap. Adding it above the header, which can be froze, will let the button be available always.

Next I need to add a reference to Microsoft’s web browser to the excel spreadsheet. In the visual basic editor, go to Tools References and add Microsoft Internet Controls.

Now it is time to start coding.
Option Explicit

Public ie As Object

Public strAddress As String 'Builds the address to send to Google

'The following line will let the code sleep while waiting for the page to load
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
'When working in excel the browser will stay in the background. This will bring the browser to the top
Private Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long

Private Sub GetMap_Click()

'There is a bug/feature with Excel that when the IE window closes the ie object still has a reference.
'Normally you would check for the IE window to close and then set the reference to nothing.
'Web Control does not have an on_quit event. So you have to trap by error.
On Error GoTo ErrHandle:

Dim strCursorLocation As String 'The location of the cursor when the button was clicked
Dim strRow As String 'Will hold the row number
Dim i As Integer

strCursorLocation = ActiveCell.Address(False, False) 'Get the current cursor location
strRow = ""

'Remove letters from strCursorLocation
For i = 1 To Len(strCursorLocation)

If IsNumeric(Mid(strCursorLocation, i, 1)) Then

strRow = strRow & Mid(strCursorLocation, i, 1)

End If

Next i

'Build the address by adding columns B thru E
strAddress = Range("B" + strRow).Value + " "
strAddress = strAddress + Range("C" + strRow).Value + " "
strAddress = strAddress + Range("D" + strRow).Value + " "
strAddress = strAddress + Range("E" + strRow).Value + " "

'Check to see if an IE window has been opened
If ie Is Nothing Then

Call OpenGoogle

End If

'Bring IE to top. Does not bring to top 100 percent of the time.
If Not ie Is Nothing Then

BringWindowToTop (ie.hwnd)

End If

Call EnterAddress

Exit Sub

ErrHandle:

'Reopen IE window if it was closed
If Err.Number = -2147417848 Or Err.Number = 462 Then

Call OpenGoogle
Resume

Else

'Something else has happened
MsgBox "Error: " + CStr(Err.Number) + " " + Err.Description, vbCritical

End If

End Sub

Private Sub OpenGoogle()

'Create IE Window
Set ie = CreateObject("Internetexplorer.Application")
'Show IE Window
ie.Visible = True
'Navigate to the google map page
ie.Navigate "http://maps.google.com/maps?hl=en&tab=wl"

'Wait for the page to load
Do While ie.Busy = True

DoEvents
Sleep 500

Loop

End Sub

Private Sub EnterAddress()

'Check to see if map page was navigated away from
If ie.document.URL <> "http://maps.google.com/maps?hl=en&tab=wl" Then

'Reload map page
ie.Navigate "http://maps.google.com/maps?hl=en&tab=wl"

'Wait for the page to load
Do While ie.Busy = True

DoEvents
Sleep 500

Loop

End If

With ie

.document.all("q_d").Value = strAddress 'enter the address
.document.all("q-sub").Click 'click the search button

End With

End Sub


Wednesday, February 17, 2010

Liquid, left, or middle? What to do, what to do.

I used to be all about liquid layouts. But these days there are more and more people using wide screen monitors. The liquid layout design can turn ugly quick on one of these displays. If you have a lot of information on web page it seems to work okay. For me, more often then not the amount of information on a page will vary greatly.

When I see sites built on the left through a wide screen it all seems out of balance. I think the key to left built sites could be a background graphic that would balance it out.

On the other hand, I am now seeing web sites that have been developed on wide screens. When I display them on my older CRT monitor the columns are really scrunched up and hard to read. When I move the page to my wide screen it looks fine.

So now I am becoming a fan of centered pages. All the sites I have done recently are centered and usually have a width of 1000 pixels. I spent some time on http://www.statowl.com/ and looked through the information they have. Looks like 800 x 600 display modes and less are almost gone. 1024 x 768 and greater are the norm.

Even though I like strict, clean, and bare bones code I do not like to sacrifice design because of a few people using older browsers or displays. (Hmmm... wonder if the people developing sites on wide screens feel the same way about non wide screen monitors.)

Just curious as to other viewpoints.

Monday, December 28, 2009

IE5.5, IE6, IE7, IE8 CSS Compatibility Check

Checking the CSS compatibility between browsers is a hassle. Especially when you have to check for different flavors of the same browser. I was using VPC images to test various IE versions which is a real pain. I ran across this while trying to find a website that did compatibility checking.

IETester. This is an alpha release of a browser that can test your CSS in IE5.5 up to IE8. It was pretty accurate too. I did have a couple times that the browser tabs crashed. I am not sure what caused it but that was a minimal issue compared to trying to test with VPC images.

Check it out here: http://www.my-debugbar.com/wiki/IETester/HomePage

Wednesday, December 16, 2009

"Q" and "A" Formatting with CSS

When I was coding my Q and A page, I had started using an unordered list to organize the question and answer detail. At first I wanted to have the letters Q and A to signify the difference between the lines. I also thought different text styles and indentation would help distinguish the difference too.

It looked like I was going to have to use two classes to define the CSS presentation side and alternate those throughout the unordered list. Nothing wrong with it, but I felt like it was starting to make the code look messy.

After a bit of searching in Google I found several people talking about using this little gem, Definition Lists.

Definition Lists consist of three elements:
  • DL - A container element for the DT and DD elements.
  • DT - Element for the term being defined
  • DD - Element for the definition of the term.

This is perfect. I have a container to surround each Q and A segment, and I have built in elements to separate the CSS presentation for each question and answer.


You can go here to see how I used this method.

http://www.webdebris.com/qanda.htm

Saturday, December 12, 2009

CSS: The Missing Manual

I would say that 75% of the time I have bought a technical book it has turned out to be a disappointment. They are either to basic or way to advanced. So I was really dreading spending money on a CSS book.

I knew the basics of CSS but needed to learn the next step of what to do with it, and personally I hate reading digitized text. I prefer to have physical media in hand. After taking a look at what Amazon had to offer I bought CSS: The Missing Manual by David Sawyer. It had a 4.5 star rating so I took a chance.

What a pleasant surprise this book turned out to be. It had the basics, but the book did an excellent job of teaching what you can do with CSS. There were a lot of exercises that you can see on the fly how your CSS code affected the page.

If I had a complaint, it would be the lack of a complete CSS command reference with a brief explanation of what they do. Still have to rely on The Ultimate CSS Reference for that one.

If you are learning CSS I would suggest taking a look.

Thursday, December 10, 2009

Flash and W3C Validation without JavaScript

For those of you who are familiar with my site know that I am trying to create it using only strict XHMTL and CSS. This presented a bit of a problem when I wanted to use one of my Flash projects for the holidays.


I scoured through several articles on the Internet but I could not find anything that fit exactly what I wanted. SATAY was a good approach but to be honest my Flash creations are not designed properly, and some of them I do not have the original code to fix them the way they should be. Other approaches did not allow for an alternate image for all browsers or required JavaScript.


I had come to the conclusion that I was just going to have to publish my page without an alternate image when I decided to search one last time. I ran across a site with a solution using the IE comment hack that also allowed alternate content for non IE browsers. I wish I had written down the link for you to reference as they had written 95 percent of the following code. The only thing I could not get it to do was to validate as strict XHTML.


I reworked their code and came up with a solution that would validate. Of course this is not the holy grail of Flash validation code. But it works well with simple movies. I have tested it in IE6, IE7, IE8, Firefox 3.5.5, and Safari 4.0.4 for windows. (Personally, I do not care about compatibility for obscure or older browsers.) All browsers showed the alternate image when the plugin was not installed and displayed the Flash animation when it was. I do not have access to an actual Mac so no idea on the compatibility. For the alternate image I used a jpg that was the same size as the movie.


(I do not have a handle on blogging yet so the code formatting is not the greatest.)


The actual source code can be seen here: www.webdebris.com/test/flashtest.htm

<div class="cFlashMovie">

<!--[if !IE]> <-->

<object type="application/x-shockwave-flash"

data="assets/moviename.swf" width="580" height="435">


<!--> <![endif]-->

<!--[if IE]>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"

codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"

width="580" height="435">

<param name="movie" value="assets/moviename.swf" />

<param name="quality" value="high" />

<!-->

<img src="assets/flashrequired.jpg" alt="This animation requires Flash."/>

</object>

<!--> <![endif] -->

</div>