Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-10-2017, 06:45 AM
jakecahill jakecahill is offline VBA script runs once but if I try to run it again, it does not work Windows 7 64bit VBA script runs once but if I try to run it again, it does not work Office 2010 64bit
Novice
VBA script runs once but if I try to run it again, it does not work
 
Join Date: Jul 2017
Posts: 2
jakecahill is on a distinguished road
Question VBA script runs once but if I try to run it again, it does not work

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
Reply With Quote
  #2  
Old 07-10-2017, 03:34 PM
cboshdave cboshdave is offline VBA script runs once but if I try to run it again, it does not work Windows 7 64bit VBA script runs once but if I try to run it again, it does not work Office 2013
Novice
 
Join Date: Jul 2017
Posts: 3
cboshdave is on a distinguished road
Default

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.
Reply With Quote
  #3  
Old 07-11-2017, 12:40 AM
jakecahill jakecahill is offline VBA script runs once but if I try to run it again, it does not work Windows 7 64bit VBA script runs once but if I try to run it again, it does not work Office 2010 64bit
Novice
VBA script runs once but if I try to run it again, it does not work
 
Join Date: Jul 2017
Posts: 2
jakecahill is on a distinguished road
Default

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?
Reply With Quote
  #4  
Old 07-11-2017, 07:32 AM
cboshdave cboshdave is offline VBA script runs once but if I try to run it again, it does not work Windows 7 64bit VBA script runs once but if I try to run it again, it does not work Office 2013
Novice
 
Join Date: Jul 2017
Posts: 3
cboshdave is on a distinguished road
Default

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.
Reply With Quote
Reply

Tags
vba excel



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA script runs once but if I try to run it again, it does not work VBA runs I then save as to keep master free from editds but does not work 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

Other Forums: Access Forums

All times are GMT -7. The time now is 08:36 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft