An option to either select a file, or choose the active document that is open at the time the macro is run

RoundRocket

New Member
Joined
Nov 28, 2013
Messages
24
Hi,
Below is the starting code of a macro in excel 2007 which does a find and replace for a word document. It is currently set up to allow the user to navigate to a file, which works fine...but i would like to be able to give the user the option to either navigate to, or, just use the active document that is open at the time the macro is run. I am not sure how to amend this code, but i'm guessing it would involve a msgbox with a choice, so that the user can click 'yes' to navigate to a file as it is currently set to do, or 'no' to use current open document. Any help /suggestions you could give would be a fantastic help. Thank you, Alex


Sub Button5_Click()
Dim file
file = Application.GetOpenFilename(Title:="Please choose a file to import")

If file = "" Or file = False Then
MsgBox "You must select a file, please try again"
Exit Sub
End If
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Maybe:
Code:
Dim ans as long
ans = MsgBox("Use an open file?", vbYesNo)
If ans = vbNo Then
    'GetOpenFilename code
Else
    'Other code
End If
End Sub
 
Upvote 0
This is what i came up with based on the above help, thanks again:

Sub Button5_Click()
Dim file As String
Dim check As Long

check = MsgBox("Would you like to use the open document?", vbYesNo)

If check = vbNo Then
file = Application.GetOpenFilename(Title:="Please choose a file to import")
Else
file = ActiveDocument.path & "\" & ActiveDocument.Name
End If
 
Upvote 0

Forum statistics

Threads
1,215,377
Messages
6,124,597
Members
449,174
Latest member
chandan4057

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