簡體   English   中英

如何將excel中單個單元格的超鏈接復制到由VBA aleady創建和填充的word文檔?

[英]How to copy a hyperlink from a single cell in excel to a word document which is being created and populated by VBA aleady?

對 VBA 非常缺乏經驗,但正在嘗試處理一個項目。 我有一個包含大量聯系信息的 excel 文件。 每個條目都包含特定家庭的姓名、電話號碼和地址。

我正在用這些信息創建一個地址簿,它需要一個 Word 文檔(是的,還有其他方法可以做到這一點,但這是一個聯合項目,這是選擇的方法)。

我能夠以我想要的方式填充整個地址簿,包括頁面和頁面格式等。

顯然,這其中的大部分內容都是以有序的方式完成工作簿,並使用 TypeText 將各種信息放在我想要的地方。

不過,我也想把它做成pdf,放到我的手機里,把電話號碼做成超鏈接,這樣我就可以直接撥打了。 我唯一的障礙是在從 Excel 到 Word 的傳輸中維護超鏈接。 似乎 .TypeText 只攜帶文本,而不是超鏈接格式,所以它沒有做我需要的。

還有什么我可以做的嗎?

我認為有一個復制/粘貼安排可以幫助我,但我缺乏足夠的經驗來在我的代碼中間插入這樣一個例程。

這是一段將電話號碼轉移到的代碼:

對於 y3 = (b + 1) 到 d

        'FirstPhone
        .TypeText Value(Cells(y3, 7)) & vbTab
        'LastName
        .BoldRun
        If Cells(y3, 20) = "U" Then .Font.Underline = wdUnderlineSingle
        .TypeText (Cells(y3, 3))
        .Font.Underline = wdUnderlineNone
        .BoldRun
        'Logic to determine if there needs to be a hard return, and if there are more phones
        ' Add if there is a spouse, or children, etc
        .TypeText (", " & Cells(y3, 4))
          If Cells(y3, 5).Value <> "" Then .TypeText (" & " & Cells(y3, 5))
          If Cells(y3, 6) <> "" Then .TypeText ("; " & Cells(y3, 6))
            Names = Cells(y3, 3) & ", " & Cells(y3, 4) & " & " & Cells(y3, 5) & "; " & Cells(y3, 6)
            Address = Cells(y3, 15) & ", " & Cells(y3, 17) & ", " & Cells(y3, 18) & "  " & Cells(y3, 19)
          If Len(Names) + Len(Address) > 70 Or Cells(y3, 8) <> "" Then .TypeText (Chr(11))
          If Cells(y3, 8) = "" And Address = ", ,   " Then .TypeText (Chr(11))
          If Cells(y3, 8) <> "" Then .TypeText (Cells(y3, 8))
          If Cells(y3, 15) <> "" Then .TypeText (vbTab & Address)
          If Cells(y3, 8) <> "" Or Cells(y3, 15) <> "" Then .TypeText (Chr(11))
          If Cells(y3, 9) <> "" Then .TypeText (Cells(y3, 9) & Chr(11))
          If Cells(y3, 10) <> "" Then .TypeText (Cells(y3, 10) & Chr(11))
          If Cells(y3, 11) <> "" Then .TypeText (Cells(y3, 11) & Chr(11))

        Next y3

y3 是行的索引。 工作簿中的第 7 列是電話號碼所在的位置。 本節的其余部分專門用於引入地址等......對於這個問題來說並不是那么必要。

在這段代碼之前,我已經創建了 Word Doc,選擇了一些格式,並在 Word Doc 中打印了一個部分標題。 這就是為什么我可以在這里通過簡單地 TypeText 有問題的單元格的內容開始。

我想知道的是:如何在這里復制電話號碼的超鏈接,而不是簡單地將文本輸入到 WordDoc 中?

謝謝。

為了執行您所描述的操作,從用戶模擬(可能記錄的宏,完全按照用戶所做的操作)過渡到使用 Word 的對象模型會很有用。 由於只提供了一個代碼片段,因此很難證明這意味着什么,但是......

而不是Selection.TypeText ,在這種情況下,它意味着使用Range對象,可以為其分配文本( Range.Text = "abc" )。 然后, Range也可以用作創建Hyperlink對象的目標對象。

僅采用似乎處理電話號碼的代碼行:

Dim rng as Word.Range `or Object, if late-binding is use - not evident from the code in the question
Set rng = Selection.Range
If Cells(y3, 11) <> "" Then 
    rng.Text = Cells(y3, 11) & Chr(11)  'Assign normal text
    'rng.Parent is the document. I use this since there's no information in 
    'the question about how the document is manipulated in this code
    rng.Parent.Hyperlinks.Add rng, "link information"
End If

請注意,我無法向您提供超鏈接地址信息,因為我不知道電話號碼可能是什么。 我的系統沒有超鏈接到電話號碼。 您必須研究電話號碼的有效超鏈接地址是什么。 最好的方法可能是直接在 Word 中創建一個,然后按 Alt+F9 切換到基礎字段代碼。 上面的代碼片段中的"link information"需要這種基本格式。

暫無
暫無

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

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