crslack,
Here's a vb program I wrote (it's pretty ugly, but I was in a heck of a hurry). Anyway, what it does is creates a new Word document, then adds a module to it, and adds a couple of subs to the module, including one that is run after creation, which inserts a reference to the ADO, and another one that processes commands from click event procedures which it creates in the ThisDocument Module.
The program creates hyperlinks in the document that are used to look up report images in a Filenet system, and command buttons that delete associated rows from an Oracle database.
It's probably way more than you are looking for, but maybe it will give you some idea of some more possibilities.
Tranman
Option Explicit
Public appW As New Word.Application
Public doc As Word.Document
Public lngRBegin As Long
Public lngREnd As Long
Public rng As Range
Public rngAccNo As Range
Public rngBtn As Range
Public strDBName As String
Public strDBYear As String
Public strDocName As String
Public strInFN As String
Public strOutFN As String
Public strWord As String
Private Sub Form_Load()
ParseCmd
OpenFiles
ProcFile
Close
doc.SaveAs strOutFN
appW.Quit
Set appW = Nothing
Set doc = Nothing
Set rng = Nothing
Set rngAccNo = Nothing
Set rngBtn = Nothing
End
End Sub
Private Sub ParseCmd()
Dim intSpPos As Integer
Dim strCmdLine As String
strCmdLine = Command()
'MsgBox strCmdLine, vbOKOnly, "COMMAND LINE"
strCmdLine = Trim(strCmdLine)
If strCmdLine = "" Then Exit Sub
intSpPos = InStr(1, strCmdLine, " ", vbTextCompare)
strInFN = Left(strCmdLine, intSpPos - 1)
strDBName = Mid(strCmdLine, Len(strCmdLine) - intSpPos, 8)
strDBYear = Right(strCmdLine, 4)
End Sub
Private Sub OpenFiles()
strOutFN = Left(strInFN, Len(strInFN) - 3) & "DOC"
Open strInFN For Input As #1
Set doc = appW.Documents.Add
appW.Visible = False
appW.Options.CheckGrammarAsYouType = False
appW.Options.CheckSpellingAsYouType = False
Options.CheckGrammarWithSpelling = False
doc.PageSetup.LeftMargin = 36
doc.PageSetup.RightMargin = 36
doc.PageSetup.TopMargin = 36
doc.PageSetup.BottomMargin = 36
Set rng = doc.Range
rng.Font.Name = "courier new"
rng.Font.Size = 8
End Sub
Private Sub ProcFile()
Dim dblWordStart As Double
Dim intLen As Integer
Dim intResult As Integer
Dim lngLineCtr As Long
Dim rngDel As Range
Dim sCode1 As String
Dim shp1 As Word.InlineShape
Dim strDelSub As String
Dim strRefADO As String
Dim strIn As String
Dim strStop As String
Dim strAddr As String
Dim VBComp As VBComponent
'Add NewMod module to the Word Document
Set VBComp = doc.VBProject.VBComponents.Add(vbext_ct_StdModule)
VBComp.Name = "NewMod"
'Add the delete sub to the Word Document
lngLineCtr = 2
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, "Public sub DeleteAcc(strYear As String, strAccNbr As String)" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, "Dim conOR as New ADODB.Connection" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, "Dim conSQL as New ADODB.Connection" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, "Dim intResponse As Integer" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, "Dim lngRecAff As Long" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, "Dim rsOR As New ADODB.Recordset" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, "Dim strFullAccNo As String" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, "Dim strShortAccNo as String" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " intResponse = MsgBox(""Are you certain you wish to delete accident"" & vbNewLine & vbNewLine & "" "" & strYear & strAccNbr & vbNewLine & vbNewLine & "" from KARS and FILENET?"", vbYesNo, ""DELETE ACCIDENT?""

"
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " Select Case intResponse"
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " Case vbNo"
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " Exit Sub"
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " End Select"
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " conOR.Open ""Provider=MSDAORA.1;Password=kn0b23;User ID=KARS;Data Source=KARSTEST;Persist Security Info=True""" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " rsOR.Open ""Select ACCIDENT_KEY from ACCIDENTS where ACCIDENT_KEY LIKE '""" & " & strYear & strAccNbr &" & """%'"",conOR,adOpenDynamic,adLockOptimistic" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " Select Case rsOR.EOF" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " Case True" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " MsgBox ""Accident Number "" & strYear & strAccNbr & ""? not found in the KARS Database."", vbOKOnly, ""ACCIDENT NOT FOUND""" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " Case False" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " strFullAccNo = rsOR.Fields(""ACCIDENT_KEY""

" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " strShortAccNo = Left(strFullAccNo,11)" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " conOR.Execute ""Delete from ACCIDENTS where ACCIDENT_KEY = '"" & strFullAccNo & ""'""" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " conOR.Execute ""Commit""" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " conSQL.Open ""Provider=SQLOLEDB.1;Password=gad12;Persist Security Info=True;User ID=gaduser;Initial Catalog=Accidents;Data Source=DT00MH25""" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " conSQL.Execute ""Update V_FN_ACCIDENTS Set DEL_DUP = 1 Where ACCIDENT_ID = '"" & strShortAccNo & ""'"", lngRecAff" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " conSQL.Close" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " Set conSQL = Nothing" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " Select Case lngRecAff"
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " Case 0"
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " Application.Selection.InlineShapes(1).Delete"
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " Application.Selection.InsertAfter ""NO IMAGE """
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " Application.ScreenRefresh"
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " Case Else"
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " Application.Selection.InlineShapes(1).Delete"
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " Application.Selection.InsertAfter ""DELETED """
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " Application.ScreenRefresh"
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " End Select"
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " End Select" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " rsOR.Close" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " conOR.Close" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " Set rsOR = nothing" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, " Set conOR = nothing" & vbCrLf
lngLineCtr = lngLineCtr + 1
VBComp.CodeModule.InsertLines lngLineCtr, "End sub"
'Add a sub to add ADO reference to the NewMod Module
strRefADO = "Public Sub AddRef()" & vbCrLf & _
"Dim doc As Document" & vbCrLf & _
" Set doc = ActiveDocument" & vbCrLf & _
" doc.VBProject.References.AddFromFile ""C:\PROGRAM FILES\COMMON FILES\SYSTEM\ADO\MSADO15.DLL""" & vbCrLf & _
" Set doc = Nothing" & vbCrLf & _
"End Sub"
VBComp.CodeModule.AddFromString strRefADO
'Run the AddRef Sub to add ADO reference to the Word Application
appW.Run "AddRef"
'Bypass (FF) at start of file
Line Input #1, strIn
Do While Not EOF(1)
Line Input #1, strIn
intLen = Len(strIn)
strIn = Replace(strIn, Chr(13), ""

strIn = Replace(strIn, Chr(10), ""

Select Case Left(strIn, 2)
Case "D1"
'Add the row of information and the <cr>
rng.InsertAfter Chr(13)
rng.InsertAfter " " & Right(strIn, Len(strIn) - 2)
rng.InsertAfter Chr(13)
'Delete 10 characters (spaces) from the area after the Accident Number
Set rngDel = appW.ActiveDocument.Sentences.Last.Words(7)
rngDel.Start = rngDel.Start + 10
rngDel.End = rngDel.Start
rngDel.Delete wdCharacter, 10
'Add the delete button (must be done BEFORE the hyperlink)
strWord = Trim(appW.ActiveDocument.Sentences.Last.Words(7).Text)
Set rngBtn = appW.ActiveDocument.Sentences.Last.Words(7)
dblWordStart = rngBtn.Start + 10
rngBtn.Start = dblWordStart
rngBtn.End = rngBtn.Start
Set shp1 = doc.Content.InlineShapes.AddOLEControl(ClassType:="forms.commandbutton.1", Range:=rngBtn)
shp1.OLEFormat.Object.Caption = "Delete"
shp1.Height = 7
shp1.Width = 47
shp1.AlternativeText = strWord
'Convert the Accident Number into a hyperlink
strWord = Trim(appW.ActiveDocument.Sentences.Last.Words(7).Text)
intLen = Len(Trim(strWord))
dblWordStart = appW.ActiveDocument.Sentences.Last.Words(7).Start
Set rngAccNo = appW.ActiveDocument.Sentences.Last.Words(7)
rngAccNo.End = dblWordStart + intLen
strAddr = SetupAddr(Left(strWord, 7))
doc.Hyperlinks.Add rngAccNo, strAddr
'Add a procedure for the click event of the inlineshape for D1 record
'**Note: The click event resides in the This Document module
strWord = Left(strWord, 7)
sCode1 = "Private Sub " & shp1.OLEFormat.Object.Name & "_Click()" & vbCrLf & _
" CONST strAccNo = """ & strWord & """" & vbCrLf & _
" CONST strYear = """ & strDBYear & """" & vbCrLf & _
" Call DeleteAcc(strYear, strAccNo)" & vbCrLf & _
"End Sub"
doc.VBProject.VBComponents("ThisDocument"

.CodeModule.AddFromString sCode1
Case "D2"
'Add the row of information and the <cr>
rng.InsertAfter " " & Right(strIn, Len(strIn) - 2)
rng.InsertAfter Chr(13)
'Delete 10 characters (spaces) from the area after the Accident Number
Set rngDel = appW.ActiveDocument.Sentences.Last.Words(6)
rngDel.Start = rngDel.Start + 10
rngDel.End = rngDel.Start
rngDel.Delete wdCharacter, 10
'Add the delete button (must be done BEFORE the hyperlink)
strWord = Trim(appW.ActiveDocument.Sentences.Last.Words(6).Text)
Set rngBtn = appW.ActiveDocument.Sentences.Last.Words(6)
dblWordStart = rngBtn.Start + 10
rngBtn.Start = dblWordStart
rngBtn.End = rngBtn.Start
Set shp1 = doc.Content.InlineShapes.AddOLEControl(ClassType:="forms.commandbutton.1", Range:=rngBtn)
shp1.OLEFormat.Object.Caption = "TestDelete"
shp1.Height = 7
shp1.Width = 47
shp1.AlternativeText = strWord
'Convert the Accident Number into a hyperlink
strWord = appW.ActiveDocument.Sentences.Last.Words(6).Text
intLen = Len(Trim(strWord))
dblWordStart = appW.ActiveDocument.Sentences.Last.Words(6).Start
Set rngAccNo = appW.ActiveDocument.Sentences.Last.Words(6)
rngAccNo.End = dblWordStart + intLen
strAddr = SetupAddr(Left(strWord, 7))
doc.Hyperlinks.Add rngAccNo, strAddr
'Add a procedure for the click event of the inlineshape for D2 record
'**Note: The click event resides in the This Document module
strWord = Left(strWord, 7)
sCode1 = "Private Sub " & shp1.OLEFormat.Object.Name & "_Click()" & vbCrLf & _
" CONST strAccNo = """ & strWord & """" & vbCrLf & _
" CONST strYear = """ & strDBYear & """" & vbCrLf & _
" Call DeleteAcc(strYear, strAccNo)" & vbCrLf & _
"End Sub"
doc.VBProject.VBComponents("ThisDocument"

.CodeModule.AddFromString sCode1
Case "E "
'E record gets hyperlink only because it was not added to the database due
'to constraint violation
rng.InsertAfter Chr(13)
rng.InsertAfter " " & Right(strIn, Len(strIn) - 2)
rng.InsertAfter Chr(13)
strWord = appW.ActiveDocument.Sentences.Last.Words(2).Text
intLen = Len(Trim(strWord))
dblWordStart = appW.ActiveDocument.Sentences.Last.Words(2).Start
Set rngAccNo = appW.ActiveDocument.Sentences.Last.Words(2)
rngAccNo.End = dblWordStart + intLen
strAddr = SetupAddr(Left(strWord, 7))
appW.ActiveDocument.Hyperlinks.Add rngAccNo, strAddr
Case Else
rng.InsertAfter strIn
rng.InsertAfter Chr(13)
End Select
Loop
Set rngDel = Nothing
Set shp1 = Nothing
Set VBComp = Nothing
End Sub
Private Function SetupAddr(FNAccNo As String) As String
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strLibId As String
Dim strSQL As String
Dim strPath As String
con.Open "Provider=SQLOLEDB.1;Password=gad12;Persist Security Info=True;User ID=gaduser;Initial Catalog=Accidents;Data Source=DT00MH25"
strSQL = "Select LIB_ID FROM V_FN_ACCIDENTS WHERE ACCIDENT_ID = '" & strDBYear & FNAccNo & "'"
rs.Open strSQL, con, adOpenDynamic, adLockOptimistic
strPath = "
strPath = strPath & rs.Fields("LIB_ID"
SetupAddr = strPath
rs.Close
con.Close
Set con = Nothing
Set rs = Nothing
End Function