將下面的代碼輸入到需要提取標題的Word檔案的VBA視窗,當打開這個Word檔案時就會運行以下代碼,提取檔案編號與標題,并存入同一目錄下的bbb.docx檔案中。(注意:檔案較大時,運行時間較長,請耐心等待)
Private Sub Document_Open()
Dim t1, t2, t3 As String
Dim pcount, i As Integer
Dim wdapp As Object
Set wdapp = CreateObject("word.application") '創建一個新的Word檔案,用于存盤編號和標題
wdapp.Documents.Add
wdapp.Visible = True
pcount = ThisDocument.Paragraphs.Count 'pcount為檔案段落總數
For i = 1 To pcount
t1 = ThisDocument.Paragraphs(i).Style
If Left(t1, 2) = "標題" Then
t2 = ThisDocument.Paragraphs(i).Range 't2為標題
t3 = ThisDocument.Paragraphs(i).Range.ListFormat.ListString 't3為編號
wdapp.Documents(1).Content.InsertAfter (t3 + t2)
End If
Next i
wdapp.Documents(1).SaveAs ("bbb.docx") '另存為同一目錄下的bbb.docx
Set wdapp = Nothing
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/267680.html
上一篇:關于awk命令【討論】
下一篇:Windows vc+++
