Search via Windows API via VBA to add a file to a macro to open

J

justagrunt

Hi,
I've used a program (copied and pasted it from a Microsoft recommended site)
with the use of the windows API to sellect an excel spreadsheet, get is path,
with success, when I use the same, to open a word document it gives me a
'Bad file name error.
Is there a way to use the explorer to find a word document so that it can be
used in and expression like,

Set objDocs = objWord.Documents.Add(strwordTemplate)

where

strwordTemplate

is the path to the document.
 
P

Perry

[strwordTemplate] does this contain the full filename?
In other words, if you run below sequence, does the messagebox popup?


If Dir(strwordTemplate) = "" Then
MsgBox "File does not exists"
Else
Set objDocs = objWord.Documents.Add(strwordTemplate)
End if

--
Krgrds,
Perry

System:
Vista/Office Ultimate
VS2005/VSTO2005 SE
 
H

Helmut Weber

I don't know about the explorer.
Possibly you don't need any api for what you want to do.

Try:

Sub Macro4()
Dim strFil As String
Dim strPth As String
Dim odlg As Dialog
Set odlg = Dialogs(wdDialogFileOpen)
With odlg
.Display
' doubleclick filename in listbox
strFil = .Name
strPth = WordBasic.FileNameInfo(.Name, 5)
End With
MsgBox strPth & strFil '!
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

justagrunt

Thanks Perry,
I get the message box after the API window has opened to select the file and
the selection has been made - "Bad File Name".
I will implement your valued comments and see where it takes me.
--
Best Regards
Bill (Happy esater from New Zealand)


Perry said:
[strwordTemplate] does this contain the full filename?
In other words, if you run below sequence, does the messagebox popup?


If Dir(strwordTemplate) = "" Then
MsgBox "File does not exists"
Else
Set objDocs = objWord.Documents.Add(strwordTemplate)
End if

--
Krgrds,
Perry

System:
Vista/Office Ultimate
VS2005/VSTO2005 SE
 

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