Activating the "Save As..." Dialog in Word from Access (1 Viewer)

David R

I know a few things...
Local time
Today, 04:34
Joined
Oct 23, 2001
Messages
2,633
I'm finding it hard to believe that this is as hard as it appears to be. I'm going to blame deafness from the fireworks over the last weekend... :rolleyes:

Anyway, what I want to do is open a Word Template from a button in my database's main form, then immediately prompt the user for a filename and location via Save As. I don't want to use late bindings, as everyone in our office has Office 2003. However I have nothing in Tools>References for "Microsoft Word 2003", and adding the reference for "Microsoft Office 11.0 Object Library" failed to dispel the error message saying that all of the Word-specific variables and commands were undefined.

Code:
Private Sub buttonMonthlyReport_Click()
' Create a Word document from template.
   Dim WordApp As [COLOR="Red"]Word.Application[/COLOR]
   Dim strTemplateLocation As String
  
   ' Specify location of template
   strTemplateLocation = "\\ng-ncsdmaster\NAC\Center for Community Solutions\Office Procedures\Forms-Spreadsheets\Monthly Report (Fill-in-Form).dot"
    
    
   On Error Resume Next
   Set WordApp = GetObject(, "Word.Application")
   If Err.Number <> 0 Then
     Set WordApp = CreateObject("Word.Application")
   End If
   On Error GoTo ErrHandler
   
   
   WordApp.Visible = True
   WordApp.WindowState = [COLOR="Red"]wdWindowStateMinimize  [/COLOR]'value: 1
   WordApp.Documents.Add Template:=strTemplateLocation, NewTemplate:=False
    
   DoEvents
   WordApp.Activate
   
    With [COLOR="Red"]Dialogs[/COLOR]([COLOR="Red"]wdDialogFileSaveAs[/COLOR]) 'value: 80
        .Name = "C:\My Documents\MonthlyReport" & fOSUserName() & Month(Today()) & Day(Today()) & ".doc"
        .Show
    End With

   Set WordApp = Nothing

ErrHandler:
Set WordApp = Nothing
End Sub
Bits in red are what are throwing "user-defined type not defined" errors.

If I use late bindings and strip out all the misunderstood variables: I can get the debugger to stop choking:
Code:
WordApp.Visible = True
   WordApp.WindowState = 1
   WordApp.Documents.Add Template:=strTemplateLocation, NewTemplate:=False
    
   DoEvents
   WordApp.Activate
   
    With FileDialog(80) 'value: 80
        .InitialFileName = "C:\My Documents\MonthlyReport" & fOSUserName() & Month(Now()) & Day(Now()) & ".doc"
        .Show
    End With
It doesn't throw an error message, but if I hover over it during a pause in execution it gives me <Method 'FileDialog' of object '_Application' failed>. Not very enlightening.

Please don't suggest "Super Easy Word", as it's impenetrable and overkill for what I want. I don't need to merge any documents, I'm literally doing this as a convenience to users so they don't have to worry about finding the template on the server (and so they don't overwrite my template every month).
 
Last edited:

SOS

Registered Lunatic
Local time
Today, 02:34
Joined
Aug 27, 2008
Messages
3,517
first of all go to your references and set a reference to MICROSOFT WORD 2003.
 

David R

I know a few things...
Local time
Today, 04:34
Joined
Oct 23, 2001
Messages
2,633
... I swear I looked for that seven times, and there was nothing there until Access automagically installed the Office 11.0 library. :eek: Friggin' Microsoft.

However, even checking that made no effect. It still throws the silent error "<Method 'FileDialog' of object '_Application' failed>".
 

ByteMyzer

AWF VIP
Local time
Today, 02:34
Joined
May 3, 2004
Messages
1,409
Try changing:
Code:
With Dialogs(wdDialogFileSaveAs)
...to:
Code:
With [B][I]WordApp.[/I][/B]Dialogs(wdDialogFileSaveAs)
 

David R

I know a few things...
Local time
Today, 04:34
Joined
Oct 23, 2001
Messages
2,633
Thanks, that seems to have solved it (at least for my computer). Final working code:
Code:
Private Sub buttonMonthlyReport_Click()
' Create a Word document from template.
   Dim WordApp As Object
   Dim strTemplateLocation As String
  
   ' Specify location of template
   strTemplateLocation = "\\ng-ncsdmaster\NAC\Center for Community Solutions\Office Procedures\Forms-Spreadsheets\Monthly Report (Fill-in-Form).dot"
    
    
   On Error Resume Next
   Set WordApp = GetObject(, "Word.Application")
   If Err.Number <> 0 Then
     Set WordApp = CreateObject("Word.Application")
   End If
   On Error GoTo ErrHandler
   
   
   WordApp.Visible = True
   WordApp.WindowState = 1 'variable: wdWindowStateMinimize
   WordApp.Documents.Add Template:=strTemplateLocation, NewTemplate:=False
    
   DoEvents
   WordApp.Activate
   
    With WordApp.Dialogs(80) 'variable: wdDialogFileSaveAs
        .Name = "C:\My Documents\MonthlyReport" & Format(Date, "yyyymmdd") & ".doc"
        .Show
    End With

   Set WordApp = Nothing

ErrHandler:
Set WordApp = Nothing
Debug.Print Error
End Sub
 

drusteeby

Registered User.
Local time
Today, 04:34
Joined
Jul 9, 2009
Messages
29
I'm doing something similar, but i cannot figure out how to fill in the bookmarks that I wrote the code for. The code i have right now opens it up and asks the user to save as without filling the form in. How to i get the form to fill in before It asks the user to save?

Code:
Private Sub Command70_Click()
' Create a Word document from template.
Dim WordApp As Object
Dim strTemplateLocation As String
 
' Specify location of template
strTemplateLocation = "LOCATION OF MY TEMPLATE"
 
 
On Error Resume Next
Set WordApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set WordApp = CreateObject("Word.Application")
End If
On Error GoTo ErrHandler
 
 
WordApp.Visible = True
WordApp.WindowState = 1 'variable: wdWindowStateMinimize
WordApp.Documents.Add Template:=strTemplateLocation, NewTemplate:=False
 
DoEvents
WordApp.Activate
 
With WordApp.Dialogs(wdDialogFileSaveAs) 'variable: wdDialogFileSaveAs
.Name = "C:\My Documents\STR_Report.doc"
.Show
 
 
'Select the bookmark setup in the template
.ActiveDocument.Bookmarks("text2").Select
'Populate the bookmark with this data
.Selection.Text = (CStr(Forms![STR Log]!cmboHullNGSS))
 
'Select the bookmark setup in the template
.ActiveDocument.Bookmarks("text3").Select
'Populate the bookmark with this data
.Selection.Text = (CStr(Forms![STR Log]!cmboHullNavy))
 
...and so on 
 
 
End With
 
 
ErrHandler:
Set WordApp = Nothing
Debug.Print Error
End Sub
 

DCrake

Remembered
Local time
Today, 10:34
Joined
Jun 8, 2005
Messages
8,632
To have saved you alot of grief why did you not simply use the FileCopy command to copy your template to the users default template name then open up that one, thus leaving your original template safe and secure.

David
 

drusteeby

Registered User.
Local time
Today, 04:34
Joined
Jul 9, 2009
Messages
29
Because I am a noob, most of this is copied code. What is the Filecopy method?
 

Users who are viewing this thread

Top Bottom