PDA

View Full Version : Solved: How to catch the user's Save As op (FileSaveAs, Save As)



avi10000
12-26-2012, 04:57 AM
Sub FileSaveAs()

Dim dlgSaveAs As FileDialog
Dim ret As Integer
Dim iFormat As Integer
Dim sTargetFilename As String

Set dlgSaveAs = Application.FileDialog(msoFileDialogSaveAs)

With dlgSaveAs
.InitialFileName = ActiveDocument.FullName
ret = .Show
iFormat = .FilterIndex

Select Case iFormat
Case 2, 3, 5, 6
Case Else ' Other values.
MsgBox ("This is an invalid Word format." & vbCr & "Please save only to Word formats supporting macros.")
Exit Sub
End Select

MsgBox ("Friendly development message." & vbCr & "Valid format. Saving ..." & vbCr & "Click OK.")

sTargetFilename = .SelectedItems(1)
ActiveDocument.SaveAs FileName:=sTargetFilename, FileFormat:=ConvFormat(iFormat)

End With

End Sub

Function ConvFormat(inFormat As Integer) As Integer
ConvFormat = Switch(inFormat = 2, wdFormatXMLDocumentMacroEnabled, _
inFormat = 3, wdFormatDocument, _
inFormat = 5, wdFormatXMLTemplateMacroEnabled, _
inFormat = 6, wdFormatTemplate)
End Function

'Filter List Format - from the Save as type drop-down list
'2 - docm
'3 - doc
'4 - dotm
'5 - dot


'WdSaveFormat Enumeration
[/SIZE][/SIZE] 'Specifies the format to use when saving a document.
'-----------------
'Name Value Description
'wdFormatDocument 0 Microsoft Word format.
'wdFormatDOSText 4 Microsoft DOS text format.
'wdFormatDOSTextLineBreaks 5 Microsoft DOS text with line breaks preserved.
'wdFormatEncodedText 7 Encoded text format.
'wdFormatFilteredHTML 10 Filtered HTML format.
'wdFormatHTML 8 Standard HTML format.
'wdFormatRTF 6 Rich text format (RTF).
'wdFormatTemplate 1 Word template format.
'wdFormatText 2 Microsoft Windows text format.
'wdFormatTextLineBreaks 3 Windows text format with line breaks preserved.'
'wdFormatUnicodeText 7 Unicode text format.
'wdFormatWebArchive 9 Web archive format.
'wdFormatXML 11 Extensible Markup Language (XML) format.
'wdFormatDocument97 0 Microsoft Word 97 document format.
'wdFormatDocumentDefault 16 Word default document file format. For Word 2010, this is the DOCX format.'
'wdFormatPDF 17 PDF format.
'wdFormatTemplate97 1 Word 97 template format.
'wdFormatXMLDocument 12 XML document format.
'wdFormatXMLDocumentMacroEnabled 13 XML document format with macros enabled.
'wdFormatXMLTemplate 14 XML template format.
'wdFormatXMLTemplateMacroEnabled 15 XML template format with macros enabled.
'wdFormatXPS 18 XPS format.