2

たとえば、「<strong> {0}</strong>」のような文字列を切り取ってコピーできるようにしたいと思います。

次に、「Hello、World」などのコードを選択してから、「<strong> Hello、World</strong>」という結果になるマクロを呼び出します。

どうすればこれを行うことができますか?

更新:なぜこれをしたいのですか?

マクロまたはショートカットを作成して、<strong>タグなどの特定のものを選択範囲に追加することができます。ただし、その場であらゆる種類の「サラウンドウィズ」ペースト動作を作成するという私の考え。

かなり頻繁に、フィールドまたはプロパティのリストに貼り付けます。だからどこかから私は得る

PersonID
FirstName
LastName

例として、私はそれらを次のように設定したいことを知っています

FieldName = dataRow("FieldName").Value

私の魔法のマクロでは、次を選択し、CTRL+Cを押してクリップボードに入れることができます。

{0} = dataRow("{0}").Value

それから私がしなければならないのは、行ごとに行き、私の魔法のペーストを適用することです。

4

3 に答える 3

1

楽しい小さなプロジェクト。

Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module StringFormatModule

    Private clipText As String

    Public Property ClipboardText() As String
        Get
            RunThread(AddressOf GetClipboardText)
            Return clipText
        End Get
        Set(ByVal value As String)
            clipText = value
            RunThread(AddressOf CopyToClipboard)
        End Set
    End Property

    Private Function RunThread(ByVal fct As Threading.ThreadStart)
        Dim thread As New Threading.Thread(fct)
        thread.ApartmentState = Threading.ApartmentState.STA

        thread.Start()
        thread.Join()
    End Function

    Private Sub GetClipboardText()
        clipText = My.Computer.Clipboard.GetText()
    End Sub

    Private Sub CopyToClipboard()
        My.Computer.Clipboard.SetText(clipText)
    End Sub

    Sub FormatSelectedTextWithCopiedText()
        Dim formatString As String

        formatString = ClipboardText

        Dim token As String
        Dim selectedText As TextSelection
        selectedText = DTE.ActiveDocument.Selection
        token = selectedText.Text
        selectedText.Text = String.Format(formatString, token)
    End Sub
End Module

ここからクリップボードのコードを借りました。

これは機能します。テキストファイルでテストし、フォーマット文字列をクリップボード(ctrl-c)にコピーし、フォーマットするテキストを強調表示してから、マクロを実行します(マクロエクスプローラーからダブルクリックしただけですが、キーボードショートカットを作成できます)。 。

于 2009-03-20T15:25:13.047 に答える
0

{0} の代わりに & を使用します。マクロを Ctrl+Q に割り当てれば、準備完了です!

' Wraps the current selection with the specified text. Use the & character as the anchor for the selected text.
Public Sub WrapSelection()
    Dim selection As TextSelection = DirectCast(DTE.ActiveDocument.Selection, TextSelection)
    DTE.UndoContext.Open("Wrap Selection")

    Try
        Dim sInput As String = InputBox("Wrap(&&, state)")
        If Len(sInput) > 0 Then
            Dim sContent As String = selection.Text
            Dim iStart As Integer = InStr(sInput, "&") - 1
            Dim iEnd As Integer = InStrRev(sInput, "&")
            selection.Insert(sInput.Substring(0, iStart) + sContent + sInput.Substring(iEnd), vsInsertFlags.vsInsertFlagsContainNewText)
            'selection.Insert(sInput.Substring(iEnd), vsInsertFlags.vsInsertFlagsInsertAtEnd)
        End If

    Catch ex As Exception
        DTE.UndoContext.SetAborted()
        MsgBox(ex.Message)

    Finally
        'If an error occured, then need to make sure that the undo context is cleaned up.
        'Otherwise, the editor can be left in a perpetual undo context
        DTE.UndoContext.Close()

    End Try

End Sub
于 2009-09-03T17:44:07.130 に答える
0

選択したテキストの周りに「強力な」タグを追加するマクロを定義したほうがよいのではないでしょうか? 次に、それを Ctrl+B などに割り当てることができます。

テキストの両方のチャンクを選択し、マクロを 2 回呼び出す必要があるのは、私には大変な作業のように思えます。

(なぜこれをしたいのかを説明する必要があるかもしれません)

于 2009-03-19T22:27:16.767 に答える