Please Help!: Type Mismatch error

S

sam

Hi All,

This is an access forms question but as word is involved in this I am
posting it here too.
I am trying to create a button on access form that would export some of the
fields to a word template. I think I have it right but I am getting a type
mismatch error on this line:

Set doc = wdApp.Documents.Open(fDialog)

what might be the reason? Here is the whole code:

Dim wdApp As Object
Dim doc As Object
On Error Resume Next
Set wdApp = GetObject("C:\My Documents\Address.dotx", "Word.Application")

If Err.Number <> 0 Then 'Word isn't already running
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0

Dim FName As String

fDialog = Application.FileDialog(msoFileDialogFilePicker).Show

On Error Resume Next
Set doc = wdApp.Documents.Open(fDialog)

wdApp.ActiveDocument.Variables("Address").Value = Me.Address.Value
wdApp.ActiveDocument.Variables("City").Value = Me.City.Value

wdApp.ActiveDocument.Fields.Update

FName = "C:\My Documents\" & "Address" & ".doc"

wdApp.ActiveDocument.SaveAs FileName:=FName

wdApp.Visible = True

Set doc = Nothing
Set wdApp = Nothing
wApp.Quit

End Sub

Please help!

Thanks in advance
 
J

Jay Freedman

Because it isn't declared, fDialog is a Variant. The value returned by
Application.FileDialog(msoFileDialogFilePicker).Show is a Long value
indicating which button was pressed; if it was the OK button then its value
is -1. You're then trying to use that as the argument of the
wdApp.Documents.Open method, which expects a file name. That's why you have
a type mismatch.

What you should do is something like this:

Dim doc As Word.Document ' not Object
Dim Dlg As FileDialog
Dim fDialog As Long
Dim fName As String
Set Dlg = Application.FileDialog(msoFileDialogFilePicker)

With Dlg
fDialog = .Show
If (fDialog <> -1) Then Exit Sub ' user canceled
fName = .SelectedItems(1)
End With
Set Dlg = Nothing ' allow garbage collection of object

Set doc = wdApp.Documents.Open(fName)

You should also use the "doc" document object throughout the rest of the
code instead of wdApp.ActiveDocument because it's safer (you don't really
know that the document you just opened will always be the active document).

Finally, I'm suspicious of the line

FName = "C:\My Documents\" & "Address" & ".doc"

There is no variable part on the right side; the result will always be the
literal string "C:\My Documents\Address.doc". Is that what you intended? If
so, why bother to allow the user to see the file picker dialog at all?

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
J

Jay Freedman

One other item I missed before: You need to call wdApp.Quit before you set
wdApp = Nothing. The reversed order in your post would result in an error,
because you can't call a method of an object that no longer points to
anything.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top