![]() |
#1
|
|||
|
|||
![]()
Hello,
I wrote a VBA script to check a table in a word document for terms that are stored in an Excel spreadsheet. The first time I run it, everything works fine. Every other time results in an Object is invalid error and Excel won't even let me open the spreadsheet manually. Instead, I have to restart my PC to be able to open it again. This is the script: Code:
Public Sub CheckTableCells() Dim oCell As Cell Dim oRow As Row Dim MyRange As Range Dim Rng As Variant Dim findText As String Dim x1 As Excel.Application Dim wkbk As Excel.Workbook Dim wksh As Excel.Worksheet Set x1 = New Excel.Application x1.Workbooks.Close Set wkbk = x1.Workbooks.Open(FileName:="F:\Blatchford termbase_MASTER.xlsx", ReadOnly:=True) Set wksh = wkbk.Sheets(1) If x1.Ready = True Then LastRow = wksh.Columns(1).SpecialCells(xlLastCell).Row Set Rng = wksh.Range(Cells(1, 1), Cells(LastRow, 1)) Debug.Print Rng(1).Text For x = 1 To Rng.Count If Not Rng(x) Is Nothing Then findText = Rng(x).Text For Each oRow In Selection.Tables(1).Rows For Each oCell In oRow.Cells Set MyRange = oCell.Range Do While True MyRange.Find.Highlight = True MyRange.Find.MatchWildcards = False MyRange.Find.MatchWholeWord = True MyRange.Find.Forward = True MyRange.Find.IgnorePunct = True MyRange.Find.Execute (findText) If MyRange.Find.Found Then Debug.Print oCell.Range.Text With MyRange.Font .ColorIndex = wdGreen .Bold = True End With Else Exit Do End If Loop Next oCell Next oRow Else MsgBox "Nothing found" Exit For End If Next x x1.Workbooks.Close Else End End If End Sub |
#2
|
|||
|
|||
![]()
Did not look in detail. Just throwing this out. Is it possible that the file is open in memory and NOT visible? I have had that happen. Look at your task manager and see if there is an instance of Excel running there that you can't see on your screen.
Just a thought. |
#3
|
|||
|
|||
![]()
I did check that. I saw it once, but even closing the instance of Excel from the task manager didn't fix the problem.
Is there a line of code that can make sure that Excel closes properly at the end of the script? |
#4
|
|||
|
|||
![]()
Yes, but if you are not seeing the instance of Excel, that is probably not your problem. I will see if I can find the code that I wrote.
|
![]() |
Tags |
vba excel |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
dingar | Word VBA | 3 | 04-27-2017 03:18 PM |
Video Slide Runs Slow | tommyc325 | PowerPoint | 0 | 04-21-2017 06:09 AM |
Adapt a script used in Word to highlight words, to work in Outlook? | flatop | Outlook | 5 | 07-15-2014 01:07 PM |
Limiting the times that it runs | hchbiker | PowerPoint | 0 | 02-09-2011 08:36 AM |
Rule runs once, but not later | stlsailor | Outlook | 0 | 07-28-2009 08:18 PM |