Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with Macro in MS Word 2007

Status
Not open for further replies.

PanfTeach

Programmer
Nov 25, 2008
6
US
Here is what I am trying to do in more detail. You have a word document that is named (for example), "key1.doc". Your students hand in 20 papers, that need to be graded via folders that I save on my flash drive. What I do is, I open Word 2007, and click the Compare option at the top. This allows me to compare the "key1.doc" to any other document that I chose. And it makes the corrections, and I put a grade on the paper, with my Tablet.

The problem is the procedure above is just too time consuming, because Word does not allow you to just keep that one document, 'key1.doc' as the main compare file. It makes you go through the process everytime of selecting it, and then you have to also select the file you want to compare. It takes too much time.

The solution that I have in my mind, is to make a macro, that by default sticks in 'key1.doc' into the Compare option, and then pops up a folder asking me to find the next document to compare. This would save a bunch of time.

I then hope to grow this, to possibly allow me to select all 20 documents at once, and compare them 1 by 1 with the macro.

I tried to build the Macro, through Word on my own, however, there is no way for me to stop it, on the open folder, without writing code. and it has been a long time since i did that.

Here is an example of the Macro I have gotten

ActiveDocument.Merge FileName:= _
"F:\Grade\214-7\214-7-3\MLA Reports\packet 1\Report 3.doc", MergeTarget:= _
wdMergeTargetSelected, DetectFormatChanges:=True, UseFormattingFrom:= _
wdFormattingFromPrompt, AddToRecentFiles:=False



Instead of the macro, leading to a specific file, I want it to lead to a folder, where I can chose which file to compare.
 
Hi PanfTeach,

Perhaps something along the lines of:
Code:
Sub MarkFiles()
Dim strFold As String
Dim strFName As Variant
strFold = "F:\Grade\214-7\214-7-3\MLA Reports\packet 1\"
strFName = Dir(strFold & "*.doc", vbNormal)
While strFName <> ""
  ActiveDocument.Merge FileName:=strFold & strFName, MergeTarget:= _
    wdMergeTargetSelected, DetectFormatChanges:=True, UseFormattingFrom:= _
    wdFormattingFromPrompt, AddToRecentFiles:=False
  strFName = Dir()
Wend
End Sub
Note: I've never used the document merge feature, so I don't really know how it works.

Cheers

[MS MVP - Word]
 
Why are you using Merge, don't you want to compare?
How about something like this?

Code:
Private Sub DocumentCompare()

Dim fs, f, f1, fc, s, DocPath, Myfile

DocPath = "F:\Grade\214-7\214-7-3\MLA Reports\packet 1\"
Myfile = DocPath & "\key1.doc"
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(DocPath)
    Set fc = f.Files
    Set s = Documents
    Set Myfile = Documents
    
    For Each f1 In fc
        s = f1.Name
        If s <> "key1.doc" Then
            s.Compare(myfile,CompareTarget:=Word.WdCompareTarget.wdCompareTargetSelected, _
            DetectFormatChanges:=True, AddToRecentFiles:=False)
        End If
    Next
End Sub

Let me know if you try this I haven't got word 2007 so I can't try it out myself :)


Michael Bryant
Southern Ocean Software
 
michaelbr55,

on your suggestion it is erroring out on.

s.Compare(myfile,CompareTarget:=Word.WdCompareTarget.wdCompareTargetSelected, _
DetectFormatChanges:=True, AddToRecentFiles:=False)
 
macropod,

When I tested out your macro, it didnt do anything. I just sort of ran without an error, but nothing popped up.

I feel as if we are on to something though, this would be a huge time saver for me, and I would owe you guys big time, so please dont give up.

thanks
 
You are going to have problems with:
Code:
Myfile = DocPath & "\key1.doc"
.....
.....
    Set Myfile = Documents
Do these not conflict? Further...what is Documents (plural)???

Set s = Documents

is also suspect. Documents in Word is a Collection.

From OP: "The problem is the procedure above is just too time consuming, because Word does not allow you to just keep that one document, 'key1.doc' as the main compare file. "

No, you can easily keep the one document, by making it a document object. If key1.doc is the source you wish to compare to, then:

1. open key1.doc
2. make it an explicit object
3. now use a version of Michael's code

Note 1: you DO have to a Reference to Microsoft Scripting Run-time.

Note 2: key1.doc is OPEN. You could of course add opening it to the code.
Code:
[COLOR=red]'  I prefer explicit declarations...[/color red]
Sub DocumentCompare()
Dim keyDoc As Document ' singular, NOT plural
Dim fso As Scripting.FileSystemObject
Dim fld As Scripting.Folder
Dim fil As Scripting.File
Dim DocPath As String

' just in case key1.doc is not the active doc
' grab it from the Documents collection
' if it is the active doc, you could use
' Set keyDoc = ActiveDocument

Set keyDoc = Documents("key1.doc")

DocPath = "F:\Grade\214-7\214-7-3\MLA Reports\packet 1\"

Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fs.GetFolder(DocPath)

' as I do not think you can Compare unopened files
' you need to open each one
    For Each fil In fld
        Documents.Add Filename:=DocPath & fil.Name
        ' it is now the ActiveDocument
        keyDoc _    [COLOR=red]' your key1.doc[/color red]
           .Compare(ActiveDocument, _
CompareTarget:=Word.WdCompareTarget.wdCompareTargetSelected, _
            DetectFormatChanges:=True, AddToRecentFiles:=False)
        ' close the current file
        ' save changes??
       ActiveDocument.Close wdSaveChanges
   Next
End Sub
The point being is that if you make your key1.doc an Document object, you can use it repeatedly.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top