1

Paul Beverley が作成したこの Word マクロは、ドキュメントにコメントを追加し、ページ番号と行番号を挿入します。

Sub CommentAdd()
' Version 20.02.12
' Add a comment
' Ctrl-Alt-#
attrib1 = "PB: "
attrib2 = "PB: "

postText = ""
keepPaneOpen = False
addPageNum1 = True
addLineNum1 = True
addPageNum2 = True
addLineNum2 = True

highlightTheText = False
textHighlightColour = wdYellow
colourTheText = False
textColour = wdColorBlue

Set rng = Selection.Range
rng.Collapse wdCollapseEnd
rng.MoveEnd , 1
pageNum = rng.Information(wdActiveEndAdjustedPageNumber)  ' <<<<<----- This line
lineNum = rng.Information(wdFirstCharacterLineNumber)
If Selection.End <> Selection.Start Then
  If Right(Selection, 1) = Chr(32) Then Selection.MoveEnd wdCharacter, -1
  Set rng1 = Selection.Range
' Either highlight it ...
  myTrack = ActiveDocument.TrackRevisions
  ActiveDocument.TrackRevisions = False
  If highlightTheText = True Then Selection.Range.HighlightColorIndex _
       = textHighlightColour
' And/or change the text colour to red
  If colourTheText = True Then Selection.Font.Color = textColour
  ActiveDocument.TrackRevisions = myTrack
' Now add the comment
  Selection.Comments.Add Range:=Selection.Range
  If addPageNum1 = True Then attrib1 = attrib1 & "(p. " & _
       pageNum & ") "
  If addLineNum1 = True Then attrib1 = attrib1 & "(line " & _
       lineNum & ") "
  Selection.TypeText Text:=attrib1 & ChrW(8216) & ChrW(8217)
' Move back to between the close and open quotes
  Selection.MoveEnd wdCharacter, -1
' 'Paste' in a copy of the selected text
  Set rng2 = Selection.Range
  rng2.FormattedText = rng1.FormattedText
  rng2.Revisions.AcceptAll
  rng2.Start = rng2.End - 1
  If rng2.Text = Chr(13) Then rng2.Delete
' Move back past the close quote
  rng2.Start = rng2.End + 1
  If postText > "" Then
    rng2.InsertAfter Text:=postText
  Else
    rng2.InsertAfter Text:=" " & ChrW(8211) & " "
  End If
If keepPaneOpen = False Then ActiveWindow.ActivePane.Close
Else
  cmntText = attrib2
  If addPageNum2 = True Then cmntText = cmntText & _     ' <<<<<----- And this I guess
       "(p. " & pageNum & ") "
  If addLineNum2 = True Then cmntText = cmntText & _
       "(line " & lineNum & ") "
  Selection.MoveEnd , 1
  Selection.Comments.Add Range:=Selection.Range, Text:=cmntText
End If
End Sub

私は (Word の VB エディターで) 強調表示された行 (ここでは '<<<<----- この行' が続きます) を微調整して、マクロがページの代わりに前のヘッダーの番号を取得して書き込むようにしています。番号。ユーザーが文字列を選択すると、マクロはその上にある最も近いヘッダー レベル (h1、h2、h3 など) を探し、その番号を書き込みます。

例:

1 This is header1 > 選択した文字列がたまたまこのヘッダーの下にある場合は、「1」を書き込みます

1.2 This is header2 > 選択した文字列がたまたまこのヘッダーの下にある場合は、「1.2」と書きます

1.2.1 This is header3 > 選択した文字列がたまたまこのヘッダーの下にある場合は、「1.2.1」と書きます

これまでのところ、太字の行を置き換えるために次の 2 つのオプションを試しましたが、役に立ちませんでした (「pageNum」から「headerNum」に名前を変更しました)。

' Option 1
headerNum = ActiveDocument.Styles("Heading")

' Option 2
headerNum = Selection.range = Styles("Heading")

私は何を間違っていますか?

どんな助けでも大歓迎です!!!

4

0 に答える 0