简体   繁体   中英

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

Very inexperienced with VBA, but trying to work on a project. I have an excel file with lots of contact information. Each entry contains Names, phone numbers, addresses for a particular family.

I am creating an address book of sorts with this information, which requires a Word document (yes, there would be other ways to do this, but it's a joint project, and this is the chosen method).

I am able to populate the entire address book in the way I want to, including page and page formatting, etc.

Obviously, most of this is a case of following through the workbook in an orderly fashion, and using TypeText to put the various pieces of information where I want them.

However, I would also like to make this a pdf, and put it on my phone, and make the phone numbers hyperlinks, so that I can dial them directly. My only snag is maintaining the hyperlink in the transfer from Excel to Word. It seems that .TypeText only carries the text, not the hyperlink formatting, so it's not doing what I need.

Is there something else I can do?

I am thinking that there is a Copy/Paste arrangement which would help me, but I lack enough experience to be able to insert such a routine in the middle of my code.

This is the piece of code which transfers the phone numbers over:

For y3 = (b + 1) To 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 is the index of the rows. The 7th column in the Workbook is where the phone number is located. The rest of this section is devoted to bringing in addresses, etc... And is not so necessary for the question.

Prior to this piece of code, I have already created the Word Doc, chosen some formatting, and printed a section heading in the Word Doc. That's why I can begin here by simply TypeText the contents of the cell in question.

What I would like to know is: How can I copy a hyperlink of a telephone number here, rather than simply typing the text into the WordDoc?

Thanks.

In order to do what you describe, it would be useful to make the transition from user emulation (the macro, likely recorded, does exactly what a user does) to working with Word's object model. Since only a code snippet is provided, it's difficult to demonstrate what this means, but...

Rather than Selection.TypeText , in this context, it means working with a Range object, to which text can be assigned ( Range.Text = "abc" ). The Range can then also be used as the target object for creating a Hyperlink object.

Taking just the line of code that seems to deal with the telephone number:

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

Note that I cannot give you the hyperlink address information as I do not know what that might be for a telephone number. My system does not hyperlink to telephone numbers. You have to research what the valid hyperlink address would be for a telephone number. Best would probably be to create one directly in Word, then press Alt+F9 to toggle to the underlying field code. That basic format is what is needed for "link information" in the code snippet, above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM