当前位置:首页教育技巧word技巧word图表制作

word重命名如何换行

2024-07-12 09:57:59


1.如何将word批量命名word文件名自动改成文档的首行的内容?

Sub ChangeDocName(sPath As String)

Dim wdApp

Dim oDoc

Dim sFileName As String

Dim sNewName As String

Dim i As Integer

On Error Resume Next

Set wdApp = CreateObject("Word.Application")

On Error GoTo 0

If wdApp Is Nothing Then

MsgBox "创建word实例失败,程序将退出。本程序只能在装有word程序的电脑上运行。"

Exit Sub

End If

If Right(sPath, 1) "\" Then sPath = sPath & "\"

sFileName = Dir(sPath & "*.doc", vbHidden + vbSystem)

Do While sFileName ""

i = 0

Set oDoc = wdApp.Documents.Open(sapth & sFileName)

oDoc.Select

wdApp.Selection.HomeKey Unit:=wdStory

wdApp.Selection.Expand wdLine

sNewName = wdApp.Selection.Text

sNewName = Replace(sNewName, "\", "")

sNewName = Replace(sNewName, "/", "")

sNewName = Replace(sNewName, ":", "")

sNewName = Replace(sNewName, "?", "")

sNewName = Replace(sNewName, "*", "")

sNewName = Replace(sNewName, ">", "")

sNewName = Replace(sNewName, "sNewName = Replace(sNewName, """", "")

oDoc.Close

Err.Clear

On Error GoTo FileAlreadyExist

Name sPath & sFileName As sPath & sNewName & ".doc"

FileAlreadyExist:

If Err.Number >0 Then

i = i + 1

Name sPath & sFileName As sPath & sNewName & i & ".doc"

End If

On Error GoTo 0

sFileName = Dir()

Loop

Set wdApp = Nothing

End Sub

上述代码中已经加入了常见错误的处理,使用的时候将文件夹的完整路径作为参数输入就可以了。

相关信息


电脑版

【免责声明】本站信息来自网友投稿及网络整理,内容仅供参考,如果有错误请反馈给我们及时更正,对文中内容的真实性和完整性本站不提供任何保证,不承但任何责任。
版权所有:学窍知识网 Copyright © 2011-2024 www.at317.com All Rights Reserved .