我正在使用 Office 2016。我从此处从 Microsoft 网站复制了一些文本。 Microsoft 网站上的代码片段采用灰色背景格式。现在,当我复制此文本并将其粘贴到 Microsoft Word 文档时,我想要复制的文本前景色,并希望在粘贴操作期间删除灰色背景色。有什么办法可以做到这一点吗?
这是我的问题
您可以使用 VBA 轻松完成此操作(您可以手动完成,但我认为那里的词很奇怪):
Sub RemoveAll()
With ActiveDocument.Range.Font
With .Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorAutomatic
End With
End With
End Sub
我创建了一个 VBA 脚本,按下 CTRL+V 即可执行以下功能。您可以在 VBA 脚本中注释掉不需要的函数。我不是 VBA 程序员,只是使用 ChatGPT、MSDN 帮助、StackOverflow 和大量的尝试创建了这个解决方案。希望它将来可以帮助其他人!
注意: 如果由于某种原因您不希望在粘贴期间执行 VBA 脚本,那么您始终可以通过右键单击内容菜单中的
paste
选项进行粘贴,或者您可以为此自定义粘贴指定一些自定义组合键来运行来自 VBA。
它作为一个单元执行上述所有任务,因此,如果您按 CTRL+Z 撤消所有粘贴的文本,则会将其作为单个操作删除。 我已经在Word文档中解释了
how to set up VBA script
这里
这是VBA脚本代码
Option Explicit
Private Sub Document_Open()
With Application.KeyBindings.Add(WdKeyCategory.wdKeyCategoryCommand, "PasteAndFormatText", BuildKeyCode(WdKey.wdKeyControl, WdKey.wdKeyV))
End With
End Sub
Private Sub Document_Close()
' Reset the CTRL+V key combination when the document is closed
Application.KeyBindings.ClearAll
End Sub
Sub PasteAndFormatText()
Dim oUndo As UndoRecord
Set oUndo = Application.UndoRecord
' Begin the custom undo unit
oUndo.StartCustomRecord "Paste and Format"
On Error Resume Next
Dim startPos As Long
' Store the start position
startPos = Selection.Start
' Paste the content from the clipboard
Selection.PasteAndFormat (wdFormatOriginalFormatting)
' Extend the selection from the start position to the current selection end
Selection.SetRange Start:=startPos, End:=Selection.End
' Now, with the pasted content selected, change its formatting
ApplyDocumentDefaultNormalStyleForFontAndParagraph
ApplyNoSpaceBetweenParagraphsOfSameStyleRule
RemoveBackgroundColor
ChangeFontColorToBlackForLightColorFont
SetTableWidthAndBorders
On Error GoTo 0
' End the custom undo unit
oUndo.EndCustomRecord
End Sub
Sub RemoveBackgroundColor()
' Remove background color from the pasted text
With Selection.Font
With .Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorAutomatic
End With
End With
End Sub
Sub ApplyNoSpaceBetweenParagraphsOfSameStyleRule()
With Dialogs(wdDialogFormatParagraph)
.NoSpaceBetweenParagraphsOfSameStyle = 1
.Execute 'This sets it.
End With
End Sub
Sub ApplyDocumentDefaultNormalStyleForFontAndParagraph()
Dim para As Paragraph
' Now, with the pasted content selected, change its formatting for Paragraph and font
With Selection.Font
.Name = ActiveDocument.Styles(wdStyleNormal).Font.Name ' Change font name as needed
.Size = ActiveDocument.Styles(wdStyleNormal).Font.Size ' Change font size as needed
End With
' Loop through paragraphs and set spacing
For Each para In Selection.Paragraphs
para.SpaceBefore = ActiveDocument.Styles(wdStyleNormal).ParagraphFormat.SpaceBefore
para.SpaceAfter = ActiveDocument.Styles(wdStyleNormal).ParagraphFormat.SpaceAfter
para.LineSpacingRule = ActiveDocument.Styles(wdStyleNormal).ParagraphFormat.LineSpacingRule
Next para
End Sub
Sub ChangeFontColorToBlackForLightColorFont()
'If light font(like white, yellow etc) on dark background and if we remove background the light color text is not visible, so change font to black.
Dim char As Range
' Loop through each character in the selection
For Each char In Selection.Characters
' Check if the font color is light then change it to black. We will check for 1'st char only for efficiency.
If IsColorLight(char.Font.color) Then
char.Font.color = RGB(0, 0, 0)
Else
Exit For
End If
Next char
End Sub
Function IsColorLight(color As Long) As Boolean
Dim R As Long, G As Long, B As Long
Dim Y As Double ' Luminance
' Extract the RGB components from the color
R = color Mod 256
G = (color \ 256) Mod 256
B = (color \ 65536) Mod 256
' Calculate the luminance
Y = 0.299 * R + 0.587 * G + 0.114 * B
' Check if the luminance exceeds a certain threshold for "lightness"
' Adjust the threshold value if necessary. A common starting point is around 186, based on the formula's maximum output of 255
If Y > 186 Then
IsColorLight = True
Else
IsColorLight = False
End If
End Function
Sub SetTableWidthAndBorders()
'If your selection has tables then show all borders and set table width to be within print margin
Dim tbl As Table
Dim doc As Document
Dim pageWidth As Single, leftMargin As Single, rightMargin As Single
Dim tableWidth As Single
Set doc = ActiveDocument
' Assuming the page setup is consistent across the document
With doc.PageSetup
pageWidth = .pageWidth
leftMargin = .leftMargin
rightMargin = .rightMargin
End With
' Calculate available width for the table
tableWidth = pageWidth - leftMargin - rightMargin
' Check each table in the document
For Each tbl In doc.Tables
' Check if the table is within the current selection
If (tbl.Range.Start >= Selection.Start And tbl.Range.End <= Selection.End) Then
' Set table preferred width
tbl.PreferredWidthType = wdPreferredWidthPoints ' Set the width in points
tbl.PreferredWidth = tableWidth ' Set the width to the calculated tableWidth
' Adjust table borders
With tbl.Borders
.InsideLineStyle = wdLineStyleSingle
.OutsideLineStyle = wdLineStyleSingle
.InsideLineWidth = wdLineWidth025pt
.OutsideLineWidth = wdLineWidth025pt
.InsideColor = wdColorAutomatic
.OutsideColor = wdColorAutomatic
End With
End If
Next tbl
End Sub