VBA Help: Save As with a filter?

Posted by: tfabris

VBA Help: Save As with a filter? - 01/02/2013 23:01

Please help!

Microsoft's online help and Google searches seem to go in circles here, and I keep coming back to the same set of contradictions.

I'm writing a Macro in Microsoft Word 2010. This macro must save a datafile that contains some comma delimited data, and prompt the user for the path and file name.

The following code:

Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogSaveAs)


Works, but it forces the extension to be ".docx". I want it to be my own extension, say ".foobar" or something. Ideally, I want the user to be able to type the file name and have it automatically add ".foobar" to the file name.

So, when I try to do this:

fd.Filters.Add "Foobar files", "*.foobar", 1

... VBA gives me an error that msoFileDialogSaveAs doesn't support filters.

Okay, fine, all the stuff on the web says that when I hit this problem, change it to a file picker dialog:

Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)


That's fine and the filters work at that point, but NOW THE USER CAN'T SAVE THE FILE!. The dialog picker only works if you PICK AN EXISTING FILE, and if the user types in their own file name, then there's a "sorry file does not exist error" and the user can't save the file.

When I try searching to find out how to "Save as" with msoFileDialogFilePicker, they all say either "it should work" (it doesn't) or they say to use msoFileDialogSaveAs instead, wash rinse repeat.

Someone please help me out of this vicious circle?

NOTE: suggestions to use something other than Word or VBA will not be helpful. This has to be coded in the Word for Windows Version 10 macro language in order for it to be useful.

How do I SAVE AS WITH FILTERS in the Word VBA macro language? Anyone know?
Posted by: gbeer

Re: VBA Help: Save As with a filter? - 01/02/2013 23:10

Ugly, but, Can you save it in a temp space as .docx then rename and move it.
Posted by: gbeer

Re: VBA Help: Save As with a filter? - 01/02/2013 23:27

Oops: forgot this is the file picker problem.

This example is for excel but it might work the same in word.

http://www.vbforums.com/showthread.php?4...l=1#post3075367

If I understand it. They are removing all the file extensions from the chooser and loading back in, only one they want.
Posted by: tfabris

Re: VBA Help: Save As with a filter? - 01/02/2013 23:43

That's the one where, if I do it, it only lets the user select an existing file. The user can't save as to a new file name of their choosing. It tells them file not found.
Posted by: JBjorgen

Re: VBA Help: Save As with a filter? - 02/02/2013 03:24

Does this help at all?
Posted by: tfabris

Re: VBA Help: Save As with a filter? - 02/02/2013 03:52

Hm. Possibly. Calling the Windows API directly is certainly within my power, though messier than I'd normally want for this. If I can't find a solution in their scripting language, I'll resort to that. And that example shows how to do it in detail. THANKS!
Posted by: JBjorgen

Re: VBA Help: Save As with a filter? - 04/02/2013 03:30

Originally Posted By: tfabris
Hm. Possibly. Calling the Windows API directly is certainly within my power, though messier than I'd normally want for this. If I can't find a solution in their scripting language, I'll resort to that. And that example shows how to do it in detail. THANKS!


I lived the hell of having Access (97 no less) as a primary developing platform for 10 years. In the end, we ended up with most of that kinda stuff calling the API directly because VBA just doesn't give enough control. If you wrap it up nicely enough, it becomes just as simple and easy to use as the built in stuff.
Posted by: tfabris

Re: VBA Help: Save As with a filter? - 06/02/2013 21:44

Originally Posted By: JBjorgen
Does this help at all?


Implemented. Works.

Had to tweak the code in a couple of places where it was Access-specific, of course. But after that, works like a charm. THANKS!
Posted by: JBjorgen

Re: VBA Help: Save As with a filter? - 07/02/2013 04:29

smile Still trying to work off that lunch you bought me in Seattle...