首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将文档重命名为文档中的特定文本

将文档重命名为文档中的特定文本
EN

Stack Overflow用户
提问于 2017-07-10 15:11:10
回答 1查看 454关注 0票数 0

因此,我有800多份文件需要重新命名。它们都包含一个名为“title:”的特定部分。问题是,在每个文档中,这都会出现在不同的地方。这基本上是文本之后,我想要命名它。

代码语言:javascript
复制
Sub Macro1()
Dim strFolder As String
Dim strDoc As String
Dim wordApp As Word.Application
Dim wordDoc As Word.document

Set wordApp = New Word.Application
wordApp.Visible = True

Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
.Title = "D:\Test\"
If .Show = -1 Then
    strFolder = .SelectedItems(1) & "\"
Else
    MsgBox "You did not select the folder that contains the documents."
    Exit Sub
End If
End With

MkDir strFolder & "Processed"

strDoc = Dir$(strFolder & "*.docx")
While strDoc <> ""
Set wordDoc = Word.Documents.Open(strFolder & strDoc)
With wordDoc
    .Content.Select
    With wordApp.Selection.Find
        .Text = "Your establishment name [0-9]{4}"
        .MatchWildcards = True
        .wrap = wdFindStop
        .Execute
    End With
    .SaveAs strFolder & "Processed\" & Right(wordApp.Selection, 4) & ".docx"
    .Close
End With
strDoc = Dir$()
Wend

wordApp.Quit
Set wordApp = Nothing
End Sub

我发现了一些由Silkroad完成的代码,这看起来和我希望它做的完全一样,但是它是错误的。

这基本上是wordApp.Selection.Find行中的错误:

运行时错误91:对象变量或块变量未设置

请帮我解决这个问题。

EN

回答 1

Stack Overflow用户

发布于 2017-07-10 16:07:04

使用以下代码

代码语言:javascript
复制
With wordDoc
    .Content.Select
   'With wordApp.Selection.Find (Remove this line)
      ' Add the  wordApp.Selection.Find as below
    wordApp.Selection.Find.Text = "Your establishment name [0-9]{4}"
    wordApp.Selection.Find.MatchWildcards = True
    wordApp.Selection.Find.wrap = wdFindStop
    wordApp.Selection.Find.Execute
   End With  (Remove this line)
.SaveAs strFolder & "Processed\" & Right(wordApp.Selection, 4) & ".docx"
.Close
End With

with块在With块中不像预期的那样工作,因为它总是使用第一个With块,然后使用第二个with块。去掉其中的一个,它就能正常工作了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45015654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档