通过 VBA 将样式复制到打开的文档时文件被用户锁定
我有一个带有更新样式的活动文档。我里面也有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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论