WellsCarrie
Well-known member
Ok my code works great if you have debug points established in it. But should you stop the debugging and just try to run it as production code is sits and does nothing.
Perhapse one of you can take a look and see what I've done to cause this.
I'm really at a loss...
Oh and why doesn't my "WINWORD" exe ever close? Even running through the OnStop doesn't kill it. it just sits in the processes until I reboot.
Perhapse one of you can take a look and see what I've done to cause this.
VB.NET:
Dim ohioConn As New OracleConnection("Data Source=Ohio.world;User Id=me;Password=pw;")
Dim ohioCmd As New OracleCommand
Dim reader As OracleDataReader
Dim str As String
Dim sb As New System.Text.StringBuilder
Dim WordApp As New Word.Application
Dim wdShape As Word.InlineShape
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
If Not EL1.SourceExists("DragTag") Then
EL1.CreateEventSource("DragTag", "Drag Tag Log") ' Create Log
End If
EL1.Source = "DragTag"
'EL1.WriteEntry("DragTag", "Starting Drag Tag File Watcher. ")
WordApp.Visible = True
WordApp.WindowState = Word.WdWindowState.wdWindowStateNormal '.wdWindowStateMinimize
'WordApp.Documents.Open("c:\WatchFiles\Template\DragTag.dot")
Timer1.Interval = 15000
Timer1.Enabled = True
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
'//Release class level objects to garbage collector
reader.Dispose()
ohioCmd.Dispose()
ohioConn.Dispose()
str = Nothing
sb = Nothing
WordApp.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
WordApp.Quit()
WordApp = Nothing
wdShape = Nothing
Dim myProcesses() As Process
Dim myProcess As Process
' Returns array containing all instances of "WINWORD".
myProcesses = Process.GetProcessesByName("WINWORD")
For Each myProcess In myProcesses
myProcess.Kill()
Next
myProcesses = Nothing
myProcess = Nothing
If EL1.SourceExists("DragTag") Then
EL1.DeleteEventSource("DragTag") 'Delete Log
End If
End Sub
Public Sub CheckForFiles()
Dim di As New IO.DirectoryInfo("c:\WatchFiles\")
Dim fiGetFiles As IO.FileInfo() = di.GetFiles("*.csv")
Dim fi As IO.FileInfo
Try
For Each fi In fiGetFiles
DoProcess(fi.Name)
Next
Catch ex As Exception
sb.Remove(0, sb.Length)
sb.Append("Sub CheckForFiles - ")
sb.Append(ex.Source)
sb.Append(" - ")
sb.Append(ex.Message)
EL1.WriteEntry("DragTag", sb.ToString())
Finally
'// Clean up our mess.
di = Nothing
fiGetFiles = Nothing
fi = Nothing
End Try
End Sub
Private Sub DoProcess(ByVal file As String)
Dim oFile As IO.File
Dim oRead As IO.StreamReader
Dim bol As Boolean = True
Try
'Open the file for read
oRead = oFile.OpenText("c:\WatchFiles\" & file)
If oRead.Peek() <> -1 Then
While oRead.Peek() <> -1
oRead.ReadLine()
If bol Then
PrintDocument(BuildData(oRead.ReadLine()))
bol = False
End If
End While
End If
oRead.Close()
' Send file onto next processing point
If oFile.Exists("c:\WatchFiles\" & file) Then
oFile.Move("c:\WatchFiles\" & file, "c:\WatchFiles\ProcessedCSVs\" & file)
End If
Catch ex As Exception
sb.Remove(0, sb.Length)
sb.Append("Sub DoProcess- ")
sb.Append(ex.Source)
sb.Append(" - ")
sb.Append(ex.Message)
EL1.WriteEntry("DragTag", sb.ToString())
Finally
oFile = Nothing
oRead = Nothing
bol = Nothing
sb = Nothing
End Try
End Sub
Private Function BuildData(ByVal Value As String) As String()
Dim info As String()
Dim theInfo(20) As String
Dim SearchNumber As String
Dim temp As String = ""
Try
info = Split(Value, ",")
temp = Replace(Trim(info(65)), """", "") & " " & _
Replace(Trim(info(66)), """", "") & " " & _
Replace(Trim(info(67)), """", "") & " " & _
Replace(Trim(info(68)), """", "") & " " & _
Replace(Trim(info(69)), """", "") & " " & _
Replace(Trim(info(70)), """", "") & ", " & _
Replace(Trim(info(71)), """", "") & " " & _
Replace(Trim(info(72)), """", "")
theInfo(0) = Replace(Trim(temp), " ", " ") 'DockAddress
temp = ""
theInfo(1) = Replace(Trim(info(9)), """", "") 'ShipName
theInfo(2) = Replace(Trim(info(11)), """", "") 'ShipAddress
theInfo(3) = Replace(Trim(info(14)), """", "") & ", " & _
Replace(Trim(info(15)), """", "") 'ShipCityState
theInfo(4) = Replace(Trim(info(16)), """", "") 'ShipZip
If Replace(Trim(info(55)), """", "") = "WMIT" Then
temp = Replace(Trim(info(12)), """", "")
theInfo(5) = "DC#" 'DCLable
theInfo(6) = "TYPE" 'TypeLable
theInfo(7) = "DEPT" 'Deptlable
theInfo(8) = Mid(temp, 1, 5) 'ShipDCNum
theInfo(9) = Mid(temp, 6, 4) 'ShipType
theInfo(10) = Mid(temp, 10, 5) 'ShipDept
theInfo(14) = "WMIT" 'WMCode
Else
theInfo(5) = "" 'DCLable
theInfo(6) = "" 'TypeLable
theInfo(7) = "" 'Deptlable
theInfo(8) = "" 'ShipDCNum
theInfo(9) = "" 'ShipType
theInfo(10) = "" 'ShipDept
theInfo(14) = "" 'WMCode
End If
theInfo(11) = Replace(Trim(info(3)), """", "") 'ShipSKU
theInfo(12) = "" 'TMPArrival
theInfo(13) = Trim(Replace(info(30), """", "")) 'Carrier
theInfo(15) = Trim(Replace(info(52), """", "")) 'CustomerPO
theInfo(16) = Trim(Replace(info(44), """", "")) 'OrderNumber
temp = Trim(Replace(info(28), """", ""))
temp = CType((Math.Round(CType(temp, Double), 0) + 62), String)
theInfo(17) = CType(temp, String) 'PalletWeight
temp = Replace(info(35), """", "")
If Len(Trim(temp)) = 0 Or Trim(temp) = "0" Then
theInfo(18) = Trim(Replace(info(33), """", "")) 'MastContNum
Else
theInfo(18) = Trim(Replace(info(35), """", "")) 'MastContNum
End If
theInfo(19) = "" 'PrinterName
temp = Replace(info(44), """", "")
'System.Diagnostics.Debugger.Break()
str = "SELECT sch_shp_dt FROM [EMAIL="outordhdr@main"]outordhdr@main[/EMAIL] " & _
"WHERE ord_num = '" & Trim(temp) & _
"' UNION " & _
"SELECT sch_shp_dt FROM [EMAIL="outordhdr@deal"]outordhdr@deal[/EMAIL] " & _
"WHERE ord_num = '" & Trim(temp) & "'"
'System.Diagnostics.Debugger.Break()
ohioConn.Open()
ohioCmd.Connection = ohioConn
ohioCmd.CommandText = str
reader = ohioCmd.ExecuteReader()
If reader.HasRows() Then
While reader.Read()
If Len(Trim(CType(reader(0), String))) > 0 Then
theInfo(12) = CType(reader(0), String) 'TMPArrival
End If
End While
Else
theInfo(12) = "" 'TMPArrival
End If
reader.Close()
str = "SELECT printer_name FROM drag_tag_printers_tbl " & _
"WHERE printer = " & Replace(Trim(info(1)), """", "")
ohioCmd.Connection = ohioConn
ohioCmd.CommandText = str
reader = ohioCmd.ExecuteReader()
If reader.HasRows() Then
While reader.Read()
theInfo(19) = CType(reader(0), String) 'PrinterName
End While
Else
theInfo(19) = "nlrmis1 on may_printserver" 'PrinterName
End If
reader.Close()
Catch ex As Exception
sb.Remove(0, sb.Length)
sb.Append("Funciton BuildData - ")
sb.Append(ex.Source)
sb.Append(" - ")
sb.Append(ex.Message)
EL1.WriteEntry("DragTag", sb.ToString())
Finally
If ohioConn.State = ConnectionState.Open Then
ohioConn.Close()
End If
End Try
Return theInfo
End Function
Private Sub PrintDocument(ByVal theInfo As String())
Try
'System.Diagnostics.Debugger.Break()
'Send info for printing
With WordApp
.Documents.Open("c:\WatchFiles\Template\DragTag.dot", False)
'System.Diagnostics.Debugger.Break()
.ActiveDocument.Bookmarks.Item("DockAddress").Select()
.Selection.Text = theInfo(0)
.ActiveDocument.Bookmarks.Item("ShipName").Select()
.Selection.Text = theInfo(1)
.ActiveDocument.Bookmarks.Item("ShipAddress").Select()
.Selection.Text = theInfo(2)
.ActiveDocument.Bookmarks.Item("ShipCityState").Select()
.Selection.Text = theInfo(3)
.ActiveDocument.Bookmarks.Item("ShipZip").Select()
.Selection.Text = theInfo(4)
.ActiveDocument.Bookmarks.Item("DCLable").Select()
.Selection.Text = theInfo(5)
.ActiveDocument.Bookmarks.Item("TypeLable").Select()
.Selection.Text = theInfo(6)
.ActiveDocument.Bookmarks.Item("DeptLable").Select()
.Selection.Text = theInfo(7)
.ActiveDocument.Bookmarks.Item("ShipDCNum").Select()
.Selection.Text = theInfo(8)
.ActiveDocument.Bookmarks.Item("ShipType").Select()
.Selection.Text = theInfo(9)
.ActiveDocument.Bookmarks.Item("ShipDept").Select()
.Selection.Text = theInfo(10)
.ActiveDocument.Bookmarks.Item("ShipSku").Select()
.Selection.Text = theInfo(11)
.ActiveDocument.Bookmarks.Item("ArrivalDate").Select()
.Selection.Text = theInfo(12)
.ActiveDocument.Bookmarks.Item("Carrier").Select()
.Selection.Text = theInfo(13)
.ActiveDocument.Bookmarks.Item("WMCode").Select()
.Selection.Text = Trim(theInfo(14))
.ActiveDocument.Bookmarks.Item("CustomerPO").Select()
.Selection.Text = theInfo(15)
.ActiveDocument.Bookmarks.Item("OrderNum").Select()
.Selection.Text = theInfo(16)
.ActiveDocument.Bookmarks.Item("PalletWeight").Select()
.Selection.Text = theInfo(17)
'//Write Master Container Number as Barcode
For Each wdShape In .ActiveDocument.InlineShapes
With wdShape.OLEFormat.Object
If (StrComp(.Name, "TBarCode31", vbTextCompare) = 0) Then
.LicenseMe("Maybelline, Inc.", 3, 1, "CE7151AB", 5)
.Text = theInfo(18)
If (Len(Trim(theInfo(18))) = 0) Then
.BarCode = 0
Else
.BarCode = 33
End If
Exit For
End If
End With
Next wdShape
'Print file
.WordBasic.FilePrintSetup(Printer:=theInfo(19), DoNotSetAsSysDefault:=1)
.PrintOut(Copies:=2, BackGround:=True)
'Wait for document to print
Do While .BackgroundPrintingStatus > 0
'Loop until documents have printed then continue
Loop
.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
End With
Catch ex As Exception
sb.Remove(0, sb.Length)
sb.Append("Sub PrintDocument - ")
sb.Append(ex.Source)
sb.Append(" - ")
sb.Append(ex.Message)
EL1.WriteEntry("DragTag", sb.ToString())
End Try
End Sub
Private Sub Timer1_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
'Pause timer
Timer1.Enabled = False
CheckForFiles()
Timer1.Enabled = True
End Sub
I'm really at a loss...
Oh and why doesn't my "WINWORD" exe ever close? Even running through the OnStop doesn't kill it. it just sits in the processes until I reboot.