2017-01-26 43 views
1

我Access 2010中的形式使用VBA来用我的表中的数据Word模板。解析超链接到Word模板

到目前为止,我无法实现的是在文本中插入超链接。

为了让我更容易的事情,我将所有的数据到表模板是这样的:

Private Sub button_Click() 

On Error GoTo myError 
Dim objWRD As Object 'Word.Application 
Dim objDoc As Object 'Word.Document 
Dim db As DAO.Database 
Dim rs As DAO.Recordset 
Dim strRecords As String 

'open a query and prepare the data' 
Set db = CurrentDb() 
Set qfd = db.QueryDefs("my_query") 
Set rs = qfd.OpenRecordset() 

'open a Word template' 
Set objWRD = CreateObject("Word.Application") 
objWRD.Visible = True 
Set objDoc = objWRD.Documents.Add("path_to_my_document_template", , , True) 
objWRD.ScreenUpdating = False 


'insert records into template' 
Dim i As Integer 
i = 1 
While Not rs.EOF 
    objDoc.Tables(i).Cell(2, 1).Range.Text = "" & rs("hyperlink") 
    objDoc.Tables(i).Cell(2, 2).Range.Text = "" & rs("description") 
    rs.MoveNext 
    i = i + 1 
Wend 

rs.Close 
Set rs = Nothing 

leave: 
    Exit Sub 
myError: 
    MsgBox Error$ 
    Resume Next 
End Sub 

任何人都可以请帮我插入超链接工作的模板到rs("hyperlink")的地方吗?

回答

0

如果您引用表格单元格举行的超级链接,试试这个:

objDoc.Hyperlinks.add Anchor:=objDoc.tables(i).Cell(2, 1).Range, _ 
    Address:=rs("hyperlink") 

而且额外的文本添加到同一小区(在这种情况下,我将“文本中插入”之前超级链接

With objDoc.Tables(i).Cell(2, 1).Range 
    .Collapse Direction:=wdCollapseStart 
    .Text = "Text to Insert" & Chr(11) 
End With 

所以while循环会是这个样子:

Dim i As Integer 
    i = 1 
    While Not rs.EOF 

    objDoc.Hyperlinks.add Anchor:=objDoc.Tables(i).Cell(2, 1).Range, _ 
     Address:=rs("hyperlink") 
    With objDoc.Tables(i).Cell(2, 1).Range 
     .Collapse Direction:=wdCollapseStart 
     .Text = "Text to Insert" & Chr(11) 
    End With 
    objDoc.Tables(i).Cell(2, 2).Range.Text = "" & rs("description") 
     rs.MoveNext 
     i = i + 1 
    Wend 
+0

谢谢,RyanL,这是实际工作prett对于表格中的整个单元格都很好。我也可以使用'TextToDisplay'参数来改变链接的文本。但是如果我需要添加更多文本到同一个表格单元呢?所以我需要在同一个单元格中有一些文本和超链接。这可能吗? – yurin