PDA

View Full Version : Solved: Open File Dialog



NeroMaj
08-26-2010, 05:58 AM
I am writing a Macro in Word VBA that will open another word file, read some data from a specific table in that file (I know Excel is easier for readign tables, but not possible for my particular goals), and then calculate some values.

The only problem I am having is referencing the newly opened file once it is opened.

I have the following setup:

*****************************************************
Dim wdApp as Word.Application
Dim wdDoc as Word.Document

Sub OpenSingleFile()
Dialogs(wdDialogFileOpen).Show
End Sub

I know how to set the different properties of a dialog box and all of that, but how can I return the file path name...e.g. If file was "Compliance Evaluation" I would want to return "c:\My Documents\Compliance Evaluation.doc"

**************************************************
*What code here can return the path name of the file or is it even
*possible? As you can see I want to get the file path and then
*use that to open the document as part of wdApp collection so
*that I can use With wdApp to navigate through the document
**************************************************
Sub OpenSingleFile()
Dim FileName as string

'Dialogs(wdDialogFileOpen).Show

FileName = ??????????????????

On Error Resume Next
Set wdApp = CreateObject("Word.Application")
If Err.Number<> 0 Then
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0

wdDoc = wdApp.Documents.?????????????????
End Sub

gmaxey
08-26-2010, 03:55 PM
I don't follow what you are trying to do exactly, but if you want to know the path and name of a file selected in the file open dialog then use something like this:


Sub ScratchMacro()
Dim pName As String
With Dialogs(wdDialogFileOpen)
If .Display Then
pName = WordBasic.FilenameInfo$(.Name, 1)
Else
MsgBox "No file selected"
End If
End With

Tinbendr
08-27-2010, 05:21 AM
Welcome to VBA Express!

if you're going to load the file anyway, then,
Sub ScratchMacroTB()
Dim pName As String
Dim bDoc As Document
Dim AppPath As String
With Dialogs(wdDialogFileOpen)
If .Display Then
If .Name <> "" Then
Set bDoc = Documents.Open(.Name)
AppPath = bDoc.Path
End If
Else
MsgBox "No file selected"
End If
End With
End Sub

NeroMaj
08-27-2010, 07:38 AM
Both solutions worked like a charm. I used gmaxeys so that I could explicity set wdapp and wddoc (personal style i guess), but either is a good way to do it.

I got it up and running in minutes and now I'm on to the next step of the process. Thanks alot!