• Running a macro on multiple docs at the same time (Word 97 SR2)

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Running a macro on multiple docs at the same time (Word 97 SR2)

    Author
    Topic
    #361551

    We have about 1500 documents that we are adding a watermark to. We have recorded a macro to add the watermark to the document. We open the document, run the macro, save, and close the document. This is getting very boring. Is there a way to run the macro on all documents or multiple documents at the same time?

    Viewing 0 reply threads
    Author
    Replies
    • #546933

      Here’s a stripped-down means to do this:

      Public Sub InsertWatermarksInMultipleDocs()
      Dim sPath As String
      Dim i As Long
      
      sPath = InputBox("Type Path for Folder to be Searched")
      
      With Application.FileSearch
          .NewSearch
          .LookIn = sPath
          .FileType = msoFileTypeWordDocuments
          If .Execute > 0 Then
              For i = 1 To .FoundFiles.Count
                  Documents.Open (.FoundFiles(i))
                      'Your code to insert watermarks here
                      '#################################
                  ActiveDocument.Close (wdSaveChanges)
              Next 'i
          Else:
              MsgBox prompt:="Folder not found. Check path and try again."
              Exit Sub
          End If
      End With
      End Sub
      

      Gary

      • #547046

        I tried the macro after inserting my watermark macro, but I get a compile error. What am I doing wrong? Attached is your macro with my macro inserted.

        • #547054

          I’m no expert, but it looks to me as if there is a space before the first parenthesis (after “.Open”) in the line
          Documents.Open (.FoundFiles(i))
          Would that cause a compile error?

          Rick Ellrod

        • #547056

          You have a Sub within a Sub. Remove the “Sub WaterMark ()” line and the first “End Sub” line (the one right before Activedocument.Close (wdSaveChanges)).

          HTH,

          Chris

          • #547066

            Thanks for the help everyone! It works like a charm!

            Can the macro be modified to not add the watermark if it is already there?

            • #547182

              The following is a very quick and dirty amendment to your code so that it will only run if there is no graphic object (Shape) in the header/footer:

                              If ActiveWindow.View.SplitSpecial  wdPaneNone Then
                              ActiveWindow.Panes(2).Close
                              End If
                              If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
                              ActivePane.View.Type = wdOutlineView Or ActiveWindow.ActivePane.View.Type _
                                = wdMasterView Then
                                  ActiveWindow.ActivePane.View.Type = wdPageView
                              End If
                              ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
                              If Selection.HeaderFooter.IsHeader = True Then
                                  ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
                              Else
                                  ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
                              End If
                              Dim rngTemp As Range
                              Selection.WholeStory
                              Set rngTemp = Selection.Range
                              On Error Resume Next
                              If rngTemp.ShapeRange.Count = 0 Then
                                  Selection.HeaderFooter.Shapes.AddTextbox(msoTextOrientationHorizontal, _
                                      72#, 676.8, 468#, 115.2).Select
                                  Selection.ShapeRange.TextFrame.TextRange.Select
                                  Selection.Collapse
                                  Selection.ShapeRange.Fill.Visible = msoFalse
                                  Selection.ShapeRange.Line.Visible = msoFalse
                                  Selection.HeaderFooter.Shapes.AddTextEffect(msoTextEffect2, "History", _
                                  "Times New Roman", 96#, msoFalse, msoFalse, 161.7, 401.15).Select
                                  Selection.ShapeRange.Fill.ForeColor.RGB = RGB(192, 192, 192)
                                  Selection.ShapeRange.Fill.Visible = msoTrue
                                  Selection.ShapeRange.Fill.Solid
                                  Selection.ShapeRange.Select
                                  Selection.ShapeRange.IncrementLeft 11.1
                                  Selection.ShapeRange.IncrementTop -98.75
                              End If
                              Set rngTemp = Nothing
                              ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
              

              Gary

            • #547228

              hi, what are the changes in code for the macro(s) to run in 2000?

            • #547273

              Hi Kathleen,

              It looks like this code should run in 2000 as well without revision.

              Gary

            • #547276

              OK, i’m on a network, i typed in the entire path and the macro had a good time looking for documents. it listed a bunch as it went thru them, but none were known to me nor the ones in the path i gave it.

              long prelude, question-is it because i’m on a network and there are like several shared directories?

            • #547372

              Hi Kathleen,

              It’s really hard to tell what’s going on in that case.
              When you say it listed the docs as it went through them, what do you mean? – do you mean the name of the document displays on the status bar, or what exactly?

              I’m not much of a network whiz so am not sure what effect shared directories would have.
              Try the following: try having the macro search for files in a directory that’s on your local drive.
              If it works OK to find files on your local drive, but not on a networked drive, then that would point to a network issue.

              Hope that helps,
              Gary

            • #547433

              OK, cool. the status bar says ‘opening’ then lists the files its going thru; on the network directory it listed main.htm and panasonic.html. no clue what they are. i tried it on the c drive and (after deleting one line of duplicate rngTemp) it whipped thru the files stamping history and a box! really cool! i’ll fiddle to see what else i can do in place of the graphic! since most docs are in the network, i’d really like ideas for using on that!

              thanks for the help!

            • #547436

              I tried the macro on the network too. It still put a graphic on the documents that already contained the watermark. I am not sure why it puts the gray box on the docs. I am going to redo the watermark part, since I may have made a mistake when I recorded it. Why would it still put the watermark on docs that already contain it? I will be using this on the network though, if that makes a difference.

            • #547628

              I can’t get the ShapeRange property of the Selection object to work either.

              The following property gives an accurate count, but you need to find a way to know where you are (header or footer and which one):

              Selection.Range.Sections(1).Headers(wdHeaderFooterFirstPage).Shapes.Count

              Too late tonight to figure it out.

            • #547240

              Thanks Gary. I will give it a try.

            • #548329

              One other thing. When I run the macro, it opens the file and places the watermark on the document, but it doesn’t close the file before going on to the next file. Am I missing something?

            • #548507

              Hi again,

              The statement towards the beginning of the procedure:

              Documents.Open(.FoundFiles(i))

              opens the document, and the statement towards the end:

              ActiveDocument.Close (wdSaveChanges)

              should be serving to close it.
              That’s usually a reliable enough way to specify the correct document you want the code to be working on, but if it’s not working, a couple of other means can be used:

              Documents.Close(.FoundFiles(i))

              should work more reliably since it refers to the document by index (caveat that I haven’t tested it with your procedure).

              An even more reliable method is to create an object variable and set it to equal the document as it opens:

              Dim objDoc As Document
              Set objDoc = Documents.Open (.FoundFiles(i))

              and then later one you use:
              objDoc.Close (wdSaveChanges)
              Set objDoc = Nothing

              With regard to the other post about the watermark getting inserted in documents that already have it – the code I suggested relied on getting a count of existing shapes in the header or footer.
              This isn’t really reliable – as Jeff pointed out in his note – as it’s hard to get a count of shapes, particularly if the code isn’t specific as to whether you’re in the header or footer.
              So the code would need to be more specific that way.

              Probably a more reliable way to test for the existence of a watermark in the document, would be to have the insert watermark code finish by adding either a bookmark or custom document property to the document (for example adding a boolean (Yes or No) “HasWatermark” custom document property and setting its value to Yes.

              Then, the insert watermark procedure would first test for the existence of this custom document property, and whether its property was set to Yes.

              There’s been some code posted here recently for adding custom doc props as well as testing whether one already exists – a search on this forum (or possibly the VBA forum) should turn up more info.

              Gary

      • #549780

        Hi Gary

        Just like to say a big thank you for putting up the batch operation macro. I’ve never understood public macros and furthermore I have a huge number of docs where the paper source box has to be changed to Auto. I have just inserted my own box-change macro and whizzed through nearly half of them.

        Many thanks.

        David Solomon

    Viewing 0 reply threads
    Reply To: Running a macro on multiple docs at the same time (Word 97 SR2)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: