View Full Version : Macro to browse for document
neodjandre
07-10-2008, 09:17 AM
Hello,
I am using this code to browse for a document and compare it with the active document but it does not work as my Word VBA skills are very limited. Could someone please help with this?
Sub vba_transferc()
Dim bookstring As String
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(FileDialogType:=msoFileDialogOpen)
With dlgOpen
.AllowMultiSelect = False
.Show
End With
ActiveDocument.Compare Name:=ActiveDocument.Name _
, AuthorName:="", CompareTarget:=wdMergeTargetNew, DetectFormatChanges:= _
False, IgnoreAllComparisonWarnings:=True, AddToRecentFiles:=False, _
RemovePersonalInformation:=True, RemoveDateAndTime:=True
With ActiveWindow.View
.ShowRevisionsAndComments = False
.RevisionsView = wdRevisionsViewFinal
End With
End Sub
thanks
Andy
GregQik
07-12-2008, 01:37 AM
Have you researched the Word VBA helpfile?
There is a simple but serious logic error in your code.
Example
This example compares the active document with the document named "FirstRev.doc" in the Draft folder and places the comparison differences in a new document.
Sub CompareDocument()
ActiveDocument.Compare Name:="C:\Draft\FirstRev.doc", _
CompareTarget:=wdCompareTargetNew
End Sub
neodjandre
07-14-2008, 02:26 AM
Ok, I have changed my code to:
Dim bookstring As String
Dim wbk1 As Document
Dim wbk2 As Document
Dim dlgOpen As FileDialog
MsgBox "Please ensure that the new version of your document is activated. Please browse for the old version of your document.", vbInformation
Set wbk1 = ActiveDocument
Set dlgOpen = Application.FileDialog(FileDialogType:=msoFileDialogOpen)
With dlgOpen
.AllowMultiSelect = False
.Show
End With
Set wbk2 = ActiveDocument
wbk1.Compare Name:=wbk2.Path & Application.PathSeparator & _
wbk2.Name _
, AuthorName:="", CompareTarget:=wdMergeTargetNew, DetectFormatChanges:= _
False, IgnoreAllComparisonWarnings:=True, AddToRecentFiles:=False, _
RemovePersonalInformation:=True, RemoveDateAndTime:=True
With ActiveWindow.View
.ShowRevisionsAndComments = False
.RevisionsView = wdRevisionsViewFinal
End With
This time I don't get any errors but the documents are not compared .. i.e. nothing happens..
any ideas why? :-)
GregQik
07-15-2008, 04:39 AM
The original bug still remains.
The document.compare works by comparing a loaded document with
a disk file. Your code simply loads a second document.
The use of the FileOpen dialog is on the right track but a standard word dialog works better.
You should use the .display method instead of the .show method.
Once the user has selected the correct file, extract the filename
and path from the dialog properties. Use that string to set
wk2 and set the dialog to nothing. Now you may compare an active
document to a file on disk. You should dom some error checking
ensure the user has not cancelled the file open dialog.
Dialog Properties below:
VBA Help 'Built-in Dialog Box Argument Lists'
wdDialogFileOpen:
WritePasswordDot, Connection, SQLStatement, SQLStatement1, Format, Encoding, Visible, OpenExclusive, OpenAndRepair, SubType, DocumentDirection, NoEncodingDialog, XMLTransform
My quick and dirty version
Dim bookstring As String
Dim wbk1 As Document
Dim wbk2 As String
Dim dlgShow As Dialog
MsgBox "Please ensure that the new version of your document is activated. Please browse for the old version of your document.", vbInformation
Set wbk1 = ActiveDocument
'Set dlgOpen = Application.FileDialog(FileDialogType:=wdDialogFileOpen)
Set dlgShow = Dialogs(wdDialogFileOpen)
With dlgShow
'.AllowMultiSelect = False
.Display
wbk2 = .Name
End With
'MsgBox wbk2
wbk1.Compare Name:=wbk2 _
, AuthorName:="", CompareTarget:=wdMergeTargetNew, DetectFormatChanges:= _
False, IgnoreAllComparisonWarnings:=True, AddToRecentFiles:=False, _
RemovePersonalInformation:=True, RemoveDateAndTime:=True
Cheers
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.