Consulting

Results 1 to 4 of 4

Thread: Solved: Open File Dialog

  1. #1
    VBAX Newbie
    Joined
    Aug 2010
    Posts
    5
    Location

    Solved: Open File Dialog

    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

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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:

    [VBA]
    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
    [/VBA]
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Welcome to VBA Express!

    if you're going to load the file anyway, then,
    [vba]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
    [/vba]

    David


  4. #4
    VBAX Newbie
    Joined
    Aug 2010
    Posts
    5
    Location

    Solved!

    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •