Can';t使用Word VBA中的文件系统对象FileCopy方法复制文件
我试图循环遍历一个表,并将已知副本中的命名文件复制到项目文件夹中,以自动化当前的手动过程。我已经得到了要复制的内容和复制位置的字符串,但是我得到了FileCopy本身的“Object不支持此方法或属性”。我得到的是:Can';t使用Word VBA中的文件系统对象FileCopy方法复制文件,vba,ms-word,Vba,Ms Word,我试图循环遍历一个表,并将已知副本中的命名文件复制到项目文件夹中,以自动化当前的手动过程。我已经得到了要复制的内容和复制位置的字符串,但是我得到了FileCopy本身的“Object不支持此方法或属性”。我得到的是: Sub OEMFileCopy() Dim str, oem, oemArray() As String Dim folderLetter, folder, oemFolder, company, copyFrom As String Dim oTable As Table Di
Sub OEMFileCopy()
Dim str, oem, oemArray() As String
Dim folderLetter, folder, oemFolder, company, copyFrom As String
Dim oTable As Table
Dim oRow As Row
Dim myRng As Range
Dim i As Integer
Dim fso
oemFolder = "M:\Technical Support\Project Documentation\O.E.M Tech Literature"
Set fso = CreateObject("Scripting.FileSystemObject")
For Each oTable In ActiveDocument.Tables
oTable.Cell(1, 1).Select
str = Selection.Text
str = Left(str, Len(str) - 2) ' << remove end of cell characters
' Test maintenance table
If (str Like "Manufacturer") Then
For Each oRow In oTable.Rows
Set myRng = oTable.Cell(oRow.Index, 3).Range
myRng.MoveEnd wdCharacter, -1
oem = myRng.Text
Set myRng = oTable.Cell(oRow.Index, 1).Range
myRng.MoveEnd wdCharacter, -1
company = myRng.Text
Debug.Print ("Company is " & company)
If (Not ((oem Like "*O.E.M*") Or (oem Like "*OEM*") Or (oem Like "*WWW*") Or (oem Like "*www*"))) Then
oemArray() = Split(oem, ", ") '<< split into array of OEM filenames
For i = LBound(oemArray) To UBound(oemArray) '<< print each oem to output file
folderLetter = Left(oemArray(i), 1)
folder = ActiveDocument.Path & "\OEM Technical Literature\" & folderLetter & "\" & company
copyFrom = oemFolder & "\" & folderLetter & "\" & company & "\" & oemArray(i) & ".pdf"
If (fso.FolderExists(folder)) Then
folder = folder & "\"
fso.FileCopy copyFrom, folder
Else
fso.CreateFolder (folder)
End If
Next i
End If
Next oRow
End If
Next oTable
End Sub
子OEMFileCopy()
Dim str、oem、oemArray()作为字符串
Dim folderLetter、文件夹、oemFolder、公司、copyFrom作为字符串
像桌子一样昏暗
模糊的或像行一样的
暗myRng As范围
作为整数的Dim i
模糊fso
oemFolder=“M:\Technical Support\Project Documentation\O.E.M技术文献”
设置fso=CreateObject(“Scripting.FileSystemObject”)
对于ActiveDocument.Tables中的每个oTable
可旋转。单元格(1,1)。选择
str=Selection.Text
str=Left(str,Len(str)-2)“您混淆了文件复制命令:
FileCopy
是底层VBA语言中可用的命令之一
CopyFile
是FileSystemObject
对象可用的方法之一
因此,与其使用
fso.FileCopy copyFrom, folder
使用
相反。您混淆了文件复制命令:
FileCopy
是底层VBA语言中可用的命令之一
CopyFile
是FileSystemObject
对象可用的方法之一
因此,与其使用
fso.FileCopy copyFrom, folder
使用
相反。请注意,在同一行上定义多个变量时,必须为每个变量指定类型。在您的情况下,在Dim folderLetter,folder,oemFolder,company,copyFrom As String
中,只有copyFrom
是字符串,其他变量是变量。谢谢。我不知道是这样的。我对一个字符串变量发生了一些奇怪的事情,并将其拆分为两行,我想知道为什么这会解决它。请注意,在同一行上定义多个变量时,必须为每个变量指定类型。在您的情况下,在Dim folderLetter,folder,oemFolder,company,copyFrom As String
中,只有copyFrom
是字符串,其他变量是变量。谢谢。我不知道是这样的。我在一个字符串变量上发生了一些奇怪的事情,并将它们分成了两行,我想知道为什么这样可以解决这个问题。