【问题标题】:How to prevent link formatting from changes when editing link using VBA?使用 VBA 编辑链接时如何防止链接格式更改?
【发布时间】:2014-01-09 11:29:42
【问题描述】:

我目前正在使用以下代码来更新我的应用程序中的所有链接:

Sub AddSources()
    Dim pubPage As Page
    Dim pubShape As Shape
    Dim hprlink As Hyperlink
    Dim origAddress() As String
    Dim exportFileName As String
    exportFileName = "TestResume"
    Dim linkSource As String
    linkSource = "TestSource2"

    For Each pubPage In ActiveDocument.Pages
        For Each pubShape In pubPage.Shapes
            If pubShape.Type = pbTextFrame Then
                For Each hprlink In pubShape.TextFrame.TextRange.Hyperlinks
                    If InStr(hprlink.Address, "http://bleaney.ca") > 0 Then
                        origAddress = Split(hprlink.Address, "?source=")
                        hprlink.Address = origAddress(0) + "?source=" + linkSource
                    End If
                Next hprlink
            End If
        Next pubShape
    Next pubPage
    ThisDocument.ExportAsFixedFormat pbFixedFormatTypePDF, "C:\" + exportFileName + ".pdf"
End Sub

问题是当我更新链接时,它们会丢失格式。如何保留超链接的格式?我尝试查看Copy and Paste 方法,但似乎我真正需要的是Paste Special,它不存在于超链接对象的 Range 属性中。

【问题讨论】:

  • 您想保留哪些属性?
  • 特别是颜色和缺少下划线,但如果可能的话,我想将其推广到所有格式

标签: vba ms-publisher


【解决方案1】:

尝试添加以下行来捕获颜色和下划线,然后在地址更改后将其设置回来

Sub AddSources()
    Dim pubPage As Page
    Dim pubShape As Shape
    Dim hprlink As Hyperlink
    Dim origAddress() As String
    Dim exportFileName As String
      Dim undline AS Long
      Dim clr AS Long
    exportFileName = "TestResume"
    Dim linkSource As String
    linkSource = "TestSource2"

    For Each pubPage In ActiveDocument.Pages
        For Each pubShape In pubPage.Shapes
            If pubShape.Type = pbTextFrame Then
                For Each hprlink In pubShape.TextFrame.TextRange.Hyperlinks
                    If InStr(hprlink.Address, "http://bleaney.ca") > 0 Then
                        undline = hprlink.Range.Font.Underline
                        clr = hprlink.Range.Font.Color
                        origAddress = Split(hprlink.Address, "?source=")
                        hprlink.Address = origAddress(0) + "?source=" + linkSource
                        hprlink.Range.Font.Color = clr
                        hprlink.Range.Font.Underline = undline
                    End If
                Next hprlink
            End If
         Next pubShape
    Next pubPage
    ThisDocument.ExportAsFixedFormat pbFixedFormatTypePDF, "C:\" + exportFileName + ".pdf"
End Sub

【讨论】:

  • 不幸的是,我在文档中有不同颜色的超链接,所以这不会将它们全部更改为相同的颜色(不受欢迎)吗?
  • 好的,你说的是概括所有格式,所以我认为这意味着概括所有超链接我更新了我的答案
  • 谢谢,成功了!我之前尝试过类似的东西,但由于某种原因我无法让它工作。
猜你喜欢
  • 2010-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-30
  • 2019-12-11
  • 2013-01-09
相关资源
最近更新 更多