Monday, August 10, 2015

A MS Word Macro for Auto Footer Generation

Here is a Microsoft Word macro that I worked up for a situation where I was trying to print a large number of .rtf files that didn't have any footer, but I wanted a footer that contained the filename, the print date and time, and the current page (of the total pages). It should work for printing other types of files, such as .doc, .docx, .txt, or possibly whatever you can open with word.

In my case, I didn't want to save the footer, so I wrote a second macro that calls the first one, to print the file and exit without saving the file.

Sub GenerateFooterAndInfo()
    If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
    End If
    If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
        ActivePane.View.Type = wdOutlineView Then
        ActiveWindow.ActivePane.View.Type = wdPrintView
    End If
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
    'add filename to left side of footer
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
        "FILENAME  ", PreserveFormatting:=True
    Selection.TypeText Text:=vbTab
    'add date to center of footer
    Selection.InsertDateTime DateTimeFormat:="M/d/yyyy h:mm:ss am/pm", _
        InsertAsField:=True, DateLanguage:=wdEnglishUS, CalendarType:= _
        wdCalendarWestern, InsertAsFullWidth:=False
    Selection.TypeText Text:=vbTab
    'add page of pagecount to right side of footer
    Selection.TypeText Text:="Page "
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
        "PAGE  ", PreserveFormatting:=True
    Selection.TypeText Text:=" of "
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
        "NUMPAGES  ", PreserveFormatting:=True
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

End Sub

And here is the second macro that avoids saving the changes:

Sub CreateTempFooterThenPrintAndExit()
    'close document without saving autogenerated footer
    ActiveWindow.Close (False)
End Sub

These macros make it easy to open a file and immediately print it, without changing the file, and yet having the information about the file easily at hand. These could be easily modified to print the path to the filename, etc. for either headers or footers in Word documents.

Hope this helps someone out there.

Tuesday, April 14, 2015

Add a TAB character to a Microsoft Access column directly

I was trying to add a tab character directly into a column in Microsoft Access in the same way ALT+CODE works for other types of characters. For example, ALT+0169 produces the Copyright symbol (©) just fine. But not for TAB, and probably other standard ASCII non-printable characters less than 128...


Open Notepad and enter a tab, COPY it, and go to your cell in Microsoft Access, and add the tab wherever you want it.

You won't be able to see it though...

But not to worry, just copy the whole cell, and then past it back into Notepad and you will see it show up.

Not sure why you may want a tab in your data, but if you do, this will work...

I was using SQL Server 2005 and Access 2013 for this

Clean Every SQL Table Column of Unwanted Data

Here is a simple T-SQL snippet for Microsoft SQL Server 2005 (and probably other editions) that you can use to clean up the strings in the columns of your table by removing extra spaces, tabs, carriage returns, and newlines. This is useful when you have imported data from somewhere and need to do some cleaning on it...


DESCRIPTION:  This TSQL Code snippet will perform data cleaning on every varchar
column and nvarchar column in the specified table, except for varchar(max) and
nvarchar(max) columns.

PURPOSE:  This is useful for removing extra spaces, tabs, carriage returns, new lines, and
trimming the front and end of the strings in all your columns. Add other cleaning
routines if you would like, or combine multiple cleaning activities in one statement.

    Every [n]varchar column with a length less than max will be affected.

RECOMMENDED:  Test on a COPY of the desired table before use to determine data loss.


declare @sql as varchar(2000)
declare @Table as varchar(250)
declare @Column as varchar(250)
declare @Count as int
declare @x as int

set @Table = 'Authors'         --  ENTER YOUR TABLE NAME HERE
set @Column = ''
set @Count = 0
set @x = 1

-- delete the temp table if it exists
IF OBJECT_ID(N'tempdb..#aTempTable') IS NOT NULL drop table #aTempTable

-- get valid column names in a temp table
     row_number() over (order by ordinal_position) as ROW
into #aTempTable
     data_type like '%varchar'            /* find all varchar and nvarchar columns */
     and table_name like @Table
     and character_maximum_length > 0    /* avoid varchar(max) columns which are -1 character_maximum_length */

-- how many columns are in the table?
set @Count = (select max(row) from #aTempTable)

while @x <= @Count          -- Loop through the column names, and perform data cleaning

     -- get the column name for this iteration
     set @Column = (select column_name from #aTempTable where row = @x)

     -- notify user what is happening (on the messages tab in SQL Server Managment Studio)
     print ''
     print ''
     print cast(@x as varchar(20)) + '.  Cleaning ' + @Column

     -- perform various data cleaning activities on each column of the data in a given table

     -- replace tabs, carriage returns, and newlines with spaces
     set @sql = 'update ' + @Table + ' set [' + @Column + '] = replace(replace(replace([' + @Column +'],char(10),'' ''), char(13), '' ''),char(9),'' '')'
     --select (@sql)
     exec (@sql)

     -- replace two spaces with one
     set @sql = 'update ' + @Table + ' set [' + @Column + '] = replace(replace([' + @Column +'],''  '','' ''), ''  '', '' '')'
     --select (@sql)
     exec (@sql)

     -- trim everything, removing beginning and trailing spaces
     set @sql = 'update ' + @Table + ' set [' + @Column + '] = ltrim(rtrim([' + @Column +']))'
     --select (@sql)
     exec (@sql)

     -- add your specialized cleaning code here...

     -- go to next column name
     set @x = @x + 1


drop table #aTempTable


/* Sample Output -- that would be run by the exec (@sql) statements

update Author set [AuthorName] = replace(replace([AuthorName],char(10),' '), char(13), ' ')