简体   繁体   English

MS Word 2013 VBA宏功能

[英]MS Word 2013 VBA Macro Function

The following VBA Code will not save an open document to a sub-folder under the active 'My Documents' Folder. 以下VBA代码不会将打开的文档保存到活动“我的文档”文件夹下的子文件夹中。 The code is called from App_DocumentBeforeClose and it executes without throwing a fault flag or process failed notification. 该代码从App_DocumentBeforeClose调用,并且在执行时不会抛出故障标志或处理失败通知。 All the code and save location string building works just the way its supposed to - the open document just doesn't get saved to the 'My Documents' sub-folder. 所有代码和保存位置字符串的构建均按其应有的方式工作-打开的文档不会保存到“我的文档”子文件夹中。 The file itself is a working copy stored on a SDHC chip - could this be the problem? 该文件本身是存储在SDHC芯片上的工作副本-这可能是问题吗? I have checked the folder rights and the sub-folder 'Read Only' attribute is turned off. 我已经检查了文件夹权限,并且子文件夹“只读”属性已关闭。

Public Sub SaveToTwoLocations()
Dim Res
Dim oDoc As Document, SourceFile As String, DestinationFile As String
Dim strBackUpPath As String, fDialog As FileDialog, Reps, DocName As String

If Right(ActiveWindow.Caption, 4) = "ode]" Then
    DocName = Left(ActiveWindow.Caption, Len(ActiveWindow.Caption) - 21)
ElseIf Right(ActiveWindow.Caption, 5) = ".docx" Then
    DocName = Left(ActiveWindow.Caption, Len(ActiveWindow.Caption) - 5)
End If

On Error GoTo CanceledByUser

Res = MsgBox("Save Source File?", vbQuestion + vbYesNo, "Save Original Prior to Back-Up Interrogative")
If Res = vbYes Then
    Application.ActiveDocument.Save
End If

If GetSetting("My_Books", DocName, "Save_2") = "" Then
    Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
    With fDialog
        .Title = "Select Folder to Save The Copy To & Click Ok"
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewList
        If .Show <> -1 Then
            MsgBox "Canceled By user", , "Save To Two Locatiions"
            Exit Sub
        End If
        strBackUpPath = fDialog.SelectedItems.Item(1) & "\"
        Res = MsgBox("Save File To Selected 'SaveTo' Location?", vbQuestion + vbYesNo, "'SaveTo' Interrogative")
        If Res = vbYes Then
            SaveSetting "My_Books", DocName, "Save_2", strBackUpPath
            strBackUpPath = strBackUpPath & DocName & ".docx"
            Application.ActiveDocument.SaveAs2 (strBackUpPath)
        Else
            Exit Sub
        End If
    End With

Else

    strBackUpPath = GetSetting("My_Books", DocName, "Save_2")
    Res = MsgBox("Save This Document To: " & strBackUpPath & "?", vbQuestion + vbYesNo, "Two Location Save Interrogative")
    If Res = vbYes Then
        If Right(ActiveDocument.Name, 1) = "x" Then
            Application.ActiveDocument.SaveAs2 (strBackUpPath = strBackUpPath & DocName & ".docx")
        Else
            MsgBox "Non-docx Doument File Save Error", vbCritical, "2nd Location File Save Error"
            GoTo CanceledByUser
        End If
    Else
        Set fDialog = Application.FileDialog(msoFileDialogSaveAs)
        With fDialog
            .Title = "Select Folder to Save The Copy To & Click Ok"
            .AllowMultiSelect = False
            .InitialView = msoFileDialogViewList
            If .Show <> -1 Then
                MsgBox "File Save Canceled By User", , "Save To Two Locatiions Canceled"
                Exit Sub
            End If
        End With
    End If

End If

CanceledByUser:
End Sub
Application.ActiveDocument.SaveAs2 (strBackUpPath = strBackUpPath & DocName & ".docx")

应该

Application.ActiveDocument.SaveAs2 strBackUpPath

The code should have been: Application.ActiveDocument.SaveAs2 (strBackUpPath & DocName & ".docx") In my defense, I will say that Microsoft is often its own worst enemy for reasons amply documented elsewhere - as for the code as originally written, it would have worked in standard VB6, BUT VBA is not VB6. 该代码应该是:Application.ActiveDocument.SaveAs2(strBackUpPath&DocName&“ .docx”)在我的辩护中,我会说,由于其他地方充分记录的原因,Microsoft通常是自己最大的敌人-至于原始编写的代码,它将在标准VB6中工作,但VBA不是VB6。 To Tim Williams I offer my thanks - while technically incorrect, you put me on to the right answer, AND it's possible the concatenation, as written was being misinterpreted by the compiler. 我要向蒂姆·威廉姆斯表示感谢-尽管从技术上讲是错误的,但您还是给了我正确的答案,并且可能由于编译器误解了书面说明而将其串联起来。 But the concatenation as rewritten was still required for reasons of efficiency and compactness. 但是由于效率和紧凑性的原因,仍然需要重新编写连接。 Oh, and it's working perfectly now that i have corrected my mistake! 哦,现在我更正了我的错误,它的运行正常! Thanks to all! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM