excel 在outlook上循环遍历表的行,并将(要显示的文本)更改为每行的升序

j1dl9f46  于 2023-06-07  发布在  其他
关注(0)|答案(2)|浏览(209)

我用下面的代码循环通过选择outlook和转换成超链接和改变文本显示Link
它工作,但它增加了递增的数字到所有细胞,如下图所示:

我的需要是添加每行的升序,如下图所示:

在此,非常感谢您的帮助。

Sub Hyperlink_Outlook()

  Dim wDoc As Word.Document, rngSel As Word.Selection, cel As Cell, i As Long
  
  Set wDoc = Application.ActiveInspector.WordEditor
  Set rngSel = wDoc.Windows(1).Selection
    
  If Not rngSel Is Nothing And rngSel.Information(wdWithInTable) Then
    If rngSel.Range.Cells.Count > 0 Then
       For Each cel In rngSel.Cells
         If Len(cel.Range.Text) > 10 Then
           i = i + 1
            wDoc.Hyperlinks.Add cel.Range, _
             Address:=Left(cel.Range.Text, Len(cel.Range.Text) - 1), _
             TextToDisplay:="Attachment " & i
         End If
       Next
    End If
  End If
  
End Sub
ivqmmu1c

ivqmmu1c1#

尝试先遍历行(以下是测试的):

Sub Hyperlink_Outlook()

  Dim wDoc As Word.Document, rngSel As Word.Selection, cel As Cell, i As Long
  Dim r As Variant
  
  Set wDoc = Application.ActiveInspector.WordEditor
  Set rngSel = wDoc.Windows(1).Selection
    
  If Not rngSel Is Nothing And rngSel.Information(wdWithInTable) Then
    If rngSel.Range.Cells.Count > 0 Then
      For Each r In rngSel.Rows
        i = 0 ' reset i here
        For Each cel In r.Cells
          If Len(cel.Range.Text) > 10 Then
            i = i + 1
             wDoc.Hyperlinks.Add cel.Range, _
              Address:=Left(cel.Range.Text, Len(cel.Range.Text) - 1), _
              TextToDisplay:="Attachment " & i
          End If
        Next cel
      Next r
    End If
  End If
  
End Sub
8fq7wneg

8fq7wneg2#

请尝试下一个修改的代码:

Sub Hyperlink_OutlookCols()
 'Columns in Excel must be autoFit
  Dim wDoc As Word.Document, rngSel As Word.Selection, cel As Word.Cell
  
  Set wDoc = Application.ActiveInspector.WordEditor
  Set rngSel = wDoc.Windows(1).Selection
    
  If Not rngSel Is Nothing And rngSel.Information(wdWithInTable) Then
    If rngSel.Range.Cells.count > 0 Then
       For Each cel In rngSel.Cells
          If Len(cel.Range.Text) > 10 Then
             wDoc.Hyperlinks.Add cel.Range, _
              Address:=Left(cel.Range.Text, Len(cel.Range.Text) - 1), _
                                         TextToDisplay:="Attachment " & cel.Column.Index
          End If
       Next cel
    End If
  End If
  
End Sub

您应该在自动调整相应列后从Excel中复制单元格...

相关问题