Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Sam Sam is offline
external usenet poster
 
Posts: 699
Default 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).Sh ow

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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Please Help: Type mismatch error

The problem is that

Application.FileDialog(msoFileDialogFilePicker).Sh ow

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

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

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

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, _
MultiSelect:=False)
If FName = False Then
MsgBox "cancelled"
Else
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.


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




On Fri, 4 Sep 2009 08:05:01 -0700, sam
wrote:

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).Sh ow

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

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Visual Basic Error Run Time Error, Type Mismatch Meg Partridge Excel Discussion (Misc queries) 12 September 10th 08 06:10 PM
runtime error 13 - type mismatch error in Excel 97 on Citrix Kevin Maher Excel Programming 7 March 8th 08 11:48 AM
Conditional Formatting - Run Time Error '13' Type Mismatch Error ksp Excel Programming 0 July 11th 06 07:06 AM
Help: Compile error: type mismatch: array or user defined type expected lvcha.gouqizi Excel Programming 1 October 31st 05 08:20 PM
Befuddled with For Next Loop ------ Run - Time Error '13' Type Mismatch Error rdavis7408 Excel Programming 1 August 25th 04 03:54 AM


All times are GMT +1. The time now is 07:46 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"