如何使用VB.Net更改DOCX文档中的HyperlinkBase属性

时间:2015-10-30 14:18:55

标签: vb.net ms-word openxml

我需要为一组文档批量设置Hyperlink Base属性。我可以使用.net和OPEN XML打开docx,但是我在下一步到达并更改正确的属性时遇到了问题。在下面的示例中,我得到了字符数,但不确定这是我应该遵循的路径。

Private Sub ListFiles(ByVal lst As ListBox, ByVal pattern As String, ByVal dir_info As DirectoryInfo)
    ' Get the files in this directory.
    Dim fs_infos() As FileInfo = dir_info.GetFiles(pattern)
    For Each fs_info As FileInfo In fs_infos

        Dim xmlProperties As XmlDocument = New XmlDocument
        Dim wordDoc As WordprocessingDocument = WordprocessingDocument.Open(fs_info.FullName, False)
        Dim appPart As ExtendedFilePropertiesPart = wordDoc.ExtendedFilePropertiesPart
        Dim props = wordDoc.PackageProperties
        xmlProperties.Load(appPart.GetStream)
        Dim chars As XmlNodeList = xmlProperties.GetElementsByTagName("Characters")
        MessageBox.Show("Number of characters in the file = " + chars.Item(0).InnerText, "Character Count")

    Next fs_info
    fs_infos = Nothing


End Sub

1 个答案:

答案 0 :(得分:0)

以下是如何做我想要的事情:

Private Sub ListFiles(ByVal lst As ListBox, ByVal pattern As String, ByVal dir_info As DirectoryInfo)

Dim fs_infos() As FileInfo = dir_info.GetFiles(pattern)
    Dim message As String
    For Each fs_info As FileInfo In fs_infos

        Dim wordDoc As WordprocessingDocument = WordprocessingDocument.Open(fs_info.FullName, True)
        Dim props = wordDoc.ExtendedFilePropertiesPart.Properties

        message = fs_info.Name.ToString & ": Old base hyperlink: " & props.HyperlinkBase.Text & " New base hyperlink: " & cboPaths.Text
        lstFiles.Items.Add(message)


        Dim hylnkBase As DocumentFormat.OpenXml.ExtendedProperties.HyperlinkBase = New DocumentFormat.OpenXml.ExtendedProperties.HyperlinkBase()

        hylnkBase.Text = cboPaths.Text

        props.HyperlinkBase = hylnkBase

        wordDoc.Close()

    Next fs_info
    fs_infos = Nothing End Sub