I have a macro that has been working for about 3 years, but I am now occasionally getting a bad record length error. The process starts in a Visual Basic application that sends information to a text file that is opened using the Open statement in VBA and then reads the data records. The data type is defined in the Declarations area of the procedure as follows:
Private Type ChgOrd
ChgOrderNo As String * 3
CostCode As String * 6
Phs As String * 5
Phase As String * 3
ChgOrderType As String * 6
CurContractAmt As Double
ChgOrderAmt As Double
DaysExtend As String * 2
CostType As String * 1
CENum As String * 5
CEDesc As String * 50
ContractNum As String * 11
PCCType As Long
PCC As Boolean
FilePath As String * 100
OwnerRefNo As String * 6
SubPropNo As String * 6
End Type
Public COFile As ChgOrd
Later I use this statement to open the file, read the records and place the information on the document being created from the template. The problem is that when the code comes to the first Get statement, it sometimes tells me that it is a bad record length even though the record length is the same as the defined variable.
Dim intCount As Integer
Dim Count As Integer
intFN = FreeFile
strFile = Dir("c:\Temp\" + strCOFile)
intCount = 1
totCOAmt = 0
If strFile = strCOFile Then
strFile = "c:\Temp\" + strCOFile
Open strFile For Random As #intFN Len = Len(COFile)
Get #intFN, intCount, COFile
'Check to see if the document was created before and open the old document to update if it was.
Dim OldDocument As String
Dim Path As String
CurContractAmt = Trim(COFile.CurContractAmt)
tmpContractNo = Trim(COFile.ContractNum)
tmpCONo = Trim(COFile.ChgOrderNo)
Path = "o:\contracts\" + tmpJobNo + "\"
OldDocument = tmpContractNo + "CO" + tmpCONo
With Application.FileSearch
.FileName = OldDocument
.LookIn = Path
.Execute
found = .FoundFiles.Count
If found = 1 Then
Documents.Open FileName:=Path + OldDocument
OldDoc = ActiveDocument.Name
Documents(CurrentDocument).Activate
ActiveDocument.Close wdDoNotSaveChanges
Documents(OldDoc).Activate
CurrentDocument = ActiveDocument.Name
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:="co"
End If
' ActiveDocument.Unprotect Password:="co"
tmpOldCOAmt = ActiveDocument.Variables("varChangeOrderValue"
With Selection
'Go to the CE bookmark and delete all the previously inserted change order info
.GoTo what:=wdGoToBookmark, Name:="CE"
.Find.ClearFormatting
.MoveUp Unit:=wdLine, Count:=1
.SelectRow
.Copy
.Tables(1).Select
.Delete Unit:=wdCharacter, Count:=1
.Paste
.SelectRow
.Rows.Delete
'Go to the Phase bookmark and delete all the previously inserted phase info
.GoTo what:=wdGoToBookmark, Name:="Phase"
.Find.ClearFormatting
.MoveUp Unit:=wdLine, Count:=1
.SelectRow
.Copy
.Tables(1).Select
.Delete Unit:=wdCharacter, Count:=1
.Paste
.SelectRow
.Rows.Delete
End With
End If
End With
With ActiveDocument
.Variables("varTradeItem"
= IIf(Len(Mid(COFile.ContractNum, 6, 4)) = 0, " ", Mid(COFile.ContractNum, 6, 4))
.Variables("varCostCode"
= IIf(Len(Trim(COFile.CostCode)) = 0, " ", Trim(COFile.CostCode))
' .Variables("varContractNo"
= IIf(Len(Trim(COFile.ContractNum)) = 0, " ", Trim(COFile.ContractNum))
.Variables("varCONo"
= IIf(Len(Trim(COFile.ChgOrderNo)) = 0, " ", Trim(COFile.ChgOrderNo))
.Variables("varExtension"
= IIf(Len(Trim(COFile.DaysExtend)) = 0, " ", Asc(COFile.DaysExtend))
.Variables("varOldContractValue"
= IIf(Len(Trim(COFile.CurContractAmt)) = 0, " ", Trim(COFile.CurContractAmt))
End With
tmpPCC = COFile.PCC
Selection.GoTo what:=wdGoToBookmark, Name:="CE"
While Not EOF(intFN)
Selection.TypeText Text:=UCase(Trim(COFile.Phase))
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=Trim(COFile.CENum)
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=Trim(COFile.OwnerRefNo)
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=Trim(COFile.SubPropNo)
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=Trim(COFile.CEDesc)
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=Trim(COFile.ChgOrderType)
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=Format(Trim(COFile.ChgOrderAmt), "$#,##0.00"
Selection.MoveRight Unit:=wdCell
If COFile.Phase <> tmpPhase Then
tmpPhase = COFile.Phase
Count = Count + 1
End If
If Count > 1 Then
tmpPhase = "MULTI"
Else
tmpPhase = COFile.Phase
End If
totCOAmt = totCOAmt + Trim(COFile.ChgOrderAmt)
intCount = intCount + 1
Get #intFN, intCount, COFile
Wend
Close #intFN
Else
MsgBox "Error #2, Cannot find path to original contract. Try re-running the original contract " _
+ "and rerunning the Change Order", vbOKOnly + vbExclamation, "Error Message"
End
End If
Private Type ChgOrd
ChgOrderNo As String * 3
CostCode As String * 6
Phs As String * 5
Phase As String * 3
ChgOrderType As String * 6
CurContractAmt As Double
ChgOrderAmt As Double
DaysExtend As String * 2
CostType As String * 1
CENum As String * 5
CEDesc As String * 50
ContractNum As String * 11
PCCType As Long
PCC As Boolean
FilePath As String * 100
OwnerRefNo As String * 6
SubPropNo As String * 6
End Type
Public COFile As ChgOrd
Later I use this statement to open the file, read the records and place the information on the document being created from the template. The problem is that when the code comes to the first Get statement, it sometimes tells me that it is a bad record length even though the record length is the same as the defined variable.
Dim intCount As Integer
Dim Count As Integer
intFN = FreeFile
strFile = Dir("c:\Temp\" + strCOFile)
intCount = 1
totCOAmt = 0
If strFile = strCOFile Then
strFile = "c:\Temp\" + strCOFile
Open strFile For Random As #intFN Len = Len(COFile)
Get #intFN, intCount, COFile
'Check to see if the document was created before and open the old document to update if it was.
Dim OldDocument As String
Dim Path As String
CurContractAmt = Trim(COFile.CurContractAmt)
tmpContractNo = Trim(COFile.ContractNum)
tmpCONo = Trim(COFile.ChgOrderNo)
Path = "o:\contracts\" + tmpJobNo + "\"
OldDocument = tmpContractNo + "CO" + tmpCONo
With Application.FileSearch
.FileName = OldDocument
.LookIn = Path
.Execute
found = .FoundFiles.Count
If found = 1 Then
Documents.Open FileName:=Path + OldDocument
OldDoc = ActiveDocument.Name
Documents(CurrentDocument).Activate
ActiveDocument.Close wdDoNotSaveChanges
Documents(OldDoc).Activate
CurrentDocument = ActiveDocument.Name
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:="co"
End If
' ActiveDocument.Unprotect Password:="co"
tmpOldCOAmt = ActiveDocument.Variables("varChangeOrderValue"
With Selection
'Go to the CE bookmark and delete all the previously inserted change order info
.GoTo what:=wdGoToBookmark, Name:="CE"
.Find.ClearFormatting
.MoveUp Unit:=wdLine, Count:=1
.SelectRow
.Copy
.Tables(1).Select
.Delete Unit:=wdCharacter, Count:=1
.Paste
.SelectRow
.Rows.Delete
'Go to the Phase bookmark and delete all the previously inserted phase info
.GoTo what:=wdGoToBookmark, Name:="Phase"
.Find.ClearFormatting
.MoveUp Unit:=wdLine, Count:=1
.SelectRow
.Copy
.Tables(1).Select
.Delete Unit:=wdCharacter, Count:=1
.Paste
.SelectRow
.Rows.Delete
End With
End If
End With
With ActiveDocument
.Variables("varTradeItem"
.Variables("varCostCode"
' .Variables("varContractNo"
.Variables("varCONo"
.Variables("varExtension"
.Variables("varOldContractValue"
End With
tmpPCC = COFile.PCC
Selection.GoTo what:=wdGoToBookmark, Name:="CE"
While Not EOF(intFN)
Selection.TypeText Text:=UCase(Trim(COFile.Phase))
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=Trim(COFile.CENum)
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=Trim(COFile.OwnerRefNo)
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=Trim(COFile.SubPropNo)
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=Trim(COFile.CEDesc)
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=Trim(COFile.ChgOrderType)
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=Format(Trim(COFile.ChgOrderAmt), "$#,##0.00"
Selection.MoveRight Unit:=wdCell
If COFile.Phase <> tmpPhase Then
tmpPhase = COFile.Phase
Count = Count + 1
End If
If Count > 1 Then
tmpPhase = "MULTI"
Else
tmpPhase = COFile.Phase
End If
totCOAmt = totCOAmt + Trim(COFile.ChgOrderAmt)
intCount = intCount + 1
Get #intFN, intCount, COFile
Wend
Close #intFN
Else
MsgBox "Error #2, Cannot find path to original contract. Try re-running the original contract " _
+ "and rerunning the Change Order", vbOKOnly + vbExclamation, "Error Message"
End
End If