Type Mismatch error!!

S

Sam

Hi All,

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
 
O

OssieMac

Hi Sam,

Not sure that I have interpreted your question correctly. You appear to be
opening an existing document with a template. I thought that you only use
templates if creating a new document but then I am not really proficient in
word. However, the following uses the file dialog to open an existing Word
document.

If you use the Early binding method of declaring variables then in the VBA
editor you need to select tools -> references and scroll down until you find
Microsoft Word XX.0 Object Library where XX is the version (10 is Office
2002, 11 is 2003 and 12 is 2007) .Ensure you check the box not just select
the line. I usually use this method at least until I have completed the code
then use Late binding because by using early binding the VBA intellisence
helps you with the code. The advantage of late binding is that if you take
the code from say 2003 version to 2002 version then the reference needs to be
reset by the user. Going from 2002 to 2003 is not a problem.

Feel free to get back to me if you any problems with the code.

Sub test()
'Dim wdApp As Word.Application 'Early binding method
'Dim doc As Word.Document 'Early binding method

Dim wdApp As Object 'Late binding method
Dim doc As Object 'Late binding method
Dim fdialog As FileDialog
Dim pathAndFile As String
Dim filePath As String
Dim shortName As String

filePath = ThisWorkbook.Path 'for testing. Can use a string for your path.

On Error Resume Next
Set wdApp = GetObject(, "Word.Application")

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

wdApp.Visible = True 'for testing. Can be false

Dim FName As String

Set fdialog = wdApp.FileDialog(msoFileDialogFilePicker)

With fdialog
.AllowMultiSelect = False
.Filters.Clear
.InitialFileName = filePath & "\*.doc*"

'Use the Show method to display the File Picker
'dialog box and return the user's action.
If .Show Then
pathAndFile = .SelectedItems(1)
'following is filename without path if required
shortName = Right(pathAndFile, _
Len(pathAndFile) - InStrRev(pathAndFile, "\"))
Else
MsgBox "User cancelled. Did not select a file"
GoTo PastCode
End If
End With

Set fdialog = Nothing

Set doc = wdApp.Documents.Open(pathAndFile) 'Open selected file

'****************************************************
'This part of code between asterisk lines untested
'but I think it should work OK.
doc.Variables("Address").Value = Me.Address.Value
doc.Variables("City").Value = Me.City.Value
doc.Fields.Update
'***************************************************

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

doc.SaveAs Filename:=FName

PastCode:

wdApp.Quit 'quit before setting variables to nothing
Set doc = Nothing
Set wdApp = Nothing

End Sub
 
C

Chip Pearson

The problem is that

fDialog = Application.FileDialog(msoFileDialogFilePicker).Show

does not return a file name, so you cannot use the fDialog result in
the Open method, which expects a String file name.

See my reply to your other post on this topic today, 4-Sept.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(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

Top