PDA

View Full Version : Catching SaveAs (FileSaveAs)



avi10000
12-17-2012, 12:01 PM
Hi all,

I want to know what is the most straightforward way to catch the user's FileSaveAs command and then process it.

Once I have caught the command, I want to test which output format it is, and then depending on whether it is an allowed format I will allow the SaveAs to go ahead.

So ... so far so good ...

So I defined this 'catch' subroutine in the 'ThisDocument' module


Sub FileSaveAs()


Set dlgptr = Dialogs(wdDialogFileSaveAs)
With dlgptr
.Show
iFormat = dlgptr.Format
....
....
End sub


however once inside that 'catch' I don't know of a way to halt the execution of the Save if I don't like the user's selected output format (and instead just display a msg).

I tried halting the Sub by doing 'Exit Sub', but that didn't help. The SaveAs went ahead and saved the file anyway. I.e., the VBA did NOT require an Execute command:

Dialogs(wdDialogFileSaveAs).Execute

------------

So anyway, I used another method to get what I wanted; I trapped the

_DocumentBeforeSave event

And I got that to work but it has disadvantages.

So I want to know if can get the FileSaveAs subroutine to do the same thing.

TIA

avraham

avi10000
12-18-2012, 11:09 AM
Using the FileDialog object could help. As long as it is not buggy.

Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(FileDialogType:=msoFileDialogSaveAs )


I will have to check it out.

-a




Hi all,

I want to know what is the most straightforward way to catch the user's FileSaveAs command and then process it.

Once I have caught the command, I want to test which output format it is, and then depending on whether it is an allowed format I will allow the SaveAs to go ahead.

So ... so far so good ...

So I defined this 'catch' subroutine in the 'ThisDocument' module


Sub FileSaveAs()


Set dlgptr = Dialogs(wdDialogFileSaveAs)
With dlgptr
.Show
iFormat = dlgptr.Format
....
....
End sub


however once inside that 'catch' I don't know of a way to halt the execution of the Save if I don't like the user's selected output format (and instead just display a msg).

I tried halting the Sub by doing 'Exit Sub', but that didn't help. The SaveAs went ahead and saved the file anyway. I.e., the VBA did NOT require an Execute command:

Dialogs(wdDialogFileSaveAs).Execute

------------

So anyway, I used another method to get what I wanted; I trapped the

_DocumentBeforeSave event

And I got that to work but it has disadvantages.

So I want to know if can get the FileSaveAs subroutine to do the same thing.

TIA

avraham

fumei
12-18-2012, 05:53 PM
I am a little confused. Do you have an actual question?

avi10000
12-19-2012, 03:10 AM
I am a little confused. Do you have an actual question?

Yes.

I want to know what is the most straightforward way to catch a user's 'Save As' command (clicking File| Save As to open the Save As dialog box).

This includes choosing to ignore the user's OK command to execute the save on the selected file.

I.e., once the user has selected a file and clicked OK to save the file, I want to be able to use VBA to choose to ignore it, and NOT save the file.


Method 1: First I tried this:


Sub FileSaveAs()
Set dlgptr = Dialogs(wdDialogFileSaveAs)
With dlgptr
.Show
iFormat = dlgptr.Format
....
....
End sub

But I didn't know how use this method to cancel the user's save.

Method 2: Then I tried another method: to trap the _DocumentBeforeSave event.

I assumed that would work. In the VBA documentation it says that I just have to put the following statement inside the event handler:


Cancel = true

But that also did not work: as soon as the user clicked OK, the file was actually saved.

So ... questions:

1. Is there a way to use Method 1 above to achieve what I want?
2. Is there a way to fix Method 2 above to achieve what I want?

Thanks,
Avraham

fumei
12-19-2012, 09:28 AM
Please post your entire code. Everything you used.

avi10000
12-19-2012, 09:40 AM
Method 1:

Sub FileSaveAs()

'0 1 13 15
Dim dlgptr As Dialog
Dim iFormat As Integer
Set dlgptr = Dialogs(wdDialogFileSaveAs)
With dlgptr
.Format = 1
.Show
iFormat = dlgptr.Format
Select Case iFormat
Case 0, 1, 13, 15
FileSaveAs_ValidFormat = True
Case Else
FileSaveAs_ValidFormat = False
End Select

End With

'HOW DO I ABORT THE SAVE OP?

End Sub

avi10000
12-19-2012, 09:42 AM
Method 2:


Option Explicit



Public WithEvents appWord As Word.Application

Private Sub appWord_DocumentBeforeSave _
(ByVal Doc As Document, _
SaveAsUI As Boolean, _
Cancel As Boolean)

Dim dlgptr As Dialog
Dim ret As Integer
Dim fn As String
Dim iFormat As Integer
Dim FileSaveAs_ValidFormat As Boolean

On Error Resume Next

If SaveAsUI = False Then GoTo SKIP_TRAP 'For 'Save op' skips this whole handler body

Cancel = True

If IsActiveDoc_SaveAsProtected() = False Then GoTo SKIP_TRAP

Set dlgptr = Dialogs(wdDialogFileSaveAs)

iFormat = dlgptr.Format

'intResponse = MsgBox("Do you really want to " _
& "save the document?", _
vbYesNo)

ret = dlgptr.Show

If ret = 0 Then GoTo SKIP_TRAP

iFormat = dlgptr.Format

Select Case iFormat
Case 0, 1, 13, 15
FileSaveAs_ValidFormat = True
Case Else
FileSaveAs_ValidFormat = False
End Select

If FileSaveAs_ValidFormat Then
fn = ActiveDocument.Path & "\" & dlgptr.Name
MsgBox ("Friendly development message." & vbCr & "Valid format. Saving ..." & vbCr & "Click OK.")
ActiveDocument.SaveAs FileName:=dlgptr.Name, _
FileFormat:=iFormat
Else
MsgBox ("This is an invalid format." & vbCr & "Save only to formats supporting macros.")
End
End If


SKIP_TRAP:

Cancel = True ' Add one more time - just for luck - and Do not display the SaveAs dlg box (again)

End Sub




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

fumei
12-19-2012, 11:09 AM
Sub FileSaveAs()
Dim dlgptr As Dialog
Dim iFormat As Integer
Set dlgptr = Dialogs(wdDialogFileSaveAs)
With dlgptr
.Format = 1
.Show
iFormat = dlgptr.Format
Select Case iFormat
Case Is <> 0, 1, 13, 15
MsgBox "No way."
Exit Sub
End Select
End With
End SubIf the format is not 0, 1, 13 or 15 the document saveas is rejected. No event handler required.

avi10000
12-19-2012, 11:32 AM
Hi fumei,

I wish your soln. worked for me.
Did it work for you?

It did not work for me.

I opened the Save As dlg and selected the RTF format (a "forbidden" format type) and Word saved it as the RTF as soon as I clicked OK.

FYI, the 'Select Case iFormat' statement worked correctly and I got the "No way" msg.

-avraham

I wish

Sub FileSaveAs()
Dim dlgptr As Dialog
Dim iFormat As Integer
Set dlgptr = Dialogs(wdDialogFileSaveAs)
With dlgptr
.Format = 1
.Show
iFormat = dlgptr.Format
Select Case iFormat
Case Is <> 0, 1, 13, 15
MsgBox "No way."
Exit Sub
End Select
End With
End SubIf the format is not 0, 1, 13 or 15 the document saveas is rejected. No event handler required.

fumei
12-19-2012, 01:34 PM
Darn...and that is what I get for not testing.

hmmmm, and now that I am actually looking at things, I am wondering if what you ask is even possible.

avi10000
12-20-2012, 02:42 AM
>> I am wondering if what you ask is even possible.

1. (i) The API itself implies that you should be able to abort a save op.
(ii) In my Method #2: the documentation explicitly says that you can abort a save op. See:
http://msdn.microsoft.com/en-us/library/office/aa211877(v=office.11).aspx.

2. Well, here is an additional approach that I did not try yet:

Dim dlgSaveAs As FileDialog
Set dlgSaveAs = Application.FileDialog( FileDialogType:=msoFileDialogSaveAs)
With dlgSaveAs
...
...
...
End With

avi10000
12-20-2012, 03:05 AM
That also should work ...



>> I am wondering if what you ask is even possible.

1. (i) The API itself implies that you should be able to abort a save op.
(ii) In my Method #2: the documentation explicitly says that you can abort a save op. See:
http://msdn.microsoft.com/en-us/library/office/aa211877(v=office.11).aspx.

2. Well, here is an additional approach that I did not try yet:

Dim dlgSaveAs As FileDialog
Set dlgSaveAs = Application.FileDialog( FileDialogType:=msoFileDialogSaveAs)
With dlgSaveAs
...
...
...
End With

avi10000
12-20-2012, 12:30 PM
Darn...and that is what I get for not testing.

hmmmm, and now that I am actually looking at things, I am wondering if what you ask is even possible.

Hi fumei,

This does it. See below.
But really sad that msoFileDialogSaveAs does not provide full suoport for filters.

-avraham


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
'dlgSaveAs.Filters (iFormat)

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

'fn = ActiveDocument.FullName
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)
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
'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.

avi10000
12-20-2012, 12:31 PM
And then you can add a loop to repeat opening the Save As dlg box to offer the user a further chance to do a valid Save As.

-avraham

avi10000
12-26-2012, 04:42 AM
There was a bug in the below:


Function ConvFormat(inFormat As Integer)

should be:


Function ConvFormat(inFormat As Integer) As Integer

-avraham




Hi fumei,

This does it. See below.
But really sad that msoFileDialogSaveAs does not provide full suoport for filters.

-avraham


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
'dlgSaveAs.Filters (iFormat)

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

'fn = ActiveDocument.FullName
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)
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
'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.