簡體   English   中英

超鏈接列= Excel宏

[英]Hyperlink Columns = Excel Macro

我在一個文件夾中有許多pdf和excel表。 命名順序是一致的。

工作表將命名為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

設置path1時,您缺少文檔的目錄路徑。 當您單擊超鏈接以打開其查找目錄“ Apple1.pdf”時,該目錄不是有效的文件路徑。 您只需要將目錄路徑添加到路徑的開頭,該路徑應類似於“ C:\\ MyPath \\ Apple1.pdf”。 您的代碼:

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

解決方案1:假設文檔與activedocument在同一文件夾中。

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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM