GetOpenFilename problem

henrik2h

Board Regular
Joined
Aug 25, 2008
Messages
155
Office Version
  1. 2021
Platform
  1. Windows
I have this macro to copy data from Excel and paste into Word. I am trying to get the file picker to work but throws me an error. If no file is selected it works as expected, the macro ends. If a file is selected I get "run error 13, incompatible types" (or something like that in English) on the row marked below. Any ideas?

VBA Code:
Dim wb As Workbook
Dim wApp As Word.Application
Dim wDoc As Object
Dim wFileSelect As String

Set wb = ActiveWorkbook
    
    Set wApp = CreateObject("Word.Application")
            
    'Choose the target Word file
    
    wFileSelect = Application.GetOpenFilename("Word-files,*.doc*", _
        1, "Select Source File To Open", , False)
    
    'if the user didn't select a file, exit sub
    If wFileSelect = False Then    '''ERROR HERE!
        Set wApp = Nothing
        Set wb = Nothing
        Application.ScreenUpdating = True
        Exit Sub
    End If
    
    ' Open the word file
    
    wApp.Visible = True
    Documents.Open wFileSelect
    Set wDoc = ActiveDocument
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Change
VBA Code:
 If wFileSelect = False Then    '''ERROR HERE!

to
Code:
 If wFileSelect = "False" Then
 
Upvote 0
Change
VBA Code:
 If wFileSelect = False Then    '''ERROR HERE!

to
Code:
 If wFileSelect = "False" Then

Thank you, but noop, then the error check is not working. So what I want is if no file is selected the macro stops, if a file is selected the macro continues and opens the file
 
Upvote 0
Should do as wFileSelect is defined as a String. Either that or change it to variant to accept FALSE as a Boolean.
 
Upvote 0
Solution
Should do as wFileSelect is defined as a String. Either that or change it to variant to accept FALSE as a Boolean.

It IS working when no file is selected, FALSE is returned and the macro ends. The problem is when a file actually is selected, the line should then pass by the If-block but actually throws an error. It is probably something easy but I just can't figure it out. If I use "False" and no file is choosen then the macro tries to open a file that does not exist and an error occurs.
 
Upvote 0
Odd, If I select a file it passes over the IF statement.
 
Upvote 0

Forum statistics

Threads
1,214,798
Messages
6,121,630
Members
449,041
Latest member
Postman24

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top