<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blawgg &#187; code</title>
	<atom:link href="http://vikramdhunta.com/blog/category/tech/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://vikramdhunta.com/blog</link>
	<description>Tinkerer</description>
	<lastBuildDate>Sun, 07 Aug 2011 12:31:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Formatting MS Word 2003 Documents for Kindle publishing: MS Word VBA Macro</title>
		<link>http://vikramdhunta.com/blog/2011/06/12/formatting-ms-word-2003-documents-for-kindle-publishing/</link>
		<comments>http://vikramdhunta.com/blog/2011/06/12/formatting-ms-word-2003-documents-for-kindle-publishing/#comments</comments>
		<pubDate>Sun, 12 Jun 2011 04:53:36 +0000</pubDate>
		<dc:creator>vik</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[kindle]]></category>
		<category><![CDATA[formatting]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[macro]]></category>
		<category><![CDATA[ms word]]></category>
		<category><![CDATA[vba]]></category>

		<guid isPermaLink="false">http://vikramdhunta.com/blog/?p=201</guid>
		<description><![CDATA[Tweet The Kindle Direct Publishing program is a great way for authors to self-publish their books for Kindle users. There are several limitations however when it comes to readying your book for the Kindle since Kindle only supports very basic HTML. You have to abandon any fancy formatting you may have done in recent word [...]]]></description>
			<content:encoded><![CDATA[<div id="bottomcontainerBox" style="border:1px solid #808080; border-radius:5px 5px 5px 5px; box-shadow:2px 2px 5px rgba(0,0,0,0.3);">
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fvikramdhunta.com%2Fblog%2F2011%2F06%2F12%2Fformatting-ms-word-2003-documents-for-kindle-publishing%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width=85px; height:21px;" allowTransparency="true"></iframe></div>
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<g:plusone size="medium" href="http://vikramdhunta.com/blog/2011/06/12/formatting-ms-word-2003-documents-for-kindle-publishing/"></g:plusone>
			</div>			
			<div style="float:left; width:85px; padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://vikramdhunta.com/blog/2011/06/12/formatting-ms-word-2003-documents-for-kindle-publishing/"></script></div>
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://vikramdhunta.com/blog/2011/06/12/formatting-ms-word-2003-documents-for-kindle-publishing/"  data-text="Formatting MS Word 2003 Documents for Kindle publishing: MS Word VBA Macro" data-count="horizontal">Tweet</a>
			</div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div><p>The Kindle Direct Publishing program is a great way for authors to self-publish their books for Kindle users. There are several limitations however when it comes to readying your book for the Kindle since Kindle only supports very basic HTML. You have to abandon any fancy formatting you may have done in recent word processors and give your book a &#8217;1999 Netscape Navigator 3.0 compatible website&#8217; look.</p>
<p>I recently decided to move one of my Dad&#8217;s books into this channel. The book was written in MS-word and formatted to properly print on both sides of a page. Additionally there were many non-standard formatting techniques &#8211; eg different Header styles applied to different chapter headings. Properly formatting the book for Kindle would have taken a lot of effort, so I decided to accomplish at least part of that effort, using VBA for MS-word.</p>
<p>The VBA Macro I developed is below with instructions for use. This macro Kindle-readies a word document by:<br />
1 &#8211; Transforming Page size to be 4.5&#8243;x5.5&#8243;, plus very small margins.<br />
2 &#8211; Justifies all paragraphs, and reduces their indents to a very small value<br />
3 &#8211; Merges all columns within Tables, so there are only tables with one column and as many rows as originally existed. I did this because the tables in my word documents were poorly formatted, and I decided to go with a 1-column table layout that the kindle could handle more easily.<br />
4 &#8211; Centers all tables and shapes on the page</p>
<p>This macro will also save the current document and save a copy as a HTML (Filtered) file in a specified subdirectory with a &#8220;KDP_&#8221; prefix. You can choose to switch off either option.</p>
<p>What this macro does not do -<br />
1 &#8211; Clean up headers/footers<br />
2 &#8211; Standardize formatting &#8211; e.g. the header styles for different sections<br />
3 &#8211; Clean up any font issues<br />
4 &#8211; Clean up the resulting HTML code  &#8211; I recommend running HTMLTidy within Notepad++ on the HTML files generated through this code. One specific problem that HTMLTidy resolved was long strings of space, newline or tab characters that create unnecessary whitespace on the Kindle. As this kind of whitespace may have specific purpose in your word document, I did not make this cleanup part of the code.<br />
5 &#8211; Reduce image size. Still need to figure this one out &#8211; does Kindle automatically reduce all images to viewable size?<br />
6 &#8211; a lot of other issues your word files may have!</p>
<p>Below are instructions on using the code and the code itself. <strong>Please note this was made for a very specific purpose, and there are absolutely no warranties of any kind. Use this at your own risk! And take a backup of all your files before proceeding</strong>. Also I will only make future updates to the code as required to suit my purpose. <strong>This macro was designed and tested on MS Office 2003</strong>.</p>
<p><strong>How to use this macro -</strong></p>
<ol>
<li> I run this Macro in the Normal.dot file. See these <a href="#AddMacrotoNormal">steps at the end</a> to know how to insert this code into the Normal.dot file</li>
<li>After the Macro has been inserted into the Normal.dot file, I created a toolbar button to run it. You could choose to run it from the Tools menu everytime.</li>
<li>If you would like to save resulting HTML files in a subdirectory of the folder where your word document is, then modify the KDPPATH variable in the macro.</li>
<li>This macro will save the current document (Doc) and also save a HTML version. To stop either option, you can comment out the corresponding line in the macro.</li>
<li>Open the word document that you need to convert.</li>
<li>Run the macro using the toolbar button or from the Tools menu.</li>
<li>Wait until you get a &#8216;Done&#8217; message box. Then your doc/html file should be ready. The MSWord window will show the HTML file after completion (unless you opted out of the HTML generation).</li>
<li>You may need to further work on the DOC file and address other issues before it is finally ready for Kindle formatting. If you do this, you will need to save it as an HTML from the File menu yourself.</li>
</ol>
<p><strong>Macro Code</strong></p>
<blockquote><p>Option Explicit<br />
Dim KDPPATH As String<br />
Dim INDENTVALUE As Single<br />
Public Sub PerformKindleFormatting()<br />
&#8216;if you have a subdirectory in the SAME folder as the DOC file where you want the KDP html files to be saved, then<br />
&#8216;put that subdirectory name in the KDPPATH variable below. Leave blank if you want html files to be in the same location as the DOC<br />
KDPPATH = &#8220;myKDP/&#8221;   &#8216; don&#8217;t remove the trailing backslash \ if you put a value here  !!!!!</p>
<p>INDENTVALUE = 1 / 72</p>
<p>&#8216;MsgBox &#8220;Starting &#8216;&#8221; &amp; ActiveDocument.Name &amp; &#8220;&#8216; You will receive another message when the process is done.&#8221;<br />
Step1DoPagesetup<br />
Step2FixAlignments<br />
&#8216; Step3<br />
&#8216;Step4SaveDocument   &#8216; comment out this line if you don&#8217;t want to save the modified DOC file itself<br />
Step5SaveAsWebpage  &#8216; comment out this line if you don&#8217;t want to save a copy in HTML (Filtered) format<br />
MsgBox &#8220;Done&#8221;<br />
End Sub</p>
<p>Sub Step1DoPagesetup()<br />
&#8216; MOST OF THE CODE HERE GENERATED BY RECORDING A MACRO..SOME TWEAKS AS COMMENTED BELOW<br />
With ActiveDocument.Styles(wdStyleNormal).Font<br />
If .NameFarEast = .NameAscii Then<br />
.NameAscii = &#8220;&#8221;<br />
End If<br />
.NameFarEast = &#8220;&#8221;<br />
End With<br />
With ActiveDocument.PageSetup<br />
.LineNumbering.Active = False<br />
.Orientation = wdOrientPortrait<br />
.TopMargin = InchesToPoints(INDENTVALUE)   &#8216;VERY SMALL MARGIN<br />
.BottomMargin = InchesToPoints(INDENTVALUE) &#8216;VERY SMALL MARGIN<br />
.LeftMargin = InchesToPoints(INDENTVALUE)  &#8216;VERY SMALL MARGIN<br />
.RightMargin = InchesToPoints(INDENTVALUE) &#8216;VERY SMALL MARGIN<br />
.Gutter = InchesToPoints(0)<br />
.HeaderDistance = InchesToPoints(0) &#8216;HEADER IS RIGHT ON TOP<br />
.FooterDistance = InchesToPoints(0) &#8216;FOOTER IS RIGHT AT BOTTOM<br />
.PageWidth = InchesToPoints(4.5)    &#8216;USING 4.5 INCHES TO SOMEWHAT ALIGN WITH KINDLE VIEWABLE SCREEN WIDTH<br />
.PageHeight = InchesToPoints(5.5)   &#8216;USING 5.5 INCHES TO SOMEWHAT ALIGN WITH KINDLE VIEWABLE SCREEN HEIGHT<br />
.FirstPageTray = wdPrinterDefaultBin<br />
.OtherPagesTray = wdPrinterDefaultBin<br />
.SectionStart = wdSectionContinuous &#8216; MODIFIED THIS TO CONTINOUS. TEST WITH NEWPAGE OPTION ALSO?<br />
.OddAndEvenPagesHeaderFooter = False<br />
.DifferentFirstPageHeaderFooter = False<br />
.VerticalAlignment = wdAlignVerticalTop<br />
.SuppressEndnotes = False<br />
.MirrorMargins = False<br />
.TwoPagesOnOne = False<br />
.BookFoldPrinting = False<br />
.BookFoldRevPrinting = False<br />
.BookFoldPrintingSheets = 1<br />
.GutterPos = wdGutterPosLeft<br />
End With<br />
If ActiveWindow.View.SplitSpecial &lt;&gt; wdPaneNone Then<br />
ActiveWindow.Panes(2).Close<br />
End If<br />
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.ActivePane.View.Type = wdOutlineView Then<br />
ActiveWindow.ActivePane.View.Type = wdPrintView<br />
End If<br />
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument<br />
End Sub<br />
Public Sub Step2FixAlignments()<br />
Dim i As Long<br />
Dim j As Long<br />
With ActiveDocument<br />
For i = 1 To .Paragraphs.Count<br />
.Paragraphs(i).Alignment = wdAlignParagraphJustify  &#8216; JUSTIFY ALL TEXT<br />
.Paragraphs(i).FirstLineIndent = InchesToPoints(INDENTVALUE)    &#8216; SMALL INDENT TO FIRST LINE OF PARAS<br />
.Paragraphs(i).LeftIndent = InchesToPoints(INDENTVALUE) &#8216; SMALL INDENT TO PARA<br />
Next i<br />
For i = 1 To .Sections.Count<br />
&#8216;Sections(i).Range.Borders.InsideLineStyle = wdLineStyleThickThinLargeGap    &#8216;DOING NOTHIN TO SECTIONS<br />
Next i<br />
For i = .Tables.Count To 1 Step -1<br />
For j = 1 To .Tables(i).Rows.Count<br />
.Tables(i).Rows(j).Select<br />
Selection.Cells.Merge       &#8216; MERGE COLUMNS IN TABLES SO THERE ARE ONLY TABLES WITH SINGLE COLUMNS<br />
Next j<br />
.Tables(i).Rows.Alignment = wdAlignRowCenter    &#8216;CENTER TABLE<br />
.Tables(i).AutoFitBehavior (wdAutoFitWindow)    &#8216;MAKE TABLE AUTOFIT TO WINDOW<br />
Next i<br />
&#8216;For i = 1 To .InlineShapes.Count<br />
&#8216;   .InlineShapes(i).Borders.Enable = True<br />
&#8216;   .InlineShapes(i).Borders.OutsideLineWidth = wdLineWidth150pt<br />
&#8216;   .InlineShapes(i).Borders.OutsideColor = wdColorBrightGreen<br />
&#8216;   .InlineShapes(i).Borders.OutsideLineStyle = wdLineStyleDashLargeGap<br />
&#8216;Next i<br />
Dim oILShp As InlineShape<br />
For Each oILShp In ActiveDocument.InlineShapes<br />
oILShp.Select<br />
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter    &#8216; CAUSE INLINE SHAPES TO CENTER<br />
Next<br />
For i = 1 To .Shapes.Count<br />
.Shapes(i).Left = wdShapeCenter<br />
.Shapes(i).RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin  &#8216;CENTER SHAPES<br />
Next i<br />
End With<br />
End Sub<br />
Sub Step4SaveDocument()<br />
&#8216; Currently we just save the document AS-IS. We don&#8217;t save a copy. Because if we save a copy,<br />
&#8216; its the copy that&#8217;s open when the HTML file is being saved next, and that distorts the path/filenames<br />
ActiveDocument.Save</p>
<p>&#8216;Dim fn As String<br />
&#8216;fn = &#8220;KDP_&#8221; &amp; ActiveDocument.Name<br />
&#8216;Debug.Print &#8220;Saving as &#8221; &amp; ActiveDocument.Path &amp; fn<br />
&#8216;ActiveDocument.SaveAs FileName:=ActiveDocument.Path &amp; &#8220;\&#8221; &amp; KDPPATH &amp; fn, FileFormat:=wdFormatDocument</p>
<p>End Sub<br />
Sub Step5SaveAsWebpage()</p>
<p>With ActiveDocument.WebOptions<br />
.RelyOnCSS = False              &#8216; STILL RESULTS IN CSS .. MSWORD #FML<br />
.OptimizeForBrowser = True      &#8216; OK YA?<br />
.OrganizeInFolder = True<br />
.UseLongFileNames = True<br />
.RelyOnVML = False              &#8216; VML HA HA<br />
.AllowPNG = False               &#8216; I LIKE MY LAWSUITS IN GIF<br />
.ScreenSize = msoScreenSize640x480  &#8216; CLOSEST OPTION TO KINDLE?<br />
.PixelsPerInch = 72             &#8216; READ SOMEWHERE THAT KINDLE IS GOOD WITH 72 DPI?<br />
.Encoding = msoEncodingWestern<br />
End With<br />
With Application.DefaultWebOptions<br />
.UpdateLinksOnSave = True<br />
.CheckIfOfficeIsHTMLEditor = True<br />
.CheckIfWordIsDefaultHTMLEditor = True<br />
.AlwaysSaveInDefaultEncoding = False<br />
.SaveNewWebPagesAsWebArchives = False<br />
End With</p>
<p>&#8216; this part takes the file name, removes the DOC (or any) extension, and prepends KDP to form a new filename with extension html<br />
&#8216; it will save the new html file in the same location as the original DOC file.<br />
&#8216; If you put a value in the KDPPATH variable in the GLOBAL section on the top of this code,<br />
&#8216; the html file will be saved in the resulting KDPPATH subdirectory<br />
Dim fn As String<br />
fn = ActiveDocument.Name<br />
fn = &#8220;KDP_&#8221; &amp; Left(fn, InStr(fn, &#8220;.&#8221;) &#8211; 1)<br />
Debug.Print &#8220;Saving as &#8221; &amp; ActiveDocument.Path &amp; fn<br />
ActiveDocument.SaveAs FileName:=ActiveDocument.Path &amp; &#8220;\&#8221; &amp; KDPPATH &amp; fn, FileFormat:=wdFormatFilteredHTML<br />
End Sub</p></blockquote>
<p><strong>Other Useful links:</strong></p>
<ol>
<li><a href="http://johnaugust.com/2009/kindle-formatting-for-web-geeks">John August&#8217;s basics for Kindle formatting</a></li>
<li><a href="http://www.newselfpublishing.com/WordKindle.html">Aaron Shepard&#8217;s tutorial on word to Kindle formatting</a></li>
<li><a href="http://kindleformatting.com/faq.php">Kindle Formatting Faq</a></li>
<li><a href="http://kindleformatting.com/book/files/KindleHTMLtags.pdf">HTML tags compatible with Kindle</a></li>
<li><a href="http://pubs.logicalexpressions.com/pub0009/LPMArticle.asp?ID=151">Understand Normal.dot</a></li>
</ol>
<p><strong><a name="AddMacrotoNormal">How to add a macro to your Normal.dot file</a></strong></p>
<p>1 &#8211; Open MS Word<br />
&#8212; Close any documents such as the &#8220;New blank document&#8221; that opens up.<br />
2 &#8211; Click on the menu Tools-&gt;Macros-&gt;Visual Basic Editor (Alternatively press Alt-F11)<br />
3 &#8211; If you don&#8217;t see a &#8220;Project&#8221; pane on the left hand side of the VB editor, click on the menu View-&gt;Project Explorer<br />
4 &#8211; In the project pane, you should be able to see a high level item called &#8220;Normal&#8221;, under which there will be a subitem called &#8220;Modules&#8221;.<br />
5 &#8211; If under &#8220;Modules&#8221;. you see a module called &#8220;NewMacros&#8221;, then double click on it to open it in the editor. If you don&#8217;t, then right click on &#8220;Modules&#8221;, and select the contextmenu item Insert-&gt;Module. Open the new module for editing.<br />
6 &#8211; Paste the macro into the module, in the editor window.<br />
7 &#8211; Save the file. Close word. Now the macro should be a permanent part of your MSWord and can be used on any word document you open in the future.</p>
]]></content:encoded>
			<wfw:commentRss>http://vikramdhunta.com/blog/2011/06/12/formatting-ms-word-2003-documents-for-kindle-publishing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Turning a webcam into a home security monitor while you&#8217;re away</title>
		<link>http://vikramdhunta.com/blog/2009/11/20/turning-a-webcam-into-a-home-security-monitor-while-youre-away/</link>
		<comments>http://vikramdhunta.com/blog/2009/11/20/turning-a-webcam-into-a-home-security-monitor-while-youre-away/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 04:06:34 +0000</pubDate>
		<dc:creator>vik</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[home security]]></category>
		<category><![CDATA[laptop health check]]></category>
		<category><![CDATA[security webcam]]></category>
		<category><![CDATA[VB]]></category>

		<guid isPermaLink="false">http://vikramdhunta.com/blog/?p=63</guid>
		<description><![CDATA[Tweet Due to security concerns in our vicinity last year, I needed a way to keep tabs on our home while the whole family was away on vacation. The idea was to be able to view images of your home, live over the internet from whereever we were. CCTV solutions weren&#8217;t an option as they [...]]]></description>
			<content:encoded><![CDATA[<div id="bottomcontainerBox" style="border:1px solid #808080; border-radius:5px 5px 5px 5px; box-shadow:2px 2px 5px rgba(0,0,0,0.3);">
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fvikramdhunta.com%2Fblog%2F2009%2F11%2F20%2Fturning-a-webcam-into-a-home-security-monitor-while-youre-away%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width=85px; height:21px;" allowTransparency="true"></iframe></div>
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<g:plusone size="medium" href="http://vikramdhunta.com/blog/2009/11/20/turning-a-webcam-into-a-home-security-monitor-while-youre-away/"></g:plusone>
			</div>			
			<div style="float:left; width:85px; padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://vikramdhunta.com/blog/2009/11/20/turning-a-webcam-into-a-home-security-monitor-while-youre-away/"></script></div>
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://vikramdhunta.com/blog/2009/11/20/turning-a-webcam-into-a-home-security-monitor-while-youre-away/"  data-text="Turning a webcam into a home security monitor while you&#8217;re away" data-count="horizontal">Tweet</a>
			</div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div><p>Due to security concerns in our vicinity last year, I needed a way to keep tabs on our home while the whole family was away on vacation. The idea was to be able to view images of your home, live over the internet from whereever we were. CCTV solutions weren&#8217;t an option as they were expensive and none seemed to have a remote viewing option. I believe now there are advanced Logitech webcams in the market that also pivot and stream images to the net, but again they come at a price, and don&#8217;t take care of a few things like problems with your internet connection or laptop power when you&#8217;re not available to take care of them.</p>
<p>However, there are freeware/shareware solutions on the internet that allow you to capture your webcam and stream it online, even at a particular schedule.The software that I used was a super little app called <a title="Active Webcam" href="http://www.pysoft.com/ActiveWebCamMainpage.htm" target="_blank">Active Webcam</a> from Pysoft.</p>
<p>Further, I needed a software that would take care of laptop restarts/windows crashes/net connection blips (and also auto logging into the net account if the login expires every 24 hrs) and that would automatically start Active Webcam every time the system started up &#8211; I decided to code that in VB.</p>
<p>Apart from the software I also needed my personal laptop, regular webcam and a DSL internet connection. I had gprs activated on my phone as well, in order to view the images uploaded.</p>
<p>First, i installed Active Webcam and set it up so it would stream images from the webcam to my website at a specific URL.  (Yes, you need a website host that offers FTP where the images will be uploaded to &#8211; possibly there are options to upload to photo services like flickr/photobucket too, or to send emails to you with attached images)</p>
<p>Second, compile the code below into an exe and include that exe in the Windows Startup so it runs at system startup.</p>
<p>When this program runs, it will<br />
- check for internet connection availability, and if not available will to restart the local modem<br />
- if the internet connection doesn&#8217;t come up after 3 modem restarts, it will restart Windows. It won&#8217;t restart windows more than once in 15 mins though.<br />
- log the laptop&#8217;s battery status<br />
- log everything to a remote php file so the logs can be viewed remotely too</p>
<p><a title="VB Security cam health checker.zip" href="http://www.vikramdhunta.com/blog/vikuploads/vb security cam healthchecker.zip" target="_self">Download the entire VB project</a>.</p>
<p>This VB project has :<br />
1. a form called frmMain<br />
2. a module called Module1.bas</p>
<p>The code for both is shown below &#8211; a lot of the sections came from various sources on the net, so no claims there and a big thanks to them. Also, it all seemed to work at the time. <img src='http://vikramdhunta.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Also there are certain urls &#8211; while most are simply used to check the internet connection, the &#8217;192.168.1.1&#8242; is my modem&#8217;s local admin page that you may need to change accordingly. (In order to restart the modem, I used the code here to login to the local admin at 192.168.1.1 and post my username/password etc to the appropriate file (<span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">restartmodem.htm))<br />
</span></p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p><strong>frmMain.frm &#8211; code</strong></p>
<blockquote><p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;"> Private Declare Function InternetGetConnectedState Lib &#8220;wininet.dll&#8221; (ByRef dwFlags As Long, ByVal dwReserved As Long) As Long</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Private Declare Function InternetCheckConnection Lib &#8220;wininet.dll&#8221; Alias &#8220;InternetCheckConnectionA&#8221; (ByVal lpszUrl As String, ByVal dwFlags As Long, ByVal dwReserved As Long) As Long</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Private Declare Function SendMessage Lib &#8220;user32&#8243; Alias &#8220;SendMessageA&#8221; (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long<br />
Private Declare Function OpenProcess Lib &#8220;kernel32&#8243; (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long<br />
Private Declare Function WaitForSingleObject Lib &#8220;kernel32&#8243; (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long<br />
Private Declare Function CloseHandle Lib &#8220;kernel32&#8243; (ByVal hObject As Long) As Long</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Private Declare Function GetExitCodeProcess Lib &#8220;kernel32&#8243; _<br />
(ByVal hProcess As Long, lpExitCode As Long) As Long</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Private Declare Function TerminateProcess Lib &#8220;kernel32&#8243; _<br />
(ByVal hProcess As Long, ByVal uExitCode As Long) As Long</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Private Const FLAG_ICC_FORCE_CONNECTION = &amp;H1</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Private Declare Function NetUserGetInfo Lib &#8220;netapi32&#8243; (lpServer As Any, lpUserName As Any, ByVal Level As Long, lpBuffer As Any) As Long<br />
Private Declare Function GetUserName Lib &#8220;advapi32.dll&#8221; Alias &#8220;GetUserNameA&#8221; (ByVal lpBuffer As String, nSize As Long) As Long<br />
Private Declare Function GetComputerName Lib &#8220;kernel32&#8243; Alias &#8220;GetComputerNameA&#8221; (ByVal lpBuffer As String, nSize As Long) As Long<br />
Private Declare Sub CopyMemory Lib &#8220;kernel32&#8243; Alias &#8220;RtlMoveMemory&#8221; (Destination As Any, Source As Any, ByVal Length As Long)</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Private Const UNLEN As Long = 256         &#8216; Maximum username length<br />
Private Const CNLEN As Long = 31          &#8216; Maximum computer name length<br />
Private Const NERR_Success As Long = 0&amp;</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Dim actcamShellID As Long</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Dim nwErrorCounter As Integer<br />
Const nwErrorThreshold = 1</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Dim WinHttpReq As WinHttp.WinHttpRequest<br />
Const HTTPREQUEST_SETCREDENTIALS_FOR_SERVER = 0<br />
Const HTTPREQUEST_SETCREDENTIALS_FOR_PROXY = 1</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Public Enum shutdownTypes</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Logoff = 0<br />
Shutdown = 1<br />
Reboot = 2<br />
Force = 4<br />
PowerOff = 8</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">End Enum</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Private Type LUID</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">UsedPart As Long<br />
IgnoredForNowHigh32BitPart As Long</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">End Type</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Private Type TOKEN_PRIVILEGES</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">PrivilegeCount As Long<br />
TheLuid As LUID<br />
Attributes As Long</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">End Type</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">&#8216;<br />
&#8216;The function used to actually send the request to shutdown windows. Set the &#8216;shutdownTypes&#8217;<br />
&#8216;parameter to whether you want windows to &#8220;shutdown, reboot, logOff, ect&#8230;&#8221;<br />
Private Declare Function ExitWindowsEx Lib &#8220;user32&#8243; (ByVal shutdownType As Long, ByVal dwReserved As _<br />
Long) As Long<br />
&#8216;<br />
&#8216;Will get a handle to the process this function is called.<br />
Private Declare Function GetCurrentProcess Lib &#8220;kernel32&#8243; () As Long<br />
&#8216;<br />
&#8216;<br />
&#8216;The functions below are all used to give the application that the library is bound to the proper privilege so<br />
&#8216;the OS will allow the app to Shutdown Windows.<br />
&#8216;<br />
&#8216;<br />
Private Declare Function OpenProcessToken Lib &#8220;advapi32&#8243; (ByVal ProcessHandle As Long, ByVal _<br />
DesiredAccess As Long, ByRef TokenHandle As Long) As Long<br />
&#8216;<br />
Private Declare Function LookupPrivilegeValue Lib &#8220;advapi32&#8243; Alias &#8220;LookupPrivilegeValueA&#8221; (ByVal _<br />
lpSystemName As String, ByVal lpName As String, ByRef lpLuid As LUID) As Long<br />
&#8216;<br />
Private Declare Function AdjustTokenPrivileges Lib &#8220;advapi32&#8243; (ByVal TokenHandle As Long, ByVal _<br />
DisableAllPrivileges As Boolean, ByRef NewState As TOKEN_PRIVILEGES, ByVal BufferLength As _<br />
Long, ByRef PreviousState As TOKEN_PRIVILEGES, ByRef ReturnLength As Long) As Long</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Private Sub cmdStart_Click()<br />
&#8216; moved below  2 to formm load now<br />
&#8216;Log &#8220;Starting timer&#8230;&#8221;<br />
&#8216;Timer1.Enabled = True</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">&#8216;In my case I started Active Webcam using the windows startup options &#8211; if you want this healthchecker to start the app, uncomment this line and also the one cmdStop_Click()<br />
&#8216;actcamShellID = Shell(&#8220;C:\Program Files\Active WebCam\WebCam.exe&#8221;, vbMinimizedNoFocus)<br />
End Sub</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Private Sub cmdStop_Click()<br />
Timer1.Enabled = False<br />
Log &#8220;Stopped timer.&#8221;</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">&#8216;Debug.Print EndShelledProcess(actcamShellID)<br />
End Sub</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Private Sub Form_Load()<br />
Set WinHttpReq = New WinHttpRequest<br />
Log &#8220;Application started&#8230;&#8221;, 1</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Log &#8220;Starting timer&#8230;&#8221;<br />
Timer1.Enabled = True</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">nwErrorCounter = 0<br />
End Sub</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Private Sub Form_Terminate()<br />
Log &#8220;Application terminated.&#8221;, 1<br />
End Sub</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Private Sub Timer1_Timer()<br />
DoEvents<br />
Dim nwTrulyBad As Integer</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">If (nwErrorCounter &gt; nwErrorThreshold) Then<br />
&#8216; too many times nw error &#8211; just restart network<br />
Log &#8220;Timer off&#8230;&#8221;<br />
Timer1.Enabled = False<br />
Log &#8220;restarting network&#8221;</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">nwTrulyBad = 0<br />
While (nwTrulyBad &lt; 3)<br />
Log &#8220;&#8230; restart attempt &#8221; &amp; (nwTrulyBad + 1)<br />
restartModem<br />
If (InternetCheckConnection(&#8220;http://web1219.ixwebhosting.com&#8221;, FLAG_ICC_FORCE_CONNECTION, 0&amp;) &lt;&gt; 0) Then<br />
Log &#8220;&#8230; restart attempt succeeded &#8221;<br />
nwTrulyBad = 0<br />
&#8216;Exit Do<br />
Else<br />
nwTrulyBad = nwTrulyBad + 1<br />
Log &#8220;&#8230; restart attempt failed.&#8221;<br />
End If<br />
Wend</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">If (nwTrulyBad &gt;= 3) Then<br />
Log &#8220;&#8230; Modem restart failed. Need to restart system&#8221;<br />
&#8216; Network did nost cmoe up even after restarting modem 3 times. Should restart system now.<br />
&#8216; however do not restart if last system reboot time was less than 15 mins ago<br />
If ((Now &#8211; LastLogon2()) &gt; 0.0104) Then<br />
shutdownWindowsOS shutdownTypes.Reboot Or Force<br />
Log &#8220;PC RESTARTING&#8221;<br />
Else<br />
Log &#8220;TOO SOON TO RESTART&#8221;<br />
End If<br />
End If</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">nwErrorCounter = 0<br />
End If</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Timer1.Enabled = True</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">If InternetCheckConnection(&#8220;http://web1219.ixwebhosting.com&#8221;, FLAG_ICC_FORCE_CONNECTION, 0&amp;) = 0 Then<br />
nwErrorCounter = nwErrorCounter + 1<br />
Timer1.Interval = 15000<br />
Log &#8220;No internet connection detected&#8230;nw counter: &#8221; &amp; nwErrorCounter<br />
Else<br />
nwErrorCounter = 0<br />
Timer1.Interval = 10000<br />
Log &#8220;Internet connection detected&#8230;&#8221;<br />
End If</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Log &#8220;Battery status: &#8221; &amp; GetBatteryStatus()<br />
If (InStr(1, GetBatteryStatus(), &#8220;Low&#8221;)) Then<br />
Log &#8220;BatteryRed&#8221; &amp; GetBatteryStatus(), 1<br />
End If<br />
End Sub</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Private Sub restartModem()</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">DoEvents</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">&#8216; Assemble an HTTP request.s<br />
WinHttpReq.Open &#8220;POST&#8221;, &#8220;http://192.168.1.1/cgi-bin/webcm&#8221;, False</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">&#8216; Set the user name and password.<br />
WinHttpReq.SetCredentials &#8220;admin&#8221;, &#8220;admin&#8221;, HTTPREQUEST_SETCREDENTIALS_FOR_SERVER</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">&#8216;Set Content-Type header<br />
WinHttpReq.SetRequestHeader &#8220;Content-Type&#8221;, &#8220;application/x-www-form-urlencoded&#8221;</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">On Error GoTo nextpt<br />
&#8216;Send the form data To URL As POST binary request<br />
WinHttpReq.Send &#8220;getpage=&#8221;"http://192.168.1.1/cgi-bin/html/tools/restartmodem.htm&#8221;"&amp;var:com=&#8221;"restart&#8221;"&amp;var:ip=&#8221;"&#8221;"&amp;var:restart=&#8221;"1&#8243;&#8221;&amp;logic:command/ppp_disconnect=&#8221;"&#8221;"&#8221;</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">&#8216; Display the status code and response headers.<br />
Debug.Print WinHttpReq.Status &amp; &#8221; &#8221; &amp; WinHttpReq.StatusText<br />
Debug.Print WinHttpReq.GetAllResponseHeaders &amp; &#8220;  &#8221; &amp; WinHttpReq.ResponseText</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">nextpt:<br />
End Sub</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Public Sub Log(txt As String, Optional priority As Integer)<br />
&#8216;txtLog.Text = txtLog.Text &amp; vbNewLine &amp; txt</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">&#8216;Open &#8220;c:\hclog.txt&#8221; For Append As #2<br />
&#8216;Print #2, Now<br />
&#8216;Print #2, txt<br />
&#8216;Print #2, String(91, &#8220;*&#8221;)<br />
&#8216;Close #2</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">If (priority = 1) Then<br />
WinHttpReq.Open &#8220;GET&#8221;, &#8220;http://www.YOURSITE.com/log.php?txtlog=&#8221; &amp; Now &amp; &#8220;, &#8221; &amp; txt, True<br />
WinHttpReq.Send<br />
On Error Resume Next<br />
End If<br />
End Sub<br />
Public Function EndShelledProcess(ShellReturnValue As Long) _<br />
As Boolean</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">&#8216;PURPOSE: End a process started with VB&#8217;s Shell Statement<br />
&#8216;INPUT: Task ID returned by Shell<br />
&#8216;RETURNS: True if succesful, false otherwise</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">On Error Resume Next</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Dim hInst As Long<br />
Dim hProcess As Long<br />
Dim lExitCode As Long<br />
Dim lRet As Long</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">hInst = ShellReturnValue<br />
If hInst = 0 Then Exit Function</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">&#8216;Get handle to process<br />
hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0&amp;, hInst)<br />
If hProcess &lt;&gt; 0 Then<br />
&#8216;get exit code<br />
GetExitCodeProcess hProcess, lExitCode<br />
If lExitCode &lt;&gt; 0 Then<br />
&#8216;bye-bye<br />
lRet = TerminateProcess(hProcess, lExitCode)<br />
EndShelledProcess = lRet &gt; 0<br />
End If<br />
End If</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">End Function</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Private Sub AdjustToken()<br />
Const TOKEN_ADJUST_PRIVILEGES = &amp;H20<br />
Const TOKEN_QUERY = &amp;H8<br />
Const SE_PRIVILEGE_ENABLED = &amp;H2<br />
Dim hdlProcessHandle As Long<br />
Dim hdlTokenHandle As Long<br />
Dim tmpLuid As LUID<br />
Dim tkp As TOKEN_PRIVILEGES<br />
Dim tkpNewButIgnored As TOKEN_PRIVILEGES<br />
Dim lBufferNeeded As Long</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">hdlProcessHandle = GetCurrentProcess()<br />
OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or _<br />
TOKEN_QUERY), hdlTokenHandle</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">&#8216; Get the LUID for shutdown privilege.<br />
LookupPrivilegeValue &#8220;&#8221;, &#8220;SeShutdownPrivilege&#8221;, tmpLuid</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">tkp.PrivilegeCount = 1    &#8216; One privilege to set<br />
tkp.TheLuid = tmpLuid<br />
tkp.Attributes = SE_PRIVILEGE_ENABLED</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">&#8216; Enable the shutdown privilege in the access token of this process.<br />
AdjustTokenPrivileges hdlTokenHandle, False, _<br />
tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">End Sub</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Public Function LastLogon2() As Date<br />
Dim nRet As Long<br />
Dim Server As String<br />
Dim User As String<br />
Dim ui3(1 To 29) As Long<br />
Dim lpBuffer As Long<br />
Const evServer = &#8220;LOGONSERVER&#8221;<br />
&#8216; Retrieve current user and logon server names.<br />
User = CurrentUserName()<br />
Server = Environ$(evServer)<br />
If Len(Server) = 0 Then Server = CurrentMachineName()<br />
If InStr(Server, &#8220;\\&#8221;) &lt;&gt; 1 Then Server = &#8220;\\&#8221; &amp; Server<br />
&#8216; Retrieve Level3 information on current user.<br />
nRet = NetUserGetInfo(ByVal StrPtr(Server), ByVal StrPtr(User), 3, lpBuffer)<br />
If nRet = NERR_Success Then<br />
&#8216; Results are returned as a pointer to buffer.<br />
&#8216; Copy 29 dwords/pointers into array of Longs.<br />
CopyMemory ui3(1), ByVal lpBuffer, UBound(ui3) * 4&amp;<br />
&#8216; Return last logon date/time, which is 14th element.<br />
LastLogon2 = NetTimeToVbTime(ui3(14))<br />
End If<br />
End Function</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Private Function CurrentUserName() As String<br />
Dim Buffer As String<br />
Dim nLen As Long<br />
Const NameLength = UNLEN + 1<br />
&#8216; ANSI method.<br />
nLen = NameLength<br />
Buffer = Space$(nLen)<br />
Call GetUserName(Buffer, nLen)<br />
&#8216; Trim results and return.<br />
If nLen &gt; 1 Then CurrentUserName = Left$(Buffer, nLen &#8211; 1)<br />
End Function</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Private Function NetTimeToVbTime(NetDate As Long) As Double<br />
Const BaseDate# = 25569   &#8216;DateSerial(1970, 1, 1)<br />
Const SecsPerDay# = 86400<br />
NetTimeToVbTime = BaseDate + (CDbl(NetDate) / SecsPerDay)<br />
End Function</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Private Function CurrentMachineName() As String<br />
Dim Buffer As String<br />
Dim nLen As Long<br />
Const NameLength = CNLEN + 1<br />
&#8216; Fall back to the ANSI method.<br />
nLen = NameLength<br />
Buffer = Space$(nLen)<br />
Call GetComputerName(Buffer, nLen)<br />
&#8216; Trim results and return.<br />
If nLen &gt; 0 Then CurrentMachineName = Left$(Buffer, nLen)<br />
End Function</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Function shutdownWindowsOS(ByVal shutdownType As shutdownTypes) As Integer<br />
&#8216;<br />
&#8216;This function will call the ExitWindowsEx function to request the system to shutdown/reboot or<br />
&#8216;whatever is specified in the &#8220;shutdownType&#8221; parameter. It of course, will call the &#8220;AdjustToken&#8221;<br />
&#8216;sub to give your app the privilege to use that function first.<br />
&#8216;<br />
AdjustToken<br />
&#8216;<br />
&#8216;Calls the function to begin executing.<br />
shutdownWindowsOS = ExitWindowsEx(shutdownType, 0)</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">End Function</span></p></blockquote>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p><strong>Module1.bas</strong></p>
<blockquote><p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">ByRef lpSystemPowerStatus As SYSTEM_POWER_STATUS) As Long</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Public Function GetBatteryStatus() As String &#8216; Get the status of the laptop&#8217;s battery<br />
Dim btFlag As Byte<br />
Dim SPS As SYSTEM_POWER_STATUS<br />
Dim lReturn As Long<br />
Dim sRet As String<br />
Dim lFlag As Long</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">lReturn = GetSystemPowerStatus(SPS)<br />
btFlag = SPS.BatteryFlag</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">lFlag = CLng(btFlag)</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">Select Case lFlag<br />
Case BATTERY_FLAG_CHARGING<br />
sRet = &#8220;Now Charging&#8230;&#8221;<br />
Case BATTERY_FLAG_CRITICAL<br />
sRet = &#8220;Critically Low !!&#8221;<br />
Case BATTERY_FLAG_HIGH<br />
sRet = &#8220;High Charged!&#8221;<br />
Case BATTERY_FLAG_LOW<br />
sRet = &#8220;Low Charged!&#8221;<br />
Case BATTERY_FLAG_NO_BATTERY<br />
sRet = &#8220;(No Battery)&#8221;<br />
Case BATTERY_FLAG_UNKNOWN<br />
sRet = &#8220;Unknown&#8221;<br />
End Select</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">If (SPS.ACLineStatus = 1) Then<br />
sRet = &#8220;[On AC] &#8221; &amp; sRet<br />
Else<br />
sRet = &#8220;[On Battery] &#8221; &amp; sRet</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">End If</span></p>
<p><span style="font-family: Courier New,Courier,mono,Tahoma,Arial;">GetBatteryStatus = sRet<br />
End Function</span></p></blockquote>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; </p>
]]></content:encoded>
			<wfw:commentRss>http://vikramdhunta.com/blog/2009/11/20/turning-a-webcam-into-a-home-security-monitor-while-youre-away/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

