Input box to find file in WORD

vane0326

Well-known Member
Joined
Aug 29, 2004
Messages
819
I have this code But for some reason it does not work. It pops up a inputbox and I type the file name in the box and hit enter it suppose to find the file in word but does nothing,

Any Ideas?

Code:
Sub FindFile()

Dim GetFile()
Dim Filename As String
On Error Resume Next
Filename = InputBox("What File do you wish to open?")
Workbooks.Open ("C:\" & Filename & ".DOC")


End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Workbooks.Open ("C:\" & Filename & ".DOC")

You're trying to open a Word .doc in Excel...This will always throw an invalid format error.

After this macro finds the file, what is it supposed to do with it?
 
Upvote 0
It suppose to open up the file in word. I put this code in the word module visual basic. But it still does not work. Do you have a code that it will work for word?
 
Upvote 0
Two pieces of advice:

(1)
Don't use variables that even come close to resembling reserved words or key words in Excel. Instead of "Filename" come up with a safer variable text name.

(2)
If this will be used by anyone other than you on your computer, use late binding to create a Word object, instead of early binding to set the object at the local system's Word version which will probably not be on everyone else's computer who uses the macro. You lose intellisense in the VBE but you don't lose sleep.



The default "YourFileName" argument is optional; modify for the myPath default source path.


Sub Test1()
Dim myFile$
myFile = InputBox _
("Enter the Word document file name:", _
"What File do you wish to open?", _
"YourFileName")
If myFile = "" Then Exit Sub

Dim myPath$, myDoc$
myPath = "C:\Your\File\Path\"
myDoc = myPath & myFile & ".doc"

If Dir(myDoc) = "" Then
MsgBox "The file ''" & myDoc & "'' was not found.", 48, "No such animal."
Exit Sub
End If

Application.ScreenUpdating = False
Dim appWord As Object
Dim wdDoc As Object
Set appWord = CreateObject("Word.Application")
appWord.Visible = True
Set wdDoc = appWord.Documents.Open(myDoc)
Set wdDoc = Nothing
Set appWord = Nothing
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Is it possible to modified this code so it could work in Microsoft Publisher?


Code:
Sub Test1() 
Dim myFile$ 
myFile = InputBox _ 
("Enter the Word document file name:", _ 
"What File do you wish to open?", _ 
"YourFileName") 
If myFile = "" Then Exit Sub 

Dim myPath$, myDoc$ 
myPath = "C:\Your\File\Path\" 
myDoc = myPath & myFile & ".doc" 

If Dir(myDoc) = "" Then 
MsgBox "The file ''" & myDoc & "'' was not found.", 48, "No such animal." 
Exit Sub 
End If 

Application.ScreenUpdating = False 
Dim appWord As Object 
Dim wdDoc As Object 
Set appWord = CreateObject("Word.Application") 
appWord.Visible = True 
Set wdDoc = appWord.Documents.Open(myDoc) 
Set wdDoc = Nothing 
Set appWord = Nothing 
Application.ScreenUpdating = True 
End Sub
 
Upvote 0
I played around with this after your last post and got close to making it happen for Publisher but kept getting errors in both early and late binding approaches. I'll defer to people who use Publisher to come up with a solution if there is one. I am not sure that Publisher is as robust a program as Word and Excel is, in terms of supporting the level of VBA required to do this. Maybe it is, but none of my clients have ever called on me to program Publisher and your question is not something I could confidently post a bulletproof solution for. Hopefully someone reading this with more Publisher programming experience can provide a real workable solution without the errors I kept running into.
 
Upvote 0
Can you post what you got so maybe I could play around with it also? This weekend I'm going to buy microsoft publishing book maybe I could find something in there and use what you got and to combind something. Is that ok? If not thats ok.

I would like to know is this possible? That I have a excel userform, I can input a file name in a text box then it will convert word documentt to a jpeg and it will display on the userform in a imagecontrol box. Can that be done?
 
Upvote 0
vane0326 said:
Can you post what you got so maybe I could play around with it also?
No - - I won't post what I know does not work. If I get a chance I'll take a look at it again over the holiday weekend and if I come up with anything that works I'll post it.
 
Upvote 0

Forum statistics

Threads
1,224,845
Messages
6,181,301
Members
453,031
Latest member
Chris_1

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