none
How to add document path to text box by using VBA form? RRS feed

  • Question

  • Hi,

    I've a simple macro for compare 02 documents by using MS Word comparing option. See below sample code:

    Sub CompareDoc()
    Dim oDoc1 As Document
    Dim oDoc2 As Document
    Set oDoc1 = Documents.Open("C:\Users\Desktop\S.docx")
    Set oDoc2 = Documents.Open("C:\Users\Desktop\O.docx")
    Application.CompareDocuments oDoc1, oDoc2, wdCompareDestinationNew, , True, True
    End Sub

    I've to integrate this macro to a user form to browse file paths. (Currently I've to manually change file path in VBA code.)

    Any idea/steps to do this? Thanks!


    .


    • Edited by Sam1085 Monday, August 22, 2016 2:54 PM Update
    Monday, August 22, 2016 2:53 PM

Answers

  • Not sure if you want a folder or a file dialog. Here is a file dialog. Call it with the root path you want (e.g. C:\CompareFiles). It will return the file as a string you can use in your suggested dialog UserForm1:

    '*********************************************
    '* Open dialog to choose file
    '*
    Function GetFile(theNetworkPath As String) As String
        Dim fd As Object

        GetFile = ""
        Set fd = Application.FileDialog(msoFileDialogOpen)
        With fd
            .Filters.Clear
            .Filters.Add "Excel Files", "*.xlsx"  'Adjust as needed
            .Title = "Choose file to compare (with)"
            .AllowMultiSelect = False
            .InitialFilename = theNetworkPath
            If .Show Then GetFile = .SelectedItems(1)
        End With
        Set fd = Nothing
    End Function


    Best regards, George

    Monday, August 22, 2016 3:11 PM
  • Hi,

    I've a simple macro for compare 02 documents by using MS Word comparing option. See below sample

    ...

    Any idea/steps to do this? Thanks!



    Any reason for not using the CompareDocuments dialogue? For example:

    Application.Dialogs(wdDialogToolsCompareDocuments).Show


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Sam1085 Tuesday, August 23, 2016 5:56 AM
    Tuesday, August 23, 2016 3:30 AM

All replies

  • Not sure if you want a folder or a file dialog. Here is a file dialog. Call it with the root path you want (e.g. C:\CompareFiles). It will return the file as a string you can use in your suggested dialog UserForm1:

    '*********************************************
    '* Open dialog to choose file
    '*
    Function GetFile(theNetworkPath As String) As String
        Dim fd As Object

        GetFile = ""
        Set fd = Application.FileDialog(msoFileDialogOpen)
        With fd
            .Filters.Clear
            .Filters.Add "Excel Files", "*.xlsx"  'Adjust as needed
            .Title = "Choose file to compare (with)"
            .AllowMultiSelect = False
            .InitialFilename = theNetworkPath
            If .Show Then GetFile = .SelectedItems(1)
        End With
        Set fd = Nothing
    End Function


    Best regards, George

    Monday, August 22, 2016 3:11 PM
  • Hi George,

    Thanks for the quick response. Yes, I need to open a file (*.doc & *.docx). Not a folder.

    But I've no idea about how to combine this with your code. Appreciate if you can you give me step by step guidance for this.

    Thanks!


    .

    Monday, August 22, 2016 3:21 PM
  • In the form code

    1. myFile = GetFile(rootPath)
    2. Assign text box Source to myFile (Me.Source = myFile)


    Best regards, George




    Monday, August 22, 2016 3:33 PM
  • Hi,

    Thanks for the steps guidance. But I didn't understand it properly.

    So, Now I just tried this code to open browse window. But I don't know how to get this information (File path) to textbox1.

    Private Sub Command1_Click()
    Dim Obj As Object
    Set Obj = Application.FileDialog(3)
    Obj.AllowMultiSelect = True
    If Obj.Show Then
        For i = 1 To Obj.SelectedItems.Count
            sFile = Filename(Obj.SelectedItems(i), sPath)
        Next
    End If
    End Sub

    Thanks!


    .


    • Edited by Sam1085 Monday, August 22, 2016 3:54 PM Update
    Monday, August 22, 2016 3:54 PM
  • You need to google and experiment. We cannot transfer our knowledge to you if you do not want to learn.

    Best regards, George

    Monday, August 22, 2016 3:56 PM
  • Thanks & I know I've to learn many things,

    I got the answer now. No one knows everything.


    .

    Monday, August 22, 2016 4:05 PM
  • You're ungrateful because you had to work some. Poor personal performance blamed on others ... go figure ...

    Best regards, George

    Monday, August 22, 2016 4:09 PM
  • Hi,

    I've a simple macro for compare 02 documents by using MS Word comparing option. See below sample

    ...

    Any idea/steps to do this? Thanks!



    Any reason for not using the CompareDocuments dialogue? For example:

    Application.Dialogs(wdDialogToolsCompareDocuments).Show


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Sam1085 Tuesday, August 23, 2016 5:56 AM
    Tuesday, August 23, 2016 3:30 AM
  • Hi,

    Thanks for your idea. Actually I need simple form for that. Document Compare dialogue box is more complicated. That's why I'm using this way.


    .

    Tuesday, August 23, 2016 5:56 AM