如何在文档中查找段落样式 [英] How to find paragraph style in document

查看:67
本文介绍了如何在文档中查找段落样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想阅读word文档并获取该文档中的所有格式样式。

我的问题是找到该文件中的样式。



这是我的代码

Hi guys,

I want to read a word document and get all the formatting styles in that document.
My problem is to find the styles in that document.

Here is my code

Dim doc As New word.Document
Dim docapp As New word.Application
Dim para_tag As word.Paragraph
Dim paracount As Integer

For Each para_tag In docapp.ActiveDocument.Paragraphs
      MsgBox(para_tag.Style)
Next





Plz给出一些其他建议..

谢谢...



Plz give some other suggestion..
Thanks...

推荐答案

代码似乎没问题,但需要一些更正:

使用早期绑定

Code seems to be OK, but need some corrections:
Using early binding
Dim sFileName As String = "C:\Test.doc"
Dim docapp As New Word.Application
Dim doc As Word.Document = docapp.Documents.Open(sFileName)
Dim para_tag As Word.Paragraph
Dim paracount As Integer

For Each para_tag In doc.Paragraphs
     paracount += 1 
     MsgBox(para_tag.Style.ToString(), MsgBoxStyle.OKOnly, "Para no.:" & paracount.ToString())
Next





迟到绑定



Using late binding

Dim sFileName As String = "C:\Test.doc"
Dim docapp As Object = CreateObject("Word.Application")
Dim doc As Object = docapp.Documents.Open(sFileName)
Dim para_tag As Object
Dim paracount As Integer

For Each para_tag In doc.Paragraphs
     paracount += 1 
     MsgBox(para_tag.Style.ToString(), MsgBoxStyle.OKOnly, "Para no.:" & paracount.ToString())
Next





我建议你要添加尝试......抓住...结束尝试 [ ^ ]块。







好​​的,测试一下:



I suggest you to add Try...Catch...End Try[^] block.



Ok, test it:

Module Module1

    Sub Main()
        Dim sFileName As String = "C:\MyDocument.doc"
        EnumWrdPara(sFileName)
        Console.ReadKey()

    End Sub


    Sub EnumWrdPara(sDocFullName AS String)
        Dim docapp As Object = Nothing
        Dim doc As Object = Nothing
        Dim para_tag As Object = Nothing
        Dim paracount As Integer = 0


        Try
            docapp = CreateObject("Word.Application")
            doc = docapp.Documents.Open(sDocFullName)
            For Each para_tag In doc.Paragraphs
                paracount += 1
                Console.WriteLine("Paragraph no.: {0} - style: (1)", paracount.ToString(), para_tag.Style.ToString)
            Next


        Catch ex As System.NullReferenceException
            Console.WriteLine(ex.Message)

        Catch ex As IO.IOException
            Console.WriteLine(ex.Message)

        Catch ex As Exception
            Console.WriteLine(ex.Message)

        Finally
            para_tag = Nothing
            doc.Close(SaveChanges:=False)
            doc = Nothing
            docapp.Quit()
            docapp = Nothing

        End Try

    End Sub

End Module





它有效,已经过检查。 ;)

[/ EDIT]



It works, it has been checked. ;)
[/EDIT]


这篇关于如何在文档中查找段落样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆