Consulting

Results 1 to 4 of 4

Thread: Solved: Get Document name

  1. #1
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location

    Solved: Get Document name

    I need to get a Word document name from a dialog box to open from Excel, but my mind's gone blank.
    Something like
    [VBA]ChDir ActiveWorkbook.Path
    fName = Application.FindFile
    Set wdDoc = wdApp.Documents.Open(fName)
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by mdmackillop
    I need to get a Word document name from a dialog box to open from Excel, but my mind's gone blank.
    Something like
    [vba]ChDir ActiveWorkbook.Path
    fName = Application.FindFile
    Set wdDoc = wdApp.Documents.Open(fName)
    [/vba]
    Don't you need to create an insatnce of Word, and open it from within there?

  3. #3
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Here's my code. It's opening the file in Word, rather than just returning the file name. I was sure there was a dialog for that, similar to browsing for a folder name.

    [vba]
    Sub DataFrom()

    'Remember: this code requires a referece to the Word object model

    Dim wdApp As New Word.Application
    Dim wdDoc As Word.Document
    Dim fName As String
    Dim i As Long
    Dim f

    ChDir ActiveWorkbook.Path
    'maybe
    fName = wdApp.Dialogs(wdDialogFileFind).Show
    'or
    fName = wdApp.Dialogs(wdDialogFileOpen).Show

    Set wdDoc = wdApp.Documents.Open(fName)


    For Each f In wdDoc.FormFields
    i = i + 1
    On Error Resume Next
    Cells(i, 1) = f.Result
    Next
    End Sub

    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Found it.
    [VBA]
    With Application.FileDialog(msoFileDialogOpen)
    .AllowMultiSelect = True
    .Show
    ' Display paths of each file selected
    For lngCount = 1 To .SelectedItems.Count
    MsgBox .SelectedItems(lngCount)
    Next lngCount
    End With

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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