2015-10-30 65 views
0

我需要批量設置一組文檔的超鏈接基本屬性。我可以使用.net和OPEN XML打開docx,但是我在進行下一步操作時遇到問題,並正在更改正確的屬性。在下面的示例中,我得到了字符的數量,但不知道這是我應該遵循的路徑。如何使用VB.Net更改DOCX文檔中的HyperlinkBase屬性

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 

回答

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 
相關問題