使用 VBA 运行时错误 462 合并 Excel 中的 Docx 文件 远程服务器机器,然后文件损坏
Merging Docx Files in Excel using VBA Runtime Error 462 The remote server machine, then File Corrupted
求助!已经有很多关于这个主题的帖子,我已经尝试了大部分,但仍然无济于事。
基本上我只是想合并指定文件夹中具有相同名称的docx文件的内容。每个文件都包含一个图像,1file:1image。我第一次 运行 代码时出现错误(错误 462 远程服务器计算机不存在或不可用),它指向
ActiveDocument.Range.InsertFile Filename:=myDir & nextFile
有人知道如何解决这个问题吗?请。这是我的代码:
Sub Merger()
Dim firstFileStr As String
Dim firstFile As String
Dim nextFileStr As String
Dim nextFile As String
Dim xFlag As Boolean
Dim myDir As String
myDir = "C:\Users\User\Desktop\MergeFolder\"
For iCtr = 1 To getRowCount
firstFileStr = Sheet1.Cells(iCtr, 3).Value
firstFile = Sheet1.Cells(iCtr, 1).Value
xFlag = True
Dim jCtr As Integer
jCtr = 1
Do While (xFlag)
nextFileStr = Sheet1.Cells(iCtr + jCtr, 3).Value
nextFile = Sheet1.Cells(iCtr + jCtr, 1).Value
If StrComp(firstFileStr, nextFileStr, vbTextCompare) = 0 Then
Dim WordApp As Word.Application 'word application
Dim WordDoc As Word.Document 'word document
If Not WordApp Is Nothing Then
WordApp.Quit
End If
Set WordApp = New Word.Application
Set WordDoc = WordApp.Documents.Open(myDir & firstFile, ConfirmConversions = False, ReadOnly = False)
Application.ScreenUpdating = False
ActiveDocument.Range.InsertFile Filename:=myDir & nextFile
WordDoc.SaveAs Filename:=myDir & "merged " & firstFileStr, FileFormat:=wdFormatDocument
WordDoc.Close
WordApp.Quit
Set WordDoc = Nothing
Set WordApp = Nothing
xFlag = True
jCtr = jCtr + 1
Else
xFlag = False
End If
Loop
Next
End Sub
函数getRowCount 只是return Sheet1 包含的行数。这里,
Function getRowCount() As Integer
Dim rowCount As Integer
rowCount = 0
Range("A1").Select
' Set Do loop to stop when an empty cell is reached.
Do While Not IsEmpty(ActiveCell)
rowCount = rowCount + 1
' Step down 1 row from present location.
ActiveCell.Offset(1, 0).Select
Loop
getRowCount = rowCount
End Function
现在,当我 运行 在上一个错误之后立即执行代码并且没有在任务管理器中终止 MS Word 进程时,我收到另一个错误运行时错误 5792 文件似乎已损坏。检查新创建的文件,似乎代码
ActiveDocument.Range.InsertFile Filename:=myDir & nextFile
根本没用。 excel 文件看起来像(B 列为空)
Column A B C
alpha - 1.docx alpha
alpha -2.docx alpha
alpha - 3.docx alpha
alpha - 4.docx alpha
bravo - 1.docx bravo
bravo - 5.docx bravo
charlie- 2.docx charlie
delta - 3.docx delta
delta - 5.docx delta
epsilon - 9.docx epsilon
foxtrot - 1.docx foxtrot
merger.xlsm 0
~$merger.xlsm 0
应该将所有“Alpha - *.docx”合并到“Alpha - 1.docx”,太棒了 - 5.docx to bravo - 1.docx, 等等
我做了一些研究,问题是您以只读方式打开了 word 文件。请确保 word doc 已关闭,并且您有权使用写入权限打开它。
您可以在打开文件后但在尝试向其中粘贴内容之前的某个时间添加一个消息框来测试它是否以只读方式打开:
MsgBox Worddoc.readonly
求助!已经有很多关于这个主题的帖子,我已经尝试了大部分,但仍然无济于事。
基本上我只是想合并指定文件夹中具有相同名称的docx文件的内容。每个文件都包含一个图像,1file:1image。我第一次 运行 代码时出现错误(错误 462 远程服务器计算机不存在或不可用),它指向
ActiveDocument.Range.InsertFile Filename:=myDir & nextFile
有人知道如何解决这个问题吗?请。这是我的代码:
Sub Merger()
Dim firstFileStr As String
Dim firstFile As String
Dim nextFileStr As String
Dim nextFile As String
Dim xFlag As Boolean
Dim myDir As String
myDir = "C:\Users\User\Desktop\MergeFolder\"
For iCtr = 1 To getRowCount
firstFileStr = Sheet1.Cells(iCtr, 3).Value
firstFile = Sheet1.Cells(iCtr, 1).Value
xFlag = True
Dim jCtr As Integer
jCtr = 1
Do While (xFlag)
nextFileStr = Sheet1.Cells(iCtr + jCtr, 3).Value
nextFile = Sheet1.Cells(iCtr + jCtr, 1).Value
If StrComp(firstFileStr, nextFileStr, vbTextCompare) = 0 Then
Dim WordApp As Word.Application 'word application
Dim WordDoc As Word.Document 'word document
If Not WordApp Is Nothing Then
WordApp.Quit
End If
Set WordApp = New Word.Application
Set WordDoc = WordApp.Documents.Open(myDir & firstFile, ConfirmConversions = False, ReadOnly = False)
Application.ScreenUpdating = False
ActiveDocument.Range.InsertFile Filename:=myDir & nextFile
WordDoc.SaveAs Filename:=myDir & "merged " & firstFileStr, FileFormat:=wdFormatDocument
WordDoc.Close
WordApp.Quit
Set WordDoc = Nothing
Set WordApp = Nothing
xFlag = True
jCtr = jCtr + 1
Else
xFlag = False
End If
Loop
Next
End Sub
函数getRowCount 只是return Sheet1 包含的行数。这里,
Function getRowCount() As Integer
Dim rowCount As Integer
rowCount = 0
Range("A1").Select
' Set Do loop to stop when an empty cell is reached.
Do While Not IsEmpty(ActiveCell)
rowCount = rowCount + 1
' Step down 1 row from present location.
ActiveCell.Offset(1, 0).Select
Loop
getRowCount = rowCount
End Function
现在,当我 运行 在上一个错误之后立即执行代码并且没有在任务管理器中终止 MS Word 进程时,我收到另一个错误运行时错误 5792 文件似乎已损坏。检查新创建的文件,似乎代码
ActiveDocument.Range.InsertFile Filename:=myDir & nextFile
根本没用。 excel 文件看起来像(B 列为空)
Column A B C
alpha - 1.docx alpha
alpha -2.docx alpha
alpha - 3.docx alpha
alpha - 4.docx alpha
bravo - 1.docx bravo
bravo - 5.docx bravo
charlie- 2.docx charlie
delta - 3.docx delta
delta - 5.docx delta
epsilon - 9.docx epsilon
foxtrot - 1.docx foxtrot
merger.xlsm 0
~$merger.xlsm 0
应该将所有“Alpha - *.docx”合并到“Alpha - 1.docx”,太棒了 - 5.docx to bravo - 1.docx, 等等
我做了一些研究,问题是您以只读方式打开了 word 文件。请确保 word doc 已关闭,并且您有权使用写入权限打开它。
您可以在打开文件后但在尝试向其中粘贴内容之前的某个时间添加一个消息框来测试它是否以只读方式打开:
MsgBox Worddoc.readonly