crarbo1
03-06-2011, 03:36 PM
Hello,
I'm hoping someone will be able to help me fix my issue. I have inherited some word documents that update an Excel spreadsheet. I'm not an expert in VBA but can do some basic things. The issue I have is that the word documents were created in Word 2000 and reference the Excel library. If someone opens the file in Word 2010 and saves it, then there is a compile error when someone else opens it up in Word 2000. It appears that reference to the Excel library is missing when it is opened back up in Word 2000. In trying to find a solution, it appears that taking code that uses early binding and converting it to late binding would resolve my issue. Is that correct? I really have no idea as to what to do but I have attached my code for your reference. From what I read, it is using early binding.
Thanks,
Chuck
Sub UpdateTrainingLog()
Dim xlApp As Object
Dim xlb As Object
Dim wksht As Object
Dim r As Integer
Dim LogName As String
Dim ShortLogName As String
Dim SSO As String
Dim TName As String
Dim Standard As String
Dim mrange As Object
Dim uresp As Integer
On Error GoTo XLError
ThisDocument.FormFields("Approval").Range.Tables(1).Cell(1, 1).Range.Text = "Updating training log..."
LogName = "\\TestFolder\EMC Training Log.xls"
ShortLogName = "EMC Training Log.xls"
Set xlApp = New Excel.Application
Set xlb = xlApp.Workbooks.Open(FileName:=LogName, writerespassword:="straub") ' Excel.Workbooks.Open(rffile)
If xlb.ReadOnly = True Then 'User does not have read/write permission
MsgBox "You do not have permission to access the server where the EMC Training Log is located, therefore your training record has NOT been updated as required." & vbCrLf & vbCrLf & "Corrective action: Please contact Dave Terrell to have your name added to the server permissions list.", vbCritical + vbOKOnly, "Access denied to training log"
xlb.Close SaveChanges:=False
GoTo XLBail
End If
Set wksht = xlb.Worksheets("Training Log")
SSO = GetSingleDataItem(ThisDocument, "SSO#:")
TName = GetSingleDataItem(ThisDocument, "sonnel:")
If Trim(TName) = "" Then
TName = "( No name entered )"
End If
Standard = GetSingleDataItem(ThisDocument, "Basic Standard:")
TryAgain:
If (Not IsNumeric(SSO)) Or (Len(SSO) <> 9) Then 'Bad SSO number
SSO = GetUserSSO()
uresp = MsgBox("Unable to update training records for " & TName & " due to unrecognizable SSO#." & vbCrLf & vbCrLf & "Would you like to try again using the following SSO#: " & SSO, vbYesNo, "Bad SSO#")
If uresp = vbNo Then
GoTo XLBail
Else
PutSingleDataItem ThisDocument, "SSO#:", SSO
GoTo TryAgain
End If
End If
If TName = "( No name entered )" Then
'If possible get name from log file
For r = 2 To 32000
If wksht.Range("A" & Trim(Str(r))).Value = SSO Then 'Get user name
TName = Trim(wksht.Range("B" & Trim(Str(r))).Value)
If TName > "" Then
uresp = MsgBox("Unable to update training records for SSO# " & SSO & " because no name has been entered in the Test Personnel field. Would you like to try again using the following name: " & TName, vbYesNo, "Missing user name")
If uresp = vbNo Then
GoTo XLBail
Else
PutSingleDataItem ThisDocument, "sonnel:", TName
GoTo TryAgain
End If
End If
End If
Next r
End If
If TName = "( No name entered )" Then 'Could not find name in database
MsgBox "Unable to update training records for SSO# " & SSO & " because no name has been entered in the Test Personnel field. Please complete this field, and then have your setup approved again in order to update your training records.", vbOKOnly, "Missing user name"
GoTo XLBail
End If
'Ready to update log
wksht.Range("A1:A32000").Sort Key1:=wksht.Range("A2"), Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
For r = 2 To 32000
If wksht.Range("A" & Trim(Str(r))).Value = SSO Then 'Check for Standard match
If wksht.Range("C" & Trim(Str(r))).Value = Standard Then 'This is it
wksht.Range("B" & Trim(Str(r))).Value = TName
wksht.Range("C" & Trim(Str(r))).Value = Standard
wksht.Range("D" & Trim(Str(r))).Value = Date
GoTo XLBail
End If
Else
If (wksht.Range("A" & Trim(Str(r))).Value > SSO) Or (wksht.Range("A" & Trim(Str(r))).Value = "") Then 'No more matching entries
wksht.Range("A" & Trim(Str(r))).EntireRow.Insert
wksht.Range("A" & Trim(Str(r))).Value = SSO
wksht.Range("B" & Trim(Str(r))).Value = TName
wksht.Range("C" & Trim(Str(r))).Value = Standard
wksht.Range("D" & Trim(Str(r))).Value = Date
Set mrange = wksht.Range("A" & Trim(Str(r)) & ":D" & Trim(Str(r)))
With mrange
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
With .Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
.Interior.ColorIndex = xlNone
.Font.Bold = False
End With
GoTo XLBail
End If
End If
Next r
XLBail:
On Error Resume Next
xlb.Close SaveChanges:=True
Set wksht = Nothing
Set xlb = Nothing
xlApp.Quit
Set xlApp = Nothing
Set mrange = Nothing
Exit Sub
XLError:
MsgBox "Unable to update training records.", vbOKOnly + vbInformation, "File access error"
Resume XLBail
Resume
End Sub
I'm hoping someone will be able to help me fix my issue. I have inherited some word documents that update an Excel spreadsheet. I'm not an expert in VBA but can do some basic things. The issue I have is that the word documents were created in Word 2000 and reference the Excel library. If someone opens the file in Word 2010 and saves it, then there is a compile error when someone else opens it up in Word 2000. It appears that reference to the Excel library is missing when it is opened back up in Word 2000. In trying to find a solution, it appears that taking code that uses early binding and converting it to late binding would resolve my issue. Is that correct? I really have no idea as to what to do but I have attached my code for your reference. From what I read, it is using early binding.
Thanks,
Chuck
Sub UpdateTrainingLog()
Dim xlApp As Object
Dim xlb As Object
Dim wksht As Object
Dim r As Integer
Dim LogName As String
Dim ShortLogName As String
Dim SSO As String
Dim TName As String
Dim Standard As String
Dim mrange As Object
Dim uresp As Integer
On Error GoTo XLError
ThisDocument.FormFields("Approval").Range.Tables(1).Cell(1, 1).Range.Text = "Updating training log..."
LogName = "\\TestFolder\EMC Training Log.xls"
ShortLogName = "EMC Training Log.xls"
Set xlApp = New Excel.Application
Set xlb = xlApp.Workbooks.Open(FileName:=LogName, writerespassword:="straub") ' Excel.Workbooks.Open(rffile)
If xlb.ReadOnly = True Then 'User does not have read/write permission
MsgBox "You do not have permission to access the server where the EMC Training Log is located, therefore your training record has NOT been updated as required." & vbCrLf & vbCrLf & "Corrective action: Please contact Dave Terrell to have your name added to the server permissions list.", vbCritical + vbOKOnly, "Access denied to training log"
xlb.Close SaveChanges:=False
GoTo XLBail
End If
Set wksht = xlb.Worksheets("Training Log")
SSO = GetSingleDataItem(ThisDocument, "SSO#:")
TName = GetSingleDataItem(ThisDocument, "sonnel:")
If Trim(TName) = "" Then
TName = "( No name entered )"
End If
Standard = GetSingleDataItem(ThisDocument, "Basic Standard:")
TryAgain:
If (Not IsNumeric(SSO)) Or (Len(SSO) <> 9) Then 'Bad SSO number
SSO = GetUserSSO()
uresp = MsgBox("Unable to update training records for " & TName & " due to unrecognizable SSO#." & vbCrLf & vbCrLf & "Would you like to try again using the following SSO#: " & SSO, vbYesNo, "Bad SSO#")
If uresp = vbNo Then
GoTo XLBail
Else
PutSingleDataItem ThisDocument, "SSO#:", SSO
GoTo TryAgain
End If
End If
If TName = "( No name entered )" Then
'If possible get name from log file
For r = 2 To 32000
If wksht.Range("A" & Trim(Str(r))).Value = SSO Then 'Get user name
TName = Trim(wksht.Range("B" & Trim(Str(r))).Value)
If TName > "" Then
uresp = MsgBox("Unable to update training records for SSO# " & SSO & " because no name has been entered in the Test Personnel field. Would you like to try again using the following name: " & TName, vbYesNo, "Missing user name")
If uresp = vbNo Then
GoTo XLBail
Else
PutSingleDataItem ThisDocument, "sonnel:", TName
GoTo TryAgain
End If
End If
End If
Next r
End If
If TName = "( No name entered )" Then 'Could not find name in database
MsgBox "Unable to update training records for SSO# " & SSO & " because no name has been entered in the Test Personnel field. Please complete this field, and then have your setup approved again in order to update your training records.", vbOKOnly, "Missing user name"
GoTo XLBail
End If
'Ready to update log
wksht.Range("A1:A32000").Sort Key1:=wksht.Range("A2"), Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
For r = 2 To 32000
If wksht.Range("A" & Trim(Str(r))).Value = SSO Then 'Check for Standard match
If wksht.Range("C" & Trim(Str(r))).Value = Standard Then 'This is it
wksht.Range("B" & Trim(Str(r))).Value = TName
wksht.Range("C" & Trim(Str(r))).Value = Standard
wksht.Range("D" & Trim(Str(r))).Value = Date
GoTo XLBail
End If
Else
If (wksht.Range("A" & Trim(Str(r))).Value > SSO) Or (wksht.Range("A" & Trim(Str(r))).Value = "") Then 'No more matching entries
wksht.Range("A" & Trim(Str(r))).EntireRow.Insert
wksht.Range("A" & Trim(Str(r))).Value = SSO
wksht.Range("B" & Trim(Str(r))).Value = TName
wksht.Range("C" & Trim(Str(r))).Value = Standard
wksht.Range("D" & Trim(Str(r))).Value = Date
Set mrange = wksht.Range("A" & Trim(Str(r)) & ":D" & Trim(Str(r)))
With mrange
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
With .Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
.Interior.ColorIndex = xlNone
.Font.Bold = False
End With
GoTo XLBail
End If
End If
Next r
XLBail:
On Error Resume Next
xlb.Close SaveChanges:=True
Set wksht = Nothing
Set xlb = Nothing
xlApp.Quit
Set xlApp = Nothing
Set mrange = Nothing
Exit Sub
XLError:
MsgBox "Unable to update training records.", vbOKOnly + vbInformation, "File access error"
Resume XLBail
Resume
End Sub