How to Save a Document from File Picker for File Comparison

Cook09
Cook09 used Ask the Experts™
on
The dilemma: Perform a Comparison of two Documents, and have the user to be able to select the documents via a File Picker, run the comparison and save in a Temp File.  The current code that I have is:

Sub CompareDocuments()
Dim strFile1, strFile2 
Dim strPath1, strPath2 , strPath3 
Dim Doc1 As Document
Dim Doc2 As Document
Dim Doc3 As Document

         ' Where the Documents are saved as a Constants
    Const Path1 As String = "C:\Documents and Settings\My Account\My Documents\Temp\Doc1.docx"
    Const Path2 As String = "C:\Documents and Settings\My Account\My Documents\Temp\Doc2.docx"
    Const Path3 As String = "C:\Documents and Settings\My Account\My Documents\Temp\Doc3.docx"

      With Application.FileDialog(msoFileDialogFilePicker)
      .Title = "Select Original Document For Comparison"
      .Show
      'Provides the path and filename
   strPath1 = .SelectedItems(1)             
  Documents.Open FileName:=strPath1, ReadOnly:=False, Visible:=True
     Set strPath1 = ActiveDocument
    strPath1 = Doc1
     Set Doc1 = ActiveDocument
     Debug.Print Doc1
 '   ==>   Doc1.SaveAs Path1, wdFormatDocumentDefault
End With 

              'Select Revised File 
     With Application.FileDialog(msoFileDialogFilePicker)
      .Title = "Select Revised File For Comparison"
      .Show
      'Provides the path and filename
   strPath2 = .SelectedItems(1)             
  Documents.Open FileName:=strPath2, ReadOnly:=False, Visible:=True
             'Provides just the Document Name
     Set strPath2 = ActiveDocument
     Set Doc2 = ActiveDocument
     Debug.Print Doc2
 '   ==>   Doc2.SaveAs Path2, wdFormatDocumentDefault
End With 

    Set Doc3 = Application.CompareDocuments(doc1, doc2, _
     Destination:=wdCompareDestinationNew, _
     Granularity:=wdGranularityWordLevel, _
     CompareFormatting:=True, _
     CompareCaseChanges:=True, _
     CompareWhiteSpace:=True)
    
    Doc3.SaveAs2 Path3, wdFormatDocumentDefault

End With

Open in new window


I don't understand why it won't save in the Temp Directory. I've tried several diferent ways of doing this, as can be seen between 1&2. Any Ideas??
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Are you sure that the path identified in Path3 actually exists?

If replace the constant for Path3 to use a path (not including the filename) that already exists on my machine ("c:\temp2\"), then I'm able to save Doc3.docx as the CompareResult document (if I also comment line 20, that is).

I copied your code and tried running it on my own machine, and found that I had to comment line 20 ("strPath = Doc1") in order to continue beyond.

Of course, the two lines where you're saving the documents you've opened have been commented as well: Lines 23 and 37. That means we're working with the two open documents in the CompareDocuments that begins on line 40.

When we get to line 47, where we're actually saving the compared document, I get an error with the pathname; it's attempting to save to the value of Path3, which is:
"C:\Documents and Settings\My Account\My Documents\Temp\Doc3.docx"

On my system (Office 2010 on Windows XP), the "My Account" portion of that path doesn't exist; it's replaced by the string identifying my Windows profile. Perhaps that's changed in Windows 2007, or whatever you're using, so your error may not be the same as mine. In any event, when I change that Constant declaration to use an existing path:

"C:\temp2\Doc3.docx"

...then it works as designed.

So, does the path in the Constant Path3 actually exist on your machine?

Author

Commented:
The "MyAccount" is my Windows Profile, I used that format just to identify that's what it is.   And that's where the problem was.  To make it generic for post, I changed it to My Account without changing it back after the Copy/Paste.

I got Doc1, Doc2, and Doc 3 to Save in the same location.  It will also overwrite Doc1, Doc2, and Doc 3 if they are present.

Just an overlooked error, which I would not have found if you hadn't pointed it out.  

I guess another question would be:  How to open up both docs, since we know what the path is from the Picker, run the compare, and then save "Doc3" back to the same folder with some identifier that this is the result of a Doc Comparison.

That was my initial attempt, but then couldn't get the hard coded version to work.  For instance, if I still wanted to use Doc1 then the code would be:
strPath1 = .SelectedItems(1)

Open in new window

or
strPath1 = Split(strPath1, "\")(UBound(Split(strPath1, "\")) - 1)

Open in new window


and that woud give me the entire path with the filename.  How do I write the path name so that it appears without the document name?

Then the Const would be: "The extracted file path."

Thanks

Author

Commented:
Actually, if I remove the Const statements and remove the Doc1 SaveAs and Doc2 SaveAs statements, opened the two documents, have them compared, it seems to work on Screen.  It is the saving the Doc3 where I'm now having the issue. When I use:

Doc3.SaveAs2 Path1 & "- Compared", wdFormatDocumentDefault

Open in new window

it saves it as:  
Data Modeling Standards and Guidelines-Master.docx-Compared 

Open in new window


I tried to remove the extension .docx with a Left method, but it doesn't seem to work.

Current Code:
Sub CompareDocuments()     
Dim strFile1, strFile2, Path1 As String
Dim strPath1, strPath2, strPath3
Dim Doc1 As Document
Dim Doc2 As Document
Dim Doc3 As Document

      With Application.FileDialog(msoFileDialogFilePicker)
      .Title = "Select Original Document For Comparison"
      .Show
      'Provides the path and filename
   strPath1 = .SelectedItems(1)
   Path1 = .SelectedItems(1)
   Debug.Print strPath1
   Debug.Print Path1
  Documents.Open FileName:=strPath1, ReadOnly:=False, Visible:=True
     Set strPath1 = ActiveDocument     'Filename Only
     Debug.Print strPath1
     Set Doc1 = ActiveDocument
      Debug.Print Doc1    'Filename only
End With

     With Application.FileDialog(msoFileDialogFilePicker)
      .Title = "Select Revised File For Comparison"
      .Show
      'Provides the path and filename
   strPath2 = .SelectedItems(1)
  Documents.Open FileName:=strPath2, ReadOnly:=False, Visible:=True
             'Provides just the Document Name
     Set strPath2 = ActiveDocument
     Set Doc2 = ActiveDocument
End With

 Set Doc3 = Application.CompareDocuments(Doc1, Doc2, _
     Destination:=wdCompareDestinationNew, _
     Granularity:=wdGranularityWordLevel, _
     CompareFormatting:=True, _
     CompareCaseChanges:=True, _
     CompareWhiteSpace:=True)
==>     Path1 = Left(Path1, Len(Path1) - 4)    'I receive an error that it cannot find Left
 'Need help with this
==>    Doc3.SaveAs2 [b]"Path1" & "- Compared" & ".docx", [/b]wdFormatDocumentDefault
 Doc1.Close
 Doc2.Close
End Sub

Open in new window


It appears my biggest issue is why I get an Error on the  Left(strPath1.....
I have the object models loaded:
Visual Basic for Applications
Microsoft Word 14.0 Object Library
OLE Automation
Microsoft Office 14.0 Object Library


So I'm at a loss as to the issue.
Acronis in Gartner 2019 MQ for datacenter backup

It is an honor to be featured in Gartner 2019 Magic Quadrant for Datacenter Backup and Recovery Solutions. Gartner’s MQ sets a high standard and earning a place on their grid is a great affirmation that Acronis is delivering on our mission to protect all data, apps, and systems.

Commented:
I have found a work around to the String Issue:

Path2 = Replace(Path2, ".docx", "")
Doc3.SaveAs2 Path2 & "- Compared.docx", wdFormatDocumentDefault

Open in new window

But, do you know the String Commands are Not Working?
Sorry, I've been out of the office (ill) for a couple days, and I confess I haven't looked closely at all your updates. It seems you're wondering, though, how to extract just the path and just the filename from the full path+filename that we get from the FilePicker. Here's some code I use for that:

    ' sSelectedItem is a string that's set to the selection fromthe FilePicker, e.g.:
    sSelectedItem = .SelectedItems(1)
    ' Extract the filename from sSelectedItem
    Dim sSelectedItem_Filename as String, sSelectedItem_Path as String
    Dim iPosLastSlash As Integer
    ' e.g., sSelectedItem = "c:\temp\TestFile.docx"
    iPosLastSlash = InStrRev(sSelectedItem, "\") ' returns position of last slash in path, counting from left
    ' In our example, iPosLastSlash = InStrRev("c:\temp\TestFile.docx", "\") = 8
    sSelectedItem_Path = Mid(sSelectedItem, 1, iPosLastSlash)
    ' e.g., sSelectedItem_Path = "c:\temp\"
    sSelectedItem_Filename = Mid(sSelectedItem, iPosLastSlash + 1)
    ' e.g., sSelectedItem_Filename = "TestFile.docx"

Author

Commented:
Sorry you were ill, and thanks for the explaination for the file picker.  I may have been rambling due to finding out certain answers after I had made a post.

The only question I have left is why can't I use the String commands within VBA on my machine, which is setup like yours?

I keep getting errors that it's not part of the Project or Library.  In the past it seems that I could use Left, Right, Mid, etc.  Now, I can't.  Would you know why?  Yes, I looked at the References and all the ones that should be there are.  Unless there is an obscure one that I've missed.

Maybe, just speculation, is that since my box has full file encryption, maybe those specific commands won't work....don't think that's the case, just searching for some reason.
That Left() thing is peculiar. I also have the Microsoft Forms 2.0 Object Library loaded, but I don't think that's the problem.

The Function Left() is part of the VBA library, so if you have that loaded it should work. One possibility might be that you have a disambiguation issue, in which Left() might refer also to another routine. Is it possibile that you have a macro called Left in your project, or your Normal.dotm template?

Author

Commented:
Thanks for pointing me in the right direction.  One learns by doing and the catching of certain errors, helped to focus on the correct method.  In addition, the code for name extraction was helpful.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial