Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word 2003 VBA and controlling SaveAs Dialog 1

Status
Not open for further replies.

bdavis96

Programmer
Feb 11, 2002
97
US
I have been searching the internet, but can't seem to put the pieces together. I have a document that when I click File->Save As, I would like to prefill the Filename textbox on the Save As Dialog with a hardcoded file name (I am guessing using VBA). But I would like it to work only on Save As, not on Save.

Example: I have an XML file called 08-09 Stafford Loan Instructions.xml that has XML, formatting, etc. When I click Save As, I want typesLoansStafford.xml to prefill in the Filename box. That will allow me to click the Save As Data Only option to create a raw XML file. If I click Save, it will continue to save the file as 08-09 Stafford Loan Instructions.xml. The reason for the prefill is because I have 50 files I want to do this to, and I don't want to remember what name I need to name the raw XML file.

If anyone can help me, that would be much appreciated. If you need more description, please let me know. Thank you.
 
Maybe I misunderstood your post, but here is what I came up with. I don't understand why you need to use the SaveAs dialog, thus I just saved the new file via code.

In the "ThisDocument" module:
Code:
Private Sub Document_Open()
    Call Register_Event_Handler
End Sub

In a Class Module named "SaveAsChanger"
Code:
Public WithEvents App As Word.Application

Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
    If SaveAsUI = True Then
        ThisDocument.SaveAs FileName:="typesLoansStafford.XML", FileFormat:=wdFormatXML, SaveFormsData:=True
        Cancel = True
    End If
End Sub

In any Module
Code:
Dim X As New SaveAsChanger
Sub Register_Event_Handler()
    Set X.App = Word.Application
End Sub

I hope this answers your question, if not then I may have misunderstood what you were asking.

BD
 
bdmangum,

Thank you for the reply. Your post showed me where I was going wrong (i.e. I forgot to name the class module to the same name as the line: dim x as new class module). And I didn't realize Word considers VBA a macro, so my default High security settings weren't allowing me to run the code.

There is one misunderstanding though. I don't want the file to "automatically" save using VBA. I just want to overwrite the filename in the SaveAs dialog, but leaving me with user control over the SaveAs dialog so I can press Save, Cancel, check mark options, etc. So I have found code that seems to work in some ways, but not in others.

If I change

ThisDocument.SaveAs

to

With Application.Dialogs(wdDialogFileSaveAs)
.Name = "typesLoansStafford.xml"
.Display
End With

It seems to work somewhat. If I hit cancel, another SaveAs Dialog box pops up with the original file name in the Filename box. That isn't a problem, but I wanted to know if there is better code that I can use.

Any help would be much appreciated. Thank you.
 
This should do it:

Code:
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
    Dim myDialog As Dialog
    If SaveAsUI = True Then
        Set myDialog = Application.Dialogs(wdDialogFileSaveAs)
        With myDialog
            .Name = "typesLoansStafford.xml"
            .Format = wdFormatXML
            If .Show = -1 Then  'Save button was selected
            Else    'Cancel button was selected
            End If
        End With
        Cancel = True   'Prevents normal SaveAs box from appearing
    End If
End Sub
 
Thank you very much, bdmangum. Everything works like a charm now. I really appreciate how you document the code so I know what is going on. I didn't realize the Cancel = True at the end was to prevent the normal SaveAs dialog from popping up. I have given your responses a star.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top