通过 VBA 将样式复制到打开的文档时文件被用户锁定

发布于 2025-01-11 01:24:34 字数 2853 浏览 0 评论 0原文

我有一个带有更新样式的活动文档。我里面也有VBA代码。我打开了另一个文档并尝试将样式从活动文档复制到打开的文档。但它给了我一个错误“xxx.docx 文件被 ABC 用户锁定”。我应该怎么做才能阻止此消息。也许我没有正确打开它。

    With wdApp
      .Visible = True
      
        While strFile <> ""
        If strFile = "By MBA.docm" Then GoTo NX
            
            'If IsWorkBookOpen(PathF & strFile) = False Then
                        
            On Error Resume Next
            Set dTarget = .Documents.Open(PathF & strFile)
            If Err.Number <> 0 Then MsgBox strFile & vbCr & vbCr & "Name is not correct." & vbCr & "Processing next file...", vbOKOnly + vbInformation: GoTo NX
        
            'End If

       For Each S In dSource.Styles
       
            If S.Type = wdStyleTypeParagraph Then
                
                On Error Resume Next
' - - -- - > promt line  <- -- - - -- 
                Application.OrganizerCopy Source:=dSource.FullName, _
                  Destination:=dTarget.FullName, Name:=S.NameLocal, _
                  Object:=wdOrganizerObjectStyles
                  
                If Err.Number > 0 Then
                    On Error GoTo 0
                Else
                    J = J + 1
                End If
                
            End If
        Next S
    
        sTemp = "Copied " & J & " styles"
    
    MsgBox sTemp
    
'Header Footer Part
    dTarget.Sections.PageSetup.DifferentFirstPageHeaderFooter = True
    
        For Each H In dSource.Sections.First.Headers
            dTarget.Sections(1).Headers(H.Index).Range.FormattedText = H.Range.FormattedText
            
        Next
        
        For Each H In dSource.Sections(2).Headers
            dTarget.Sections(2).Headers(H.Index).Range.FormattedText = H.Range.FormattedText
            
        Next
            
        For Each F In dSource.Sections.First.Footers
            dTarget.Sections(1).Footers(F.Index).Range.FormattedText = F.Range.FormattedText
            
        Next

        For Each F In dSource.Sections(2).Footers
            dTarget.Sections(2).Footers(F.Index).Range.FormattedText = F.Range.FormattedText
            
        Next
       
            
            With dTarget
                Cpro = Cpro + 1
                Application.StatusBar = Int((Cpro / CountDoc) * 100) & "% Completed --- (" & Cpro & "/" & CountDoc & ") Processing... File Name == " & dTarget
              
                'Close the document
                .Close SaveChanges:=False
                Set FSO = VBA.CreateObject("Scripting.FileSystemObject")
                FSO.CopyFile PathF & strFile, TempFolder & strFile

            End With
            
          'Get the next document
NX:            strFile = Dir()
        Wend
        
        Set dTarget = Nothing
        .Quit
    End With

I have an active document with updated styles. I have VBA code in it as well. I opened another document and try to copy styles from active-document to opened document. but it gives me an error "xxx.docx file is locked by ABC user". what should I do to prevent this message. maybe I am not opening it incorrectly.

    With wdApp
      .Visible = True
      
        While strFile <> ""
        If strFile = "By MBA.docm" Then GoTo NX
            
            'If IsWorkBookOpen(PathF & strFile) = False Then
                        
            On Error Resume Next
            Set dTarget = .Documents.Open(PathF & strFile)
            If Err.Number <> 0 Then MsgBox strFile & vbCr & vbCr & "Name is not correct." & vbCr & "Processing next file...", vbOKOnly + vbInformation: GoTo NX
        
            'End If

       For Each S In dSource.Styles
       
            If S.Type = wdStyleTypeParagraph Then
                
                On Error Resume Next
' - - -- - > promt line  <- -- - - -- 
                Application.OrganizerCopy Source:=dSource.FullName, _
                  Destination:=dTarget.FullName, Name:=S.NameLocal, _
                  Object:=wdOrganizerObjectStyles
                  
                If Err.Number > 0 Then
                    On Error GoTo 0
                Else
                    J = J + 1
                End If
                
            End If
        Next S
    
        sTemp = "Copied " & J & " styles"
    
    MsgBox sTemp
    
'Header Footer Part
    dTarget.Sections.PageSetup.DifferentFirstPageHeaderFooter = True
    
        For Each H In dSource.Sections.First.Headers
            dTarget.Sections(1).Headers(H.Index).Range.FormattedText = H.Range.FormattedText
            
        Next
        
        For Each H In dSource.Sections(2).Headers
            dTarget.Sections(2).Headers(H.Index).Range.FormattedText = H.Range.FormattedText
            
        Next
            
        For Each F In dSource.Sections.First.Footers
            dTarget.Sections(1).Footers(F.Index).Range.FormattedText = F.Range.FormattedText
            
        Next

        For Each F In dSource.Sections(2).Footers
            dTarget.Sections(2).Footers(F.Index).Range.FormattedText = F.Range.FormattedText
            
        Next
       
            
            With dTarget
                Cpro = Cpro + 1
                Application.StatusBar = Int((Cpro / CountDoc) * 100) & "% Completed --- (" & Cpro & "/" & CountDoc & ") Processing... File Name == " & dTarget
              
                'Close the document
                .Close SaveChanges:=False
                Set FSO = VBA.CreateObject("Scripting.FileSystemObject")
                FSO.CopyFile PathF & strFile, TempFolder & strFile

            End With
            
          'Get the next document
NX:            strFile = Dir()
        Wend
        
        Set dTarget = Nothing
        .Quit
    End With

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文