0

Set up a macro to open word documents through excel. It's been working fine, but after copying the code from the test environment into another file it's refusing to open word on every machine I test it on. Every other part of the macro is working fine, but for some reason word will no longer open via macro.

I've attached the code, but any help would be appreciated. It was working earlier today, and because of this i'm having trouble identifying the problem.

Public Function Method2(ByVal rngl As Range, ByVal strSearch As Variant, ByVal sPath As String)

Dim filePath As String

Dim directory As String

Dim fileName As String

Dim myPath As String

Dim myFile As File

Dim FSO As New FileSystemObject

Dim myFolder As Folder

Dim myExtension As String

Dim mySubFolder As Folder

Dim mySubFolder2 As Folder

Dim objWord

Dim objDoc

Dim rngRange

Dim rng1 As Range

Set myFolder = FSO.GetFolder(sPath)


directory = "S:\File Recipes\"

fileName = "Yaroze_Test"

myExtension = "*.docx*"

    Set rng1 = Range("A:A").find(strSearch, , xlValues, xlWhole)
    If strSearch = "" Then
    MsgBox "Please Enter a Product Code!"
    Exit Function
    End If
    If Not rng1 Is Nothing Then

        MsgBox "Product Codes Found!"
        For Each mySubFolder In myFolder.SubFolders

            For Each mySubFolder2 In mySubFolder.SubFolders

                For Each myFile In mySubFolder.Files

                    If InStr(myFile, strSearch) > 0 Then

                    fileName = Dir(myPath & myExtension)

                    MsgBox (myFile.Name)

                    Do While fileName <> ""

                        Set objWord = CreateObject("Word.Application")

                        objWord.Visible = True

                        ChDrive ("S")

                        ChDir ("S:\File Recipes\")

                        filePath = myFile.Path

                        MsgBox directory

                        objWord.Documents.Open fileName:=filePath

                        DoEvents

                        fileName = Dir

                    Loop

                    MsgBox "Task Complete!"

                    End If

                Next

                For Each myFile In mySubFolder2.Files

                    If InStr(myFile, strSearch) > 0 Then

                    fileName = Dir(myPath & myExtension)

                    ' MsgBox (myFile.Name)

                    Do While fileName <> ""

                        Set objWord = CreateObject("Word.Application")

                        objWord.Visible = True

                        ChDrive ("S")

                        ChDir ("S:\File Recipes\")

                        filePath = myFile.Path

                        ' MsgBox directory

                        objWord.Documents.Open fileName:=filePath

                        DoEvents

                        fileName = Dir

                    Loop

                    MsgBox "Task Complete!"

                    End If

                Next
            Next
        Next

        Else
        MsgBox "Product Codes Not Found!"
        End If
       ' Set rngRange = _
         objWord.Range(objWord.Paragraphs(1).Start, objWord.Paragraphs(1).End - 1)
       ' rngRange.InsertAfter _
         "This is now the last sentence in paragraph one."

I've attempted to test the Macro on other computers to see if it was just the copy of word I was using, and I've tested writing new Macros to open word. They worked initially but other macros are now no longer working. I've tried disabling office from references in VBA and testing with that, and I've made sure it's not an issue with instances of word being left open.

Joseph Oliver
  • 169
  • 1
  • 12
  • 1
    Is there an error? If so, what is the error. If not, please **edit** your post to include some detail about what troubleshooting you've done -- i.e., have you stepped through in debug-mode to test whether your `if` conditions are being evaluated as expected, etc.? Without these details, impossible to provide any assistance. – David Zemens Feb 20 '18 at 15:57
  • 1
    Probably missing references (https://stackoverflow.com/questions/3233203/how-do-i-use-filesystemobject-in-vba) – TomJohn Feb 20 '18 at 15:58
  • Not seeing where you close/quit those Word instances? If you go to Task Manager and check running programs are there multiple instances of Word running? – Tim Williams Feb 20 '18 at 15:59
  • The main issue is that it doesn't actually come up with an error. Everything else works fine, and the message boxes i've got telling me if different stages are working properly are coming up correctly with the proper file paths. The instances of word are closed by the user, the issue is that no instances of word are actually being opened. – Joseph Oliver Feb 20 '18 at 16:01
  • There's no issue with the file system references, it would generate an error with regards to the Folder and Filesystem objects if there was. – Joseph Oliver Feb 20 '18 at 16:07
  • Try to insert `Application.Wait(Now + TimeValue("00:00:05"))` before `Set objWord = CreateObject("Word.Application")` (this is rather long-shot :) ) – TomJohn Feb 20 '18 at 16:08
  • Can you please try to run in an admin cmd: taskkill /im winword.exe /f And then try your code again? –  Feb 20 '18 at 16:20
  • I tried reading through your code to see any tell tale signs but it seems to be using mixed bag with `fso` and `dir`. Not everything seems to make sense. Since it is Function, do you have `On Error Resume Next` somewhere which is masking the error? – shrivallabha.redij Feb 20 '18 at 16:48
  • 3
    WHY do you have CreateObject in every single loop iteration? This is resource intensive and you shouldn't be doing it. You should use CreateObject once, at the very beginning, then use objWord.Documents.Open to open each document in the loops. And at the end of your code you MUST use `Set objWord = Nothing`. You also haven't said whether you see multiple instances of WinWord.exe in the Task Manager. Quite likely you're overloading the system on which this is executed... – Cindy Meister Feb 20 '18 at 16:50
  • If you actually read the question, I stated at the end that it's not an issue with instances of word being left open. I've checked thoroughly for word instances and the issue is that they're not open. From what I can see now the issue is accessing one of the for loops. – Joseph Oliver Feb 20 '18 at 16:56
  • I've seen numerous cases where Office programs (Word, Excel, Outlook etc.) have remnants running. Thus my comment –  Feb 20 '18 at 17:00
  • I also agree with Cindy Meister. Your code needs to be amended. –  Feb 20 '18 at 17:02
  • I've amended the code to only open the instance of objWord at the start of the script, and i've restructured a lot of the for loops and it seems to be working now. Thank you! – Joseph Oliver Feb 21 '18 at 10:12

0 Answers0