Vba 在打开的文件中添加书签并将其另存为dcox

Vba 在打开的文件中添加书签并将其另存为dcox,vba,ms-word,Vba,Ms Word,我有一个关于书签的问题。我制作了一个宏,删除Word文档中的所有书签并添加一个新书签: Sub AddBookmarkInCurrentFile() ' ' Deletes all the bookmarks in an already opened file ' and add one new bookmark in the file ' ' Deletes al current bookmarks For Each bkm In ActiveDocument.Bookmark

我有一个关于书签的问题。我制作了一个宏,删除Word文档中的所有书签并添加一个新书签:

Sub AddBookmarkInCurrentFile()
'
' Deletes all the bookmarks in an already opened file
' and add one new bookmark in the file
'
   ' Deletes al current bookmarks
     For Each bkm In ActiveDocument.Bookmarks
        bkm.Delete
     Next bkm

    ' Put Cursor add the beginning of the file and adds the bookmark
    Selection.HomeKey Unit:=wdStory
    ActiveDocument.Bookmarks.Add Name:="testBookmarkAdd"

    MsgBox "Finished"
End Sub
当我运行这个程序时,它工作正常

因为我必须对100多个文档执行此操作,并将*.doc另存为.docx,所以我制作了宏的新版本。一切都可以接受添加新书签。下面的代码有什么问题

Sub AddBookmarkInAllOpenedFiles()

' Opens all word files in a directory and deletes current bookmarks
' and adds one bookmark and saves the file to a docx file

    Dim sSourcePath As String
    Dim sTargetPath As String
    Dim sDocName As String
    Dim docCurDoc As Document
    Dim sNewDocName As String

    Dim sOrigName As String
    Dim intPos As Integer

    ' Looking in this path
    sSourcePath = "H:\Mijn Documenten\test\"
    sTargetPath = "H:\Mijn Documenten\test\Converted\"

   ' Look for first DOC file
    sDocName = Dir(sSourcePath & "*.doc")
    Do While sDocName <> ""
        ' Repeat as long as there are source files
         'Only work on files where right-most characters are ".doc"
        If Right(sDocName, 4) = ".doc" Then
            ' Open file
            Set docCurDoc = Documents.Open(FileName:=sSourcePath & sDocName)


            ' Deletes all the bookmarks
            For Each bkm In ActiveDocument.Bookmarks
                bkm.Delete
            Next bkm

            ' Put Cursor add the beginning of the file and adds the bookmark
            Selection.HomeKey Unit:=wdStory
            ActiveDocument.Bookmarks.Add Name:="testBookmarkAdd"

            'Saves the document as a docx
            sNewDocName = Replace(sDocName, ".doc", ".docx")

            With docCurDoc
                .SaveAs FileName:=sTargetPath & sNewDocName, _
                FileFormat:=wdFormatDocumentDefault
                .Close SaveChanges:=wdDoNotSaveChanges
            End With
        End If
        ' Get next source file name
        sDocName = Dir
    Loop
    MsgBox "Finished"
End Sub
Sub-addbookmarkinallopeedfiles()
'打开目录中的所有word文件并删除当前书签
'并添加一个书签并将文件保存到docx文件
将sSourcePath设置为字符串
Dim sTargetPath作为字符串
Dim sDocName作为字符串
Dim docCurDoc作为文档
Dim sNewDocName作为字符串
Dim sOrigName作为字符串
作为整数的Dim intPos
“看看这条路
sSourcePath=“H:\Mijn Documenten\test\”
sTargetPath=“H:\Mijn Documenten\test\Converted\”
'查找第一个文档文件
sDocName=Dir(sSourcePath和“*.doc”)
在sDocName“”时执行此操作
'只要有源文件,就重复
'仅适用于最右边字符为“.doc”的文件'
如果正确(sDocName,4)=“.doc”,则
'打开文件
设置docCurDoc=Documents.Open(文件名:=sSourcePath&sDocName)
'删除所有书签
对于ActiveDocument.Bookmarks中的每个bkm
删除
下一个bkm
'放置光标添加文件的开头并添加书签
Selection.HomeKey单位:=wdStory
ActiveDocument.Bookmarks.Add名称:=“testBookmarkAdd”
'将文档另存为docx
sNewDocName=Replace(sDocName、.doc、.docx)
用docCurDoc
.SaveAs文件名:=sTargetPath&sNewDocName_
FileFormat:=wdFormatDocumentDefault
.Close SaveChanges:=wdDoNotSaveChanges
以
如果结束
'获取下一个源文件名
sDocName=Dir
环
MsgBox“已完成”
端接头
试试:

子书签AllFileInfolder()
'打开目录中的所有word文件并删除当前书签
'并添加一个书签并将文件保存到docx文件
Dim sSourcePath为字符串,sTargetPath为字符串
Dim sDocName作为字符串,docCurDoc作为文档
“看看这条路
sSourcePath=“H:\Mijn Documenten\test\”
sTargetPath=sSourcePath&“已转换”
'查找第一个文档文件
sDocName=Dir(sSourcePath和“*.doc”)
'只要有源文件,就重复
在sDocName“”时执行此操作
'仅打开.doc文件'
如果正确(sDocName,4)=“.doc”,则
'打开文件
设置docCurDoc=Documents.Open(文件名:=sSourcePath&sDocName,AddToRecentFiles:=False,可见:=False)
用docCurDoc
'删除所有现有书签
While.Bookmarks.Count>0
.书签(1).删除
温德
'添加我们的书签
.Bookmarks.Add Name:=“TestBookmark”,范围:=.Range(0,0)
'将文件以.docx格式保存到输出文件夹
.SaveAs2文件名:=sTargetPath&sDocName&“x”_
FileFormat:=wdFormatDocumentDefault,AddToRecentFiles:=False
.Close SaveChanges:=wdDoNotSaveChanges
以
如果结束
'获取下一个源文件名
sDocName=Dir
环
设置docCurDoc=Nothing
MsgBox“已完成”
端接头

避免使用
ActiveDocument
。此方法指的是在代码运行时位于顶部的文档。这不是非常可靠。请使用
ActiveDocument
而不是
docCurDoc
来准确地指代您使用
docCurDoc=Documents打开的文档。打开
。而代码仅回答可能无效em帮助OP他们是不好的做法。你应该解释你做了什么/改变了什么以及为什么,这样OP和其他读者可以从中学习。否则在我看来这是毫无用处的,你只是做了OPs工作(这不是堆栈溢出的目的)只有在代码之外添加一些有用的解释性内容,这才是一个很好的答案。是的,但他(或未来的读者)怎么能如果你不解释做了哪些更改,请理解/学习为什么?这就是重点。所以我理解你所做的,并尝试了它。奇怪的是,在一些文档中它工作,而在其他word文档中它不工作。我使用的模板不工作,但当我创建一个新word文档并使用代码时,它确实工作。不幸的是我无法共享模板文档,因为这是公司的政策。我使用的模板有表单字段、页眉和页脚。这就是我不能工作的原因吗?好吧,它在应用了任何保护的文档中都不能工作(例如,表单字段的“填写表单”)除非您首先删除该保护。如果该保护可能包含或不包含您可能知道或不知道的密码,这将是一个问题。如果您事先将其作为一个问题提到,而不是将其保留到现在,则会有所帮助。
Sub BookmarkAllFilesInFolder()
' Opens all word files in a directory and deletes current bookmarks
' and adds one bookmark and saves the file to a docx file
Dim sSourcePath As String, sTargetPath As String
Dim sDocName As String, docCurDoc As Document

' Looking in this path
sSourcePath = "H:\Mijn Documenten\test\"
sTargetPath = sSourcePath & "Converted\"

' Look for first DOC file
sDocName = Dir(sSourcePath & "*.doc")
' Repeat as long as there are source files
Do While sDocName <> ""
' Only open .doc files"
  If Right(sDocName, 4) = ".doc" Then
  ' Open file
  Set docCurDoc = Documents.Open(FileName:=sSourcePath & sDocName, AddToRecentFiles:=False, Visible:=False)
  With docCurDoc
    'Delete all existing bookmarks
    While .Bookmarks.Count > 0
      .Bookmarks(1).Delete
    Wend
    'Add our bookmark
    .Bookmarks.Add Name:="TestBookmark", Range:=.Range(0, 0)
    'Save the file in .docx format to the output folder
    .SaveAs2 FileName:=sTargetPath & sDocName & "x", _
      FileFormat:=wdFormatDocumentDefault, AddToRecentFiles:=False
      .Close SaveChanges:=wdDoNotSaveChanges
    End With
  End If
  ' Get next source file name
  sDocName = Dir
Loop
Set docCurDoc = Nothing
MsgBox "Finished"
End Sub