Queens Dev
Some more ASP.NET

VBScript to Change AutoText in Word 2003 Normal.dot

March 20, 2008 at 1:17 AM by bjsetegn

My client recently changed there name for branding reasons, making it more concise.  They have around 100 employees using Word with an Autotext (auto complete) entry for their old company name.  For anyone who doesn't already know what the Autotext feature is in Word it is a special feature that will complete a phrase for you after you type the first couple of letters for it.  This actual text for the Autotext is stored in the Normal.Dot file located deep within your file systems  which is the default Word template.  This template is loaded every time you open a new document.  Outlook also uses Word as its editor and thus uses the Normal.dot.  

Anyway, they wanted to change the Autotext entry for their company name on all 100 computers.  This would have been quite a hassle for each user to do themselves or taken a lot of time for one person to run around to each computer and change it.  So they had me write a script that would change the entry and could be set to run the next time the user logged in.  At first I tried to edit the Normal.dot file manually through a filestream but I ran into problems when writing the changes back.  Special characters and other items in the file header were either erased or changed and the file would be corrupted.  So I reached out for some help and an IT guy sent me a link to some code that looked a lot like what I have below (I can't find the link or I would post it, only half of what's below I will claim as my own work, the easy half).  I modified it to work for my situation and added some error checking in case it was run more than once and the old record wasn't found.  It worked pretty well and for once I was assured I had saved the client some money.  Oh how powerful vbscript can be.

To run the code save the text below to a text file such as RenameAutotext.vbs.  Then from the command prompt run: cscript RenameAutotext.vbs.  Run using cscript not wscript because if run using wscript will send the Echo command to the program running, Word, instead of writing it to the command line.  This script is also test to work only for Windows XP and Microsoft Word 2003.  Word 2007 uses a different type of template file so and this script will need to be modified to work with that.

There may be some extraneous parts that aren't 100% necessary below but with vbscript I am happy when I get it to do exactly what I want and leave it at that.  If anyone has some suggestions for improvement leave a comment. 

The Code: 

    'Rename an

    'Get Application Data Path

    Set oShell = CreateObject("WScript.Shell")

    appdata = oShell.ExpandEnvironmentStrings("%APPDATA%")

 

    Set objWord = CreateObject("Word.Application")

    objWord.Visible = True

 

    Set objDoc = objWord.Documents.Open(appdata & "\Microsoft\Templates\Normal.dot", False)

 

    'Add new autotext entries

    Set objTxtEntry = objWord.NormalTemplate.AutoTextEntries.Add("New Company Name", objWord.Selection.Range)

    objWord.NormalTemplate.AutoTextEntries("New Company Name").Value = "New Company Name"

 

    'Enable error handling

    On Error Resume Next

    'Delete old autotext entries

    objWord.NormalTemplate.AutoTextEntries("Old Company Name").Delete

 

    If Err.Number <> 0 Then

        WScript.Echo "No Old Company Name record found, new entry inserted."

    Else

        WScript.Echo "Completed Successfully."

    End If

 

    'Print all autotext entires to make sure it was changed properly

    'Wscript.Echo

    Dim i

    For Each i In objWord.NormalTemplate.AutoTextEntries

       Wscript.Echo i.Name

    Next

 

    objWord.Quit

 

ASP.NET Web.Config Inhertiance - Problem and Solution

March 17, 2008 at 5:35 AM by bjsetegn

While trying to get a Samples section of this website up and running I ran into a couple of problems.  When creating a virtual directory in IIS that was physically under the main application (QueensDev\Samples being the location of the subdirectory) I got an error that one of the Blogengine modules wasn't found.  I thought this was odd because the Samples section didn't use any Blogengine dll's at all. 

Turns out that any application that is in a subdirectory of another application in IIS will inherit all properties from the parent web.config.  This can be highly desirable in certain situations but it wasn't what I was suspecting since it was a separate application in the subdirectory.  It was a problem for me because the subdirectory doesn't contain all the dll's refrenced in the parent directory which was causing my app not to load.  I believe it was trying to find the HTTPModules and Handlers.

I ended up using the solution below.  Wrap system.web in the web.config with the location tag, self explantory.  However, VS2005 and 2008 don't seem to like the location tag but it works just fine. I found this at:

http://aspadvice.com/blogs/ssmith/archive/2006/08/21/HttpModule-Breaks-SubApplications-Problem-Solved.aspx 

<location path="." inheritInChildApplications="false" allowOverride="true">
    <system.web> ...

    </system.web>
 </location> 

More info can be found here:

http://blogs.infragistics.com/blogs/ambrose_little/archive/2007/06/20/isolating-web-settings-from-sub-applications-using-inheritinchildapplications.aspx 

http://msdn2.microsoft.com/en-us/library/ms178685.aspx 

ASP.NET Scroll Position, Page Flicker and Back Button History - Smart Navigation Functionality.

March 16, 2008 at 6:35 AM by bjsetegn

Well for some reason Microsoft decided to deprecate the smart navigation functionality in ASP.NET 2.0 and further (this is old news).  They probably just don't want to spend the time maintaining custom javascript to handle these things in all the different browsers.  This was a great feature when it worked.  You should now use MaintainScrollPositionOnPostBack which does what it says it does and thats it.  It doesn't have all the functionality of SmartNavigation but should work better.  Still haven't found a good solution for Page flicker when loading pages yet other than using AJAX but Microsoft has a couple of ideas.

More info can be found here:

http://support.microsoft.com/kb/913721 

Tags:

ASP.NET

ASP.NET - Repeater vs Datalist vs Gridview

March 14, 2008 at 8:33 AM by bjsetegn

There's a whole lot to be said about the Repeater, Datalist, and Gridview controls and I think it best if they speak for themselves.  So below is a simple page with a repeater, datalist, and gridview.  Each control loads the same sample data to show how they each render.  You can download the code below to take a look at how they each get created.  There are best practices tips in there as well.  Asking what the difference is between these three controls is a great question in an interview with a junior level developer.  If they know when and how to use each they have a mastered the basics of ASP.NET.

Also when taking a look at the link below take a look at the page source.  A great tool for this if you don't have it yet is Firebug.  It will make developing and styling and ASP.NET page 10x easier.

Repeater, Datalist, and Gridview Sample Page:

http://www.queensdev.net/Samples/RepeaterDataListGridview.aspx

To run the sample simply extract the files in the zip to their own folder, open Visual Studio 2005 or 2008, go to File->Open->Website and open the folder you extracted the files to.

Sample code can be found here:

RepeaterDatalistGridview.zip

Tags: ,

ASP.NET | C#