我對這個詞宏的東西完全陌生
我在互聯網上找到了一個宏,它將 Word 檔案中的選擇保存為新檔案
Sub SaveSelectedTextToNewDocument()
If Selection.Words.Count > 0 Then
'Copy the selected text
Selection.Copy
'Open a new document and paste the copied text into it
Dim objNewDoc As Document
Set objNewDoc = Documents.Add
Selection.Paste
'Get the first 10 characters as the filename of the new document and save them
Dim objFileName As Range
Set objFileName = objNewDoc.Range(Start:=0, End:=10)
objNewDoc.SaveAs FileName:="C:\Users\Test\Desktop\" & objFileName & ".docx"
Else
End If
End Sub
現在的問題是我不希望將檔案名保存為檔案的前 10 個字母,但我希望檔案名以遞增的數字代替(例如 1.docx、2.docx、3.docx等等....)
uj5u.com熱心網友回復:
這是一個應該可以作業的宏:
Sub SaveSelectedTextToNewDocumentNumbered()
' Charles Kenyon 16 October 2021
' https://stackoverflow.com/questions/69593130/save-word-files-with-filenames-as-increasing-numbers-using-macro
'
Retry:
If Selection.Words.Count > 0 Then
'Copy the selected text
Selection.Copy
'Open a new document and paste the copied text into it
Dim objNewDoc As Document
Dim currentDoc As Document
Dim sFileName As String
Dim i As Integer
Set currentDoc = ActiveDocument
On Error GoTo CreateVar
i = currentDoc.Variables("SaveNum")
On Error GoTo -1
i = i 1
Let sFileName = currentDoc.Name
Set objNewDoc = Documents.Add
Selection.Paste
' save and assign name
objNewDoc.SaveAs FileName:=sFileName & i
' update variable
currentDoc.Variables("SaveNum") = i
' save original document with new variable
currentDoc.Save
' cleanup
Set currentDoc = Nothing
Set objNewDoc = Nothing
On Error GoTo -1
End If
Exit Sub
CreateVar:
ActiveDocument.Variables("SaveNum") = 0
GoTo Retry
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/324386.html
