【问题标题】:Hyperlink Columns = Excel Macro超链接列 = Excel 宏
【发布时间】:2015-02-06 03:17:50
【问题描述】:

我在一个文件夹中有许多 pdf 文件和一张 excel 表格。命名顺序一致。

Sheet 将被命名为 Apple。 Pdfs 将被命名为 Apple_1、Apple_2

我想要一个 excel 宏工作 获取活动工作表名称。 超链接 G 列中的单元格。 当我单击单元格 1 中的文本时,它应该打开 Apple_1.pdf 当我单击单元格 2 时,它应该打开 Apple_2.pdf。 这应该一直持续到该列中的文本填充单元格为止。

我有一个相同的 Word 宏,但我不知道如何使它在 excel 中工作。下面是宏这个词。

Sub macro3()
Dim tbl As Table
Dim coll As Column
Dim path As String
Dim pdf As String
Dim path1 As String
pdfname = ActiveDocument.Name
pdfname = Left(pdfname, Len(pdfname) - 4)
pdfname = Replace(pdfname, " ", "_")
Set tbl = ActiveDocument.Tables(1)
Set coll = tbl.Columns(7)
Set colpdf = tbl.Columns(7)
i = 0
For Each c In coll.Cells
If (i <> 0 And InStr(c, ".pdf") > 0) Then
path1 = pdfname & "_" & i & ".pdf"
ActiveDocument.Hyperlinks.Add Anchor:=c.Range, Address:=path1
End If
i = i + 1
Next
End Sub

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    您在设置 path1 时缺少文档的目录路径。当您单击超链接以打开它在“Apple1.pdf”的目录中查找时,该目录不是有效的文件路径。您只需将目录路径添加到路径的开头,该路径应类似于“C:\MyPath\Apple1.pdf”。 你的代码:

    pdfname = ActiveDocument.Name
    pdfname = Left(pdfname, Len(pdfname) - 4)
    pdfname = Replace(pdfname, " ", "_")
    path1 = pdfname & "_" & i & ".pdf"
    

    解决方案1:假设文档与活动文档位于同一文件夹中。

    Dim MyPath as string
    MyPath = ActiveDocument.Path
    path1 = MyPath & "\" & pdfname & "_" & i & ".pdf"
    

    解决方案2:文件位于另一个位置,您可以添加另一个字符串地址。

    Dim MyPath as string
    MyPath = "C:\MyOtherLocation"
    path1 = MyPath & "\" & pdfname & "_" & i & ".pdf"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多