将文件复制到MS Word

时间:2017-03-10 05:43:36

标签: vba vbscript ms-word

以下编码用于将word文档从一个文件夹复制到另一个文件夹,其中定义了路径。

Sub copyfile()
   Const SourceFile = "D:\Macrotest\B\xxxxxxxxx_queries.docx"

    Set fso = CreateObject("Scripting.FileSystemObject")
        'Check to see if the file already exists in the destination folder
        If fso.FileExists(DestinationFile) Then
        'Check to see if the file is read-only
        If Not fso.GetFile(DestinationFile).Attributes And 1 Then
            'The file exists and is not read-only.  Safe to replace the file.
            fso.CopyFile SourceFile, "D:\Macrotest\A\", True
        Else
            'The file exists and is read-only.
            'Remove the read-only attribute
            fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes - 1
            'Replace the file
            fso.CopyFile SourceFile, "D:\Macrotest\A\", True
            'Reapply the read-only attribute
            fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes + 1
        End If
    Else
        'The file does not exist in the destination folder.  Safe to copy file to this folder.
        fso.CopyFile SourceFile, "D:\Macrotest\A\", True
    End If
    Set fso = Nothing
End Sub

我需要一个代码,将文件从文件夹移动到我正在访问的相同文件夹。弹出后面的消息后,我使用\代替D:\Macrotest\A\。错误显示在最后fso.CopyFile SourceFile, "\", True

之上的End If
Sub copyfile()
    Const SourceFile = "D:\Macrotest\B\xxxxxxxxx_queries.docx"
    Set fso = CreateObject("Scripting.FileSystemObject")
    'Check to see if the file already exists in the destination folder
    If fso.FileExists(DestinationFile) Then
        'Check to see if the file is read-only
        If Not fso.GetFile(DestinationFile).Attributes And 1 Then
            'The file exists and is not read-only.  Safe to replace the file.
            fso.CopyFile SourceFile, "\", True
        Else
            'The file exists and is read-only.
            'Remove the read-only attribute
            fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes - 1
            'Replace the file
            fso.CopyFile SourceFile, "\", True
            'Reapply the read-only attribute
            fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes + 1
        End If
    Else
        'The file does not exist in the destination folder.  Safe to copy file to this folder.
        fso.CopyFile SourceFile, "\", True
    End If
    Set fso = Nothing
End Sub

enter image description here

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

“\”是当前驱动器的根目录(很可能是C:\)。你似乎没有写访问权限,所以这就是你的错误来源。

要将文件复制到当前工作目录,请使用.\.是当前目录的路径表示法。)

fso.CopyFile SourceFile, ".\", True