【问题标题】:Error while running a macro from the ribbon从功能区运行宏时出错
【发布时间】:2019-04-19 08:41:54
【问题描述】:

我使用的是 Microsoft Word 2010。我使用 VBA 编辑器编写了一个代码,用于保存我编辑的文档。基本上,它会打开另存为文件对话框,并根据由文档中的某些文本定义的字符串和用户通过输入框输入的字符串输入名称。 当我单击查看宏命令选项卡并从那里执行宏命令时,我可以成功运行它,但是如果我在功能区上放置一个快捷方式,该文件会立即保存在 C:\Users\Username\Documents 中,其中包含文档的第一行.

Sub SaveAs()
'Preia numarul dosarului in numele fisierului

Dim oRng As Range
Dim Nrdosar As String
Dim sTags As String
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Execute FindText:="Dosar nr. ", Forward:=True, _
Format:=False, Wrap:=wdFindStop
End With
oRng.Collapse 0
Nrdosar = oRng.Paragraphs(1).Range.Text
Nrdosar = Replace(Nrdosar, "Dosar nr. ", "")
Nrdosar = Replace(Nrdosar, "DOSAR NR. ", "")
Nrdosar = Replace(Nrdosar, "/4/", "-")
Nrdosar = Replace(Nrdosar, "/", "-")
Nrdosar = Replace(Nrdosar, "*", "")
Nrdosar = Replace(Nrdosar, Chr(13), "")
MsgBox Nrdosar
sTags = InputBox("Introduceti cuvinte cheie separate de virgula")
With Dialogs(wdDialogFileSaveAs)
    .Name = Nrdosar & " " & sTags & ".docx"
    .Show
    End With
End Sub

我已导出功能区快捷方式以尝试了解问题

<mso:cmd app="Word" dt="1" />
<mso:customUI xmlns:x1="http://schemas.microsoft.com/office/2009/07/customui/macro" xmlns:mso="http://schemas.microsoft.com/office/2009/07/customui"><mso:ribbon>
<mso:qat><mso:sharedControls>
<mso:control idQ="mso:FileNewDefault" visible="false"/><mso:control idQ="mso:FileOpen" visible="false"/>
<mso:control idQ="mso:FileSendAsAttachment" visible="false" insertBeforeQ="mso:FileSave"/>
<mso:control idQ="mso:FilePrintQuick" visible="false" insertBeforeQ="mso:FileSave"/>
<mso:control idQ="mso:PrintPreviewAndPrint" visible="false" insertBeforeQ="mso:FileSave"/>
<mso:control idQ="mso:SpellingAndGrammar" visible="false" insertBeforeQ="mso:FileSave"/>
<mso:control idQ="mso:TableDrawTable" visible="false" insertBeforeQ="mso:FileSave"/>
<mso:control idQ="mso:FileOpenRecentFile" visible="false" insertBeforeQ="mso:FileSave"/>
<mso:control idQ="mso:Undo" visible="true" insertBeforeQ="mso:FileSave"/><mso:control idQ="mso:RedoOrRepeat" visible="true" InsertBeforeQ="mso:FileSave"/>
<mso:control idQ="mso:FileSave" visible="true"/>
<mso:button idQ="x1:SaveAs_1" visible="true" label="Normal.NewMacros.SaveAs" imageMso="ListMacros" onAction="SaveAs"/>
</mso:sharedControls></mso:qat>
<mso:tabs>
<mso:tab idQ="mso:TabHome">
<mso:group id="mso_c1.-D823CC6" label="Auto" autoScale="true">
<mso:gallery idQ="mso:AutoTextGallery" showInRibbon="false" visible="true"/>
<mso:control idQ="mso:TableColumnsDelete" visible="true"/>
<mso:control idQ="mso:TableRowsDelete" visible="true"/>
<mso:button idQ="x1:SaveAs_0_102AE36C" label="Normal.NewMacros.SaveAs" imageMso="ListMacros" onAction="SaveAs" visible="true"/>
</mso:group>
</mso:tab><mso:tab id="mso_c1.5F8A243" label="FilÄ nouÄ">
<mso:group id="mso_c2.5F8A243" label="Grup nou" autoScale="true">
<mso:control idQ="mso:DateAndTimeInsert" visible="true"/>
</mso:group>
</mso:tab>
</mso:tabs>
</mso:ribbon>
</mso:customUI>

我无法想象为什么从查看宏命令选项卡运行宏命令与我放在功能区中的快捷方式时结果不同。

我相信 VBA 代码本身没有问题,并且存在某种错误。 我希望能够使用快捷方式运行 VBA 代码。 谢谢!

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    SaveAs 是 Word 中的内置命令(如在文件/另存为中)。因此,发送命令以运行具有此名称的宏就是绕过实际的宏并执行 Word 的内置命令。

    为宏分配一个不同的名称(非保留)应该可以解决问题。

    【讨论】:

      【解决方案2】:

      问题或错误出在宏的名称中。我已将第一行更改为:

      子另存为()

      到

      Sub SaveDoc()

      我仍然不明白这种行为,但我想分享解决方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-29
        • 1970-01-01
        • 2023-03-27
        • 2017-08-01
        • 2010-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多