2017-10-11 3 views
1

Je suis en train d'écrire une macro rapide, pour sauver mon courrier a fusionné les documents sous forme de documents séparés, puis enregistrez chaque document comme le premier mot de chacun.Comment sélectionner le premier mot d'un document

Voici ce que j'ai jusqu'ici, pour couper le document, et l'enregistrer comme "Test_1" et ainsi de suite, mais j'ai du mal à ajouter le code pour sélectionner le premier mot.

Sub BreakOnSection() 
    'Used to set criteria for moving through the document by section. 
    Application.Browser.Target = wdBrowseSection 

    'A mailmerge document ends with a section break next page. 
    'Subtracting one from the section count stop error message. 
    For i = 1 To ((ActiveDocument.Sections.Count) - 1) 

     'Select and copy the section text to the clipboard 
     ActiveDocument.Bookmarks("\Section").Range.Copy 

     'Create a new document to paste text from clipboard. 
     Documents.Add 
     'To save your document with the original formatting' 
     Selection.PasteAndFormat (wdFormatOriginalFormatting) 

     'Removes the break that is copied at the end of the section, if any. 
     Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend 
     Selection.Delete Unit:=wdCharacter, Count:=1 



     ChangeFileOpenDirectory "H:\Output" 
     DocNum = DocNum + 1 
     ActiveDocument.SaveAs FileName:="test_" & DocNum & ".doc" 
     ActiveDocument.Close 
     'Move the selection to the next section in the document 
     Application.Browser.Next 
    Next i 
    ActiveDocument.Close savechanges:=wdDoNotSaveChanges 
End Sub 

Toute aide serait grandement appréciée.

Répondre

0

Pouvez-vous essayer ce code:

Sub BreakOnSection() 
    'Used to set criteria for moving through the document by section. 
    Application.Browser.Target = wdBrowseSection 

    'A mailmerge document ends with a section break next page. 
    'Subtracting one from the section count stop error message. 
    For i = 1 To ((ActiveDocument.Sections.Count) - 1) 

     'Select and copy the section text to the clipboard 
     ActiveDocument.Bookmarks("\Section").Range.Copy 

     'Create a new document to paste text from clipboard. 
     Documents.Add 
     'To save your document with the original formatting' 
     Selection.PasteAndFormat (wdFormatOriginalFormatting) 

     'Removes the break that is copied at the end of the section, if any. 
     Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend 
     Selection.Delete Unit:=wdCharacter, Count:=1 

     'Newly Added 
     'GoTo Starting of the Document 
     Selection.HomeKey Unit:=wdStory 
     Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=True 
     Dim FileName As String 
     FileName = ReplaceIllegalChar(Trim(Selection.Text)) 
     'End 

     ChangeFileOpenDirectory "H:\Output" 
     DocNum = DocNum + 1 
     ActiveDocument.SaveAs FileName:="test_" & FileName & ".doc" 
     ActiveDocument.Close 
     'Move the selection to the next section in the document 
     Application.Browser.Next 
    Next i 
    ActiveDocument.Close savechanges:=wdDoNotSaveChanges 
End Sub 

Function ReplaceIllegalChar(strIn As String) As String 

Dim j As Integer 
Dim varStr As String, xStr As String 
varStr = strIn 
For j = 1 To Len(varStr) 
    Select Case Asc(Mid(varStr, j, 1)) 
     Case 48 To 57, 65 To 90, 97 To 122 
     xStr = xStr & Mid(varStr, j, 1) 
    Case Else 
     xStr = xStr & "_" 

    End Select 
Next 
ReplaceIllegalChar = xStr 
End Function 
+0

Merci pour la réponse! Je reçois l'erreur - Erreur d'exécution '5096': (test_o.doc) Le o étant le petit cercle noir Debug met en évidence la ligne suivante ActiveDocument.SaveAs FileName: = "test_" & FileName & " .doc » D – LinkToThis

+0

Pouvez-vous partager le document sur lequel vous obtenez l'erreur. Je suppose que vous parlez de la liste à puces. – Arul

+0

Je ne peux pas avoir peur, je suis un enseignant et il contient beaucoup de données sur les étudiants. Il ne montre pas une liste, il montre \t ● au nom. – LinkToThis