简体   繁体   English

错误5180“ Word无法打开文档模板”

[英]Error 5180 “Word Cannot open document template”

I want to count the number of pages in Word documents using Excel VBA but some files can't be opened, with 我想使用Excel VBA计算Word文档中的页数,但是某些文件无法打开,

"Error 5180 Word Cannot open document template". “错误5180 Word无法打开文档模板”。

Function PageWord(FullFile_Name As Variant, PF As Long)
Dim objWord As Object
Dim objDoc As Object

On Error Resume Next
Set objWord = CreateObject("Word.Application")
objWord.Visible = False


Set objDoc = objWord.Documents.Open(filename:="" & FullFile_Name & "", ReadOnly:=False)
objDoc.Repaginate


PageWord = objDoc.BuiltinDocumentProperties(14)   'Pages
Debug.Print PageWord & "-" & FullFile_Name


objWord.Quit (False)
End Function  

How to open these files or is there another way to get the page counts? 如何打开这些文件,或者还有另一种获取页数的方法?

It's possible to get the page count without opening the file, using Microsoft Developer Support OLE File Property Reader 2.1 使用Microsoft开发人员支持OLE文件属性阅读器2.1,无需打开文件就可以获取页数。

http://www.microsoft.com/en-us/download/details.aspx?id=8422 http://www.microsoft.com/en-us/download/details.aspx?id=8422

Once installed, you can instantiate an instance of the property reader and find the page count like this: 安装后,您可以实例化属性读取器的实例,并按以下方式查找页数:

Function PageWord(FullFile_Path as string)

    Dim dso As Object

    Set dso = CreateObject("DSOFile.OleDocumentProperties") 
    dso.Open (FullFile_Path) 
    PageWord = dso.SummaryProperties.PageCount
    dso.Close 

    Debug.Print PageWord & "-" & FullFile_Path

End Function

If you instantiate the dso object outside of the function and pass it along with the path of the file (or if you scope it to be accessible to the function), you won't need to open and close a bunch of dso objects, either. 如果您在函数外部实例化dso对象并将其与文件路径一起传递(或者如果您将其范围限定为函数可以访问),则无需打开和关闭一堆dso对象,或者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM