我正在创建一个宏:
我的问题是当我点击“保存”按钮时,没有任何反应。对话框消失但文件未保存,字段未更新且表单未卸载(最后一行)。我该如何处理这些按钮?
这是我到目前为止所做的:
Private Sub btn_generate_Click()
Dim dlgSave As FileDialog
Set dlgSave = Application.FileDialog(FileDialogType:=msoFileDialogSaveAs)
Dim oRegex As New regexp
oRegex.Pattern = "^[0-9]+$"
'Check if WO is only digits
If (oRegex.Test(txt_wo) = False) Xor (txt_wo.TextLength <> 6) Then
'Alert message
MsgBox "The WO must be 6 digits!", vbExclamation, "Error in WO number"
'Focus on the WO textbox + selection of the content
txt_wo.SetFocus
txt_wo.SelStart = 0
txt_wo.SelLength = Len(txt_wo.Text)
'Check if path to file is filled
ElseIf txt_filePath.Text = "" Then
'Alert message
MsgBox "You must choose a document to reference", vbExclamation, "Missing document"
'Focus on the "Browse"button
btn_browse.SetFocus
Else
'Put WO after the bookmark "workorder"
ActiveDocument.Bookmarks("workorder").Range.InsertAfter (" " + txt_wo.Text)
'Copy text of the chosen file after the bookmark "texttoref"
ActiveDocument.Bookmarks("texttoref").Range.Select
Selection.InsertFile (txt_filePath)
'Open "Save as..." dialog box
With dlgSave
.InitialFileName = "eRef" + txt_wo.Text + ".docx"
'If the user clicks "Save"
If .Show = False Then
ActiveDocument.Undo 2
End If
End With
Unload Form_CreateSheet
ActiveDocument.Sections(1).Headers(1).Range.Fields.Update
End If
End Sub
答案 0 :(得分:0)
如果有人可能感兴趣。这是修改后的部分
With dlgSave
'Set default name of document
.InitialFileName = "DefaultName.docx"
If .Show = False Then
'undo the insertions
ActiveDocument.Undo 2
Else
ActiveDocument.SaveAs (.InitialFileName)
End If