Please Help: Type mismatch error



Hi All,

I am trying to create a button 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


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

wdApp.ActiveDocument.SaveAs FileName:=FName

wdApp.Visible = True

Set doc = Nothing
Set wdApp = Nothing

End Sub

Please help!

Thanks in advance

Chip Pearson

The problem is that


doesn't return a file name. You can see this with code like

Sub AAA()
Dim fdialog As Variant
fdialog = Application.FileDialog(msoFileDialogFilePicker).Show
MsgBox "FDialog: " & CStr(fdialog) & vbNewLine & _
"TypeName: " & TypeName(fdialog)
End Sub

Use the following code to get a file dialog and the selected file

Sub AAA()
Dim FName As Variant
Dim Filter As String
Filter = "All Files (*.*),*.*"
' OR
Filter = "Word Files (*.doc),*.doc*"
' OR
Filter = "Excel Files (*.xls*),*.xls*"
FName = Application.GetOpenFilename(filefilter:=Filter, _
If FName = False Then
MsgBox "cancelled"
MsgBox "File Name: " & FName
End If
End Sub

Note that GetOpenFileName does NOT actually open the file. It simply
returns the file name(s) of the selected file(s) as a string or array
of strings. It returns False (the value False, not the string "False")
if the user cancels the open operation.

Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
(email on web site)

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