作成、保存、印刷、および削除されるファイルが多数あります。私はそれらを開きません。削除する最後のファイルは別のプロセスによって保持されているため、削除できません。
これはエラーメッセージです:
別のプロセスで使用されているため、プロセスはファイル '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