Word VBA 在所有超链接之前和之后插入纯文本空格

问题描述 投票:0回答:1

我想在转录本中的时间戳之后添加空格,该转录本粘贴时时间戳超链接和周围文本之间没有空格。

oLink.Range.InsertBefore " "
插入纯文本,但
oLink.Range.InsertAfter " "
将添加的文本附加到超链接描述中 - 这意味着在空格后键入的任何内容也将成为超链接的一部分。

如何在纯文本后面插入空格?

vba text ms-word hyperlink insert
1个回答
0
投票

这是我在超链接后插入纯文本的方法

Sub H___AddSpace_PlainTextBeforeAndAfterHyperlinks()

Dim oLink As Hyperlink

'======= R___PRESELECTED_RANGE_SetAndRESELECTafterLOOPS macro
'1.===== Name pre-selected range (if LOOP is going to be executed or called on a TABLE) R___PRESELECTED_RANGE_SetAndRESELECTafterLOOPS Macro
    Dim PRESELECTED_RANGE As Range
        
    '1a.________ if no selection, msgbox select all, dont select or exit
    If Selection.Range = "" Then
        SELECT_RANGE = MsgBox("Select whole document?" & vbNewLine & _
                "Cancel to make selection manually!" & vbNewLine & _
                "No will run only on the selection type the cursor is in.", _
                vbYesNoCancel + vbDefaultButton2)
            If SELECT_RANGE = vbYes Then
                Selection.WholeStory 'selects whole document - then save selection below
            ElseIf SELECT_RANGE = vbCancel Then
                Exit Sub
            End If 'SELECT_RANGE options (will continue with no selection made if yes or cancel not pressed)
    End If

    '1b.________ set preselected range
    Set PRESELECTED_RANGE = Selection.Range 'needs to be set after every preselected range dim, and after every loop


'2.======= Loop
     For Each oLink In PRESELECTED_RANGE.Hyperlinks

'2a.---- insert space before (inerts as plain text)
    oLink.Range.InsertBefore " "

'2b.----- INSERT SPACE AFTER
    oLink.Range.Select
        Selection.MoveRight Unit:=wdCharacter, Count:=1 'COLLAPSES TO END
        Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend 'select following letter
        Selection.Copy 'copy following character
        Selection.TypeText Text:=" " 'type space over letter
        Selection.PasteAndFormat (wdFormatOriginalFormatting) 'paste the copied letter after the space
    
Next oLink
Set oLink = Nothing

'3.====== reselect range for exit or next action (in MAIN macro, before LOOP macros called, +after each "next" command)
PRESELECTED_RANGE.Select 'add after every "next" - for exit or next procedure

MsgBox "done"
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.