+ Reply to Thread
Results 1 to 4 of 4

Help with code: amend file path

Hybrid View

  1. #1
    Registered User
    Join Date
    02-11-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    4

    Help with code: amend file path

    Hi,

    I have the following code and i would like to know how to amend it so the user can choose a specific word file as opposed to the embedded "pushmerge.dot"

    Option Explicit

    Sub BCMerge()
    Dim pappWord As Object
    Dim docWord As Object
    Dim wb As Excel.Workbook
    Dim xlName As Excel.Name
    Dim TodayDate As String
    Dim Path As String

    Set wb = ActiveWorkbook
    TodayDate = Format(Date, "mmmm d, yyyy")
    Path = wb.Path & "\pushmerge.dot"

    On Error GoTo ErrorHandler

    'Create a new Word Session
    Set pappWord = CreateObject("Word.Application")

    On Error GoTo ErrorHandler

    'Open document in word
    Set docWord = pappWord.Documents.Add(Path)

    'Loop through names in the activeworkbook
    For Each xlName In wb.Names
    'if xlName's name is existing in document then put the value in place of the bookmark
    If docWord.Bookmarks.Exists(xlName.Name) Then
    docWord.Bookmarks(xlName.Name).Range.Text = Range(xlName.Value)
    End If
    Next xlName

    'Activate word and display document
    With pappWord
    .Visible = True
    .ActiveWindow.WindowState = 0
    .Activate
    End With

    'Release the Word object to save memory and exit macro
    ErrorExit:
    Set pappWord = Nothing
    Exit Sub

    'Error Handling routine
    ErrorHandler:
    If Err Then
    MsgBox "Error No: " & Err.Number & "; There is a problem"
    If Not pappWord Is Nothing Then
    pappWord.Quit False
    End If
    Resume ErrorExit
    End If
    End Sub

  2. #2
    Forum Contributor
    Join Date
    01-13-2013
    Location
    Oklahoma
    MS-Off Ver
    Office 2007 / Office 2010
    Posts
    123

    Re: Help with code: amend file path

    You can use this to allow the user to pick the document.
    Public Function GetPath(sTitle As String, fDialogType As MsoFileDialogType, Optional sFilterName As String, Optional sFileName As String) As String
    'Requires reference to Microsoft Office 12.0 Object Library.
    
        Dim fDialog As Office.FileDialog
        Dim varFile As Variant
        Dim sReturn As String
        'Set up the File Dialog.
        Set fDialog = Application.FileDialog(fDialogType)
        With fDialog
            'Allow user to make multiple selections in dialog box.
            .AllowMultiSelect = False
            
            .Title = sTitle
            'Clear out the current filters, and add our own.
            If sFileName <> "" And sFilterName <> "" Then
                .Filters.Clear
                .Filters.Add sFilterName, sFileName, 1
            Else
                .Filters.Clear
                .Filters.Add "All Files", "*.*", 1
            End If
            'Show the dialog box. If the .Show method returns True, the
            'user picked at least one file. If the .Show method returns
            'False, the user clicked Cancel.
            If .Show = True Then
            'Loop through each file selected and add it to the list box.
                For Each varFile In .SelectedItems
                    sReturn = varFile
                Next
                If fDialogType = msoFileDialogFolderPicker Then
                    If Right(sReturn, 1) <> "\" Then
                        sReturn = sReturn & "\"
                    End If
                End If
            End If
        End With
        GetPath = sReturn
    End Function
        
        ' Replace Path = wb.Path & "\pushmerge.dot" with the line below in your code
        Path = GetPath("Select Word Document", msoFileDialogFilePicker, "Word Documents", "*.dot")

  3. #3
    Registered User
    Join Date
    02-11-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Help with code: amend file path

    sorry im a bit of an amatuer, which part of the code do i replace with your suggested code?

  4. #4
    Forum Contributor
    Join Date
    01-13-2013
    Location
    Oklahoma
    MS-Off Ver
    Office 2007 / Office 2010
    Posts
    123

    Re: Help with code: amend file path

    Replace:
        Path = wb.Path & "\pushmerge.dot"
    with this:
        Path = GetPath("Select Word Document", msoFileDialogFilePicker, "Word Documents", "*.dot")
        if path = "" then exit sub
    Just be sure to include the function GetPath in a module.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1