运行时错误5792-如果打开docx时文件名更改

问题描述 投票:0回答:1

该宏应该从特定文件夹中的Docx文件中提取数据。如果在运行宏时打开了这些文件夹中的任何一个,程序将引发错误5792,并且当我检查文件名时,文件名会部分更改。为什么要这样做,以及如何围绕它编程。

文件名是这个:C:\ Users \ Ashley \ Desktop \受害者投诉\受害者投诉表格.docx

当打​​开docx并运行宏时,文件名更改为此,并且出现错误:C:\ Users \ Ashley.Martin \ Desktop \ Victim Complaints \〜$ ctim Complaint Form.docx

该程序似乎遍历了那里的实际文件,但是随后它将运行带有损坏文件名的额外文件。

Option Explicit


Dim FSO As Object
Dim myFile As Object
Dim myFolder As Object
Dim file As Object
Dim intRow As Integer
Dim docVic As Worksheet
Dim i As Integer
Dim strSumDoc As String
Dim LastSave As Date
Dim SumLastSave As Date
Dim docWord As Object
Dim appWord As Object
Dim FilePath As Variant
Dim HeadRange As Range

Sub VictimComplaints()


Set FSO = CreateObject("Scripting.FileSystemObject")

Set myFolder = FSO.getfolder(ThisWorkbook.Path)
Set docVic = ThisWorkbook.Worksheets("Sheet1")
Set appWord = CreateObject("Word.Application.16")
Set HeadRange = ThisWorkbook.Worksheets("Sheet1").Range("A2:AT2")

appWord.Visible = False

 iCol = 1


'loops through filepaths in folder


For Each myFile In myFolder.Files
    LastSave = FileDateTime(myFile)
    If Right(myFile, 5) = ".docx" Then
        intRow = docVic.Cells(docVic.Rows.Count, "B").End(xlUp).Row + 1
        i = 3


        Do While i <= intRow
            strSumDoc = Cells(i, "B")
            SumLastSave = Cells(i, "C")
            'info on summary doc is already the latest bit of information
            If strSumDoc = myFile And LastSave <= SumLastSave Then
                 Exit Do

            'matching file already on document and saved later than last save date so info gets updated
            ElseIf strSumDoc = myFile And LastSave > SumLastSave Then
            '**Extracts data--works fine
                Exit Do

            'No match was found and at first empty row, make new entry on the summary doc
            ElseIf strSumDoc = "" Then
                'copy info to last row
                'MsgBox "Copy to last row " & myFile.Name
                strSumDoc = myFile  
                Set docWord = appWord.documents.Open(strSumDoc) '**Throws error because file name changed but there should be no file left.
                tblCount = docWord.tables.Count
                    With docWord
                    With .tables(1)
                    'Extracts data from table, works fine
                Exit Do

            Else:  'iteration doesn't match myfile, loop to next row
            End If

        i = i + 1
        Loop
        End If
Next

appWord.Quit
Set appWord = Nothing

End Sub
excel vba ms-word
1个回答
0
投票

要对其编程,我跳过了以“〜$”开头的文件名。不知道在文件夹中循环浏览文件时为什么或如何找到该文件名。那里没有命名为的文件。所以这个问题就像一半回答了。我可能只是在解决一个更大的问题。

© www.soinside.com 2019 - 2024. All rights reserved.