我有许多文件被创建、保存、打印然后删除。我不打开它们。最后要删除的文件由另一个进程持有,因此无法删除。
这是错误消息:
该进程无法访问文件“C:\Temp\34_Temp.doc”,因为它正被另一个进程使用。
这是我保存文件的地方。我使用第三方控件(用于 .NET 的 Spire .doc)来创建文档。保存后也会关闭。
Dim tFname As String = My.Application.Info.DirectoryPath & "\" & Now.ToString("hhmmss") & g.ToString() & "_Temp.doc"
OrderDetailsDoc.SaveToFile(tFname, Spire.Doc.FileFormat.Doc)
OrderDetailsDoc.Close()
SendMetoPrint(tFname)
OrderDetailsDoc = Nothing
这是打印程序:
Public Sub SendMetoPrint(ByVal tmpFilename As String)
Const csSUBNAME As String = "SendMetoPrint"
On Error GoTo Errorhandler
Using dialog As New PrintDialog()
Dim d As New Spire.Doc.Document(tmpFilename)
d.PrintDialog = dialog
dialog.Document = d.PrintDocument
dialog.AllowCurrentPage = True
dialog.AllowSomePages = True
dialog.UseEXDialog = False
dialog.PrintToFile = False
dialog.Document.Print()
d.Close()
d.PrintDocument.Dispose()
d = Nothing
End Using
End Sub
这是我用来删除文件的。线程睡眠没有帮助。我希望这会给流程时间来发布文件,但事实并非如此。任何帮助,将不胜感激。
Public Sub ClearFolder(ByVal dirString As String)
Dim foundFile As String
Dim i As Integer
For Each foundFile In My.Computer.FileSystem.GetFiles(dirString, FileIO.SearchOption.SearchTopLevelOnly, "*Temp.doc")
If My.Computer.FileSystem.FileExists(foundFile) Then
For i = 1 To 10
Try
My.Computer.FileSystem.DeleteFile(foundFile)
Exit For
Catch ex As Exception
If i = 10 Then
Logit(Now.ToString("MM/dd/yyyy hh:mm:ss tt") & " " & csSUBNAME)
Logit(Now.ToString("MM/dd/yyyy hh:mm:ss tt") & " Unable to delete file " & foundFile & " after 10 tries over 10 seconds.")
Logit(Now.ToString("MM/dd/yyyy hh:mm:ss tt") & " Exception says: " & ex.Message)
Else
Threading.Thread.Sleep(1000)
End If
End Try
Next
End If
Next
Exit Sub
End Sub
这是调用打印多个订单的子程序。屏幕包含要打印的订单列表。
需要循环遍历每个订单以获取订单详细信息:
Public Sub PrintAllOrders(Optional ByVal printbatchnum As Integer = 0)
Const csSUBNAME As String = "PrintAllOrders"
On Error GoTo ErrorHandler
Dim objPrintPick As clsPickScreen
Dim argWorkObject As clsWorkOrder
Dim strErrorLocation As String = ""
Dim pbn As Integer
Dim g As Integer = 0
Dim rsBatchReprint As ADODB.Recordset
Dim OrderDetailsDoc As New Spire.Doc.Document
frmPickScreen.PickPrint = True
pbn = GeneratePrtBatchNum()
strErrorLocation = "Just generated Print Batch Number"
For Each objPrintPick In objTotalPickScreen
blnRedPrint = objPrintPick.AlreadyPrinted
If objPrintPick.printed = False Then
objPrintPick.printed = True
g = g + 1
argWorkObject = New clsWorkOrder
argWorkObject.Connection = conSQLDatabase
argWorkObject.UserID = gstrUserID
argWorkObject.Role = gconPICKING_ROLE
argWorkObject.OrderID = objPrintPick.OrderID
argWorkObject.SubOrderID = objPrintPick.SubOrderID
argWorkObject.SubOrderPickScrnStatus = CShort(objPrintPick.Status)
argWorkObject.StoreNumber = objPrintPick.FulfillStore
argWorkObject.LoadWorkOrder()
argWorkObject.PrintBatchNumber = pbn
Call SpirePrintOrder(argWorkObject, (gobjOptions.PrintMultiCopies), OrderDetailsDoc)
InsertNewPage(OrderDetailsDoc)
End If
If g Mod 10 = 0 And g > 0 Then
Dim tFname As String = My.Application.Info.DirectoryPath & "\" & Now.ToString("hhmmss") & g.ToString() & "_Temp.doc"
OrderDetailsDoc.SaveToFile(tFname, Spire.Doc.FileFormat.Doc)
OrderDetailsDoc.Close()
SendMetoPrint(tFname)
'System.Diagnostics.Process.Start(tFname)
OrderDetailsDoc = Nothing
OrderDetailsDoc = New Spire.Doc.Document
End If
Next objPrintPick
If g = 0 Then
MsgBox("There are no unprinted orders.")
frmPickScreen.cmdPrintDialog.PrinterSettings.Copies = 1
frmPickScreen.cmdPrintDialog.PrinterSettings.DefaultPageSettings.Landscape = False
Else
strErrorLocation = "About to Send Order to Printer - Outside Loop"
Dim tFname As String = My.Application.Info.DirectoryPath & "\" & Now.ToString("hhmmss") & g.ToString() & "_Temp.doc"
OrderDetailsDoc.SaveToFile(tFname, Spire.Doc.FileFormat.Doc)
OrderDetailsDoc.Close()
SendMetoPrint(tFname)
OrderDetailsDoc = Nothing
'System.Diagnostics.Process.Start(tFname)
End If
End Sub
Public Sub SpirePrintOrder(ByRef argWorkObject As clsWorkOrder, ByRef argblnPrintMultiple As Boolean, ByRef OrderDetailsDoc As Document)
Dim intPageNum As Integer
Dim intLinesPrinted As Integer
Dim strErrorLocation As String
Dim x As Integer
Dim blnSpecItem As Boolean
Dim a As Integer
Dim g As Integer
Dim lngCountofDepts As Integer
Dim strPrintDate As String
Dim strDescPrint As String = ""
Dim boolPickPrint As Boolean = frmPickScreen.PickPrint
Dim prodPara As Paragraph
Dim tmpSec As Section
On Error GoTo ErrorHandler
Dim dialog As New PrintDialog()
dialog.AllowCurrentPage = True
dialog.AllowSomePages = True
dialog.UseEXDialog = True
strErrorLocation = "preparing to print PrintOrder"
lngCountofDepts = 0
strPrintDate = CStr(Now)
dialog = frmPickScreen.cmdPrintDialog
intNumCopies = dialog.PrinterSettings.Copies
lngCountofDepts = GetNumberOfDepts((argWorkObject.SubOrderID), (argWorkObject.StoreNumber))
For g = 1 To lngCountofDepts
If g > 1 Then
InsertNewPage(OrderDetailsDoc)
End If
For a = 1 To intNumCopies
Logit(Now.ToString("MM/dd/yy hh:mm:ss") & " Number of copies is " & intNumCopies.ToString())
intPageNum = 1
iPageLineCounter = 0
SpirePageHeader(OrderDetailsDoc, argWorkObject, intPageNum, True, False, argblnPrintMultiple, g, lngCountofDepts, strPrintDate)
SpireStaticHeader(OrderDetailsDoc)
For x = 1 To argWorkObject.AllOrderItems.Count
If iPageLineCounter > 35 Then
intPageNum += 1
InsertNewPage(OrderDetailsDoc)
iPageLineCounter = 0
SpirePageHeader(OrderDetailsDoc, argWorkObject, intPageNum, True, False, argblnPrintMultiple, g, lngCountofDepts, strPrintDate)
SpireStaticHeader(OrderDetailsDoc)
End If
blnSpecItem = False
If argWorkObject.AllOrderItems.Item(x).IsGiftWrap = True Or argWorkObject.AllOrderItems.Item(x).MfgRebate = True Then
blnSpecItem = True
End If
strDescPrint = Trim(Left(argWorkObject.AllOrderItems.Item(x).ItemDescription, 65))
If blnSpecItem = True Then
strDescPrint = "(SPC ITEM) " & Left(strDescPrint, 54)
End If
If blnRedPrint = True Then
If argWorkObject.AllOrderItems.Item(x).printed = False Then
strDescPrint = "(NEW ITEM) " & Trim(Left(strDescPrint, 54))
End If
End If
'Pad all fields displayed on the page for astetic appearance
padProduct(argWorkObject, x)
addOrderData(argWorkObject.AllOrderItems.Item(x).ImagePathName, OrderDetailsDoc, strDescPrint)
If argWorkObject.AllOrderItems.Item(x).MfgRebate = True Then
tmpSec = OrderDetailsDoc.Sections.LastItem
prodPara = tmpSec.Paragraphs(0)
prodPara.AppendText(Space(5) & argWorkObject.AllOrderItems.Item(x).MfgRebateMsg & vbCrLf).CharacterFormat.Font = myCNReg12Font
prodPara.AppendText("---------------------------------------------------------------------------------------------------------------" & vbCrLf).CharacterFormat.Font = myCNReg12Font
iPageLineCounter += 2
End If
Next
If UCase(Trim(argWorkObject.ShippingMethod)) = "UPS EXPEDITED" Then
If iPageLineCounter > 35 Then
SpirePageHeader(OrderDetailsDoc, argWorkObject, intPageNum, True, False, argblnPrintMultiple, g, lngCountofDepts, strPrintDate)
End If
tmpSec = OrderDetailsDoc.Sections.LastItem
prodPara = tmpSec.Paragraphs(0)
prodPara.AppendText(" " & vbCrLf)
prodPara.AppendText(" " & vbCrLf)
prodPara.AppendText(Space(31) & "***************************************************" & vbCrLf).CharacterFormat.Font = myCNBold12Font
prodPara.AppendText(Space(31) & "* Customer is paying a premium for RUSH service *" & vbCrLf).CharacterFormat.Font = myCNBold12Font
prodPara.AppendText(Space(31) & "* Please prioritize Picking & Packing *" & vbCrLf).CharacterFormat.Font = myCNBold12Font
prodPara.AppendText(Space(31) & "* Ship UPS Second Day Air *" & vbCrLf).CharacterFormat.Font = myCNBold12Font
prodPara.AppendText(Space(31) & "***************************************************" & vbCrLf).CharacterFormat.Font = myCNBold12Font
End If
If argWorkObject.Comments.CommentsExist = True Then
Call SprireComments(OrderDetailsDoc, argWorkObject, intPageNum, iPageLineCounter, argblnPrintMultiple, g, lngCountofDepts, strPrintDate)
End If
Next
Next
End Sub