简体   繁体   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?

Very inexperienced with VBA, but trying to work on a project.对 VBA 非常缺乏经验,但正在尝试处理一个项目。 I have an excel file with lots of contact information.我有一个包含大量联系信息的 excel 文件。 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).我正在用这些信息创建一个地址簿,它需要一个 Word 文档(是的,还有其他方法可以做到这一点,但这是一个联合项目,这是选择的方法)。

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.显然,这其中的大部分内容都是以有序的方式完成工作簿,并使用 TypeText 将各种信息放在我想要的地方。

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.不过,我也想把它做成pdf,放到我的手机里,把电话号码做成超链接,这样我就可以直接拨打了。 My only snag is maintaining the hyperlink in the transfer from Excel to Word.我唯一的障碍是在从 Excel 到 Word 的传输中维护超链接。 It seems that .TypeText only carries the text, not the hyperlink formatting, so it's not doing what I need.似乎 .TypeText 只携带文本,而不是超链接格式,所以它没有做我需要的。

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对于 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 is the index of the rows. y3 是行的索引。 The 7th column in the Workbook is where the phone number is located.工作簿中的第 7 列是电话号码所在的位置。 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.在这段代码之前,我已经创建了 Word Doc,选择了一些格式,并在 Word Doc 中打印了一个部分标题。 That's why I can begin here by simply TypeText the contents of the cell in question.这就是为什么我可以在这里通过简单地 TypeText 有问题的单元格的内容开始。

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?我想知道的是:如何在这里复制电话号码的超链接,而不是简单地将文本输入到 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.为了执行您所描述的操作,从用户模拟(可能记录的宏,完全按照用户所做的操作)过渡到使用 Word 的对象模型会很有用。 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" ).而不是Selection.TypeText ,在这种情况下,它意味着使用Range对象,可以为其分配文本( Range.Text = "abc" )。 The Range can then also be used as the target object for creating a Hyperlink object.然后, Range也可以用作创建Hyperlink对象的目标对象。

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.最好的方法可能是直接在 Word 中创建一个,然后按 Alt+F9 切换到基础字段代码。 That basic format is what is needed for "link information" in the code snippet, above.上面的代码片段中的"link information"需要这种基本格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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