P
P
pipetto2021-11-21 17:11:13
Macros
pipetto, 2021-11-21 17:11:13

Why does the macro only work for those files that are opened manually, and not when selected when the dialog pops up?

The environment swears at each For Each p In ActiveDocument

Sub BatchProcessing()
    Dim w As Word.Words
    Dim sec As Word.Section
    Dim r As Word.Range
    Dim p As Word.Paragraph
    Dim sen As Word.Sentences
    Dim countP, n As Integer
    
    With Application.FileDialog(msoFileDialogFilePicker)
    .AllowMultiSelect = True
    .Title = "Âûáåðåòå ôàéëû äëÿ îáðàáîòêè"
    .Filters.Clear
    .Filters.Add "Word", "*.docm; *.docx; *.doc"
    .InitialView = msoFileDialogViewDetails
    .Show
    
    For i = 1 To .SelectedItems.Count
    
    For Each p In ActiveDocument.Sections(1).Range.Paragraphs
        p.Range.Words(1).Bold = True
    Next p
    
    For Each p In ActiveDocument.Sections(2).Range.Paragraphs
        If p.Range.Sentences.Count > 1 Then
            p.Range.Sentences(1).Bold = True
            p.Range.Sentences(2).Bold = True
        Else
            p.Range.Sentences(1).Bold = True
        End If
    Next p
    
    Set doc = Documents.Open(.SelectedItems(i), Visible:=False)
    doc.Save
    doc.Close (True)
    
    Next i
    
    End With
    
End Sub

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BasiC2k, 2021-11-21
@pipetto

In your code, the macro processes only the active document (ActiveDocument)
After selecting the file (s) you need to get a link to a specific file (doc) and work with it.
Those. after the file selection procedure, it is necessary, after the line:
For i = 1 To .SelectedItems.Count
add:
Set doc = Documents.Open(.SelectedItems(i), Visible:=False) ' Open the file
and everywhere below the code replace ActiveDocument with doc
Opening the file before
doc.Save
is no longer required.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question