Word将不再通过excel VBA打开

时间:2018-02-20 15:54:49

标签: excel vba excel-vba

设置宏以通过excel打开word文档。它工作正常,但在将测试环境中的代码复制到另一个文件后,它拒绝在我测试的每台机器上打开文字。宏的每个其他部分都工作正常,但由于某种原因,单词将不再通过宏打开。

我已经附上了代码,但任何帮助都会受到赞赏。它今天早些时候工作,因此我无法确定问题。

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."

我试图在其他计算机上测试宏以查看它是否只是我正在使用的单词的副本,并且我已经测试过将新宏编写为打开单词。他们最初工作,但其他宏现在不再工作。我已经尝试过在VBA中禁用办公室并使用它进行测试,并且我已经确定单词的实例不会出现问题。

0 个答案:

没有答案