Avatar of Cook09
Cook09
Flag for United States of America asked on

How to Save a Document from File Picker for File Comparison

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??
Microsoft WordMicrosoft OfficeMicrosoft Applications

Avatar of undefined
Last Comment
Cook09

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
PandaPants

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Cook09

ASKER
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
Cook09

ASKER
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.
SOLUTION
Cook09

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Cook09

ASKER
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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
PandaPants

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?
Cook09

ASKER
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.