Delphi 将Word文档中的项目符号替换为破折号
我试图用破折号替换word文档中列表的项目符号,基本上只替换“渲染图标”,即 替换下表中的项目符号:Delphi 将Word文档中的项目符号替换为破折号,delphi,vba,ms-word,activex,Delphi,Vba,Ms Word,Activex,我试图用破折号替换word文档中列表的项目符号,基本上只替换“渲染图标”,即 替换下表中的项目符号: 这是一个列表项 这是另一个列表项 还有一项 带破折号: -这是一个列表项 -这是另一个列表项 -还有一项 我将在Delphi中使用ActiveX来实现这一点,但是VB代码也可以,谢谢 这就是你正在尝试的吗 Option Explicit '~~> Select the relevant range before running this code Sub Sample()
- 这是一个列表项
- 这是另一个列表项
- 还有一项
我将在Delphi中使用ActiveX来实现这一点,但是VB代码也可以,谢谢 这就是你正在尝试的吗
Option Explicit
'~~> Select the relevant range before running this code
Sub Sample()
With ListGalleries(wdBulletGallery).ListTemplates(1).ListLevels(1)
.NumberFormat = ChrW(61485)
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleBullet
.NumberPosition = InchesToPoints(0.25)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(0.5)
.ResetOnHigher = 0
.StartAt = 1
.Font.Name = "Symbol"
.LinkedStyle = ""
End With
ListGalleries(wdBulletGallery).ListTemplates(1).Name = ""
Selection.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
ListGalleries(wdBulletGallery).ListTemplates(1), ContinuePreviousList:= _
False, ApplyTo:=wdListApplyToSelection, DefaultListBehavior:= _
wdWord10ListBehavior
End Sub
快照
执行此任务的宏
Dim oListTemplate As ListTemplate
Dim oListLevel As ListLevel
For Each oListTemplate In ActiveDocument.ListTemplates
For Each oListLevel In oListTemplate.ListLevels
If oListLevel.NumberStyle = wdListNumberStyleBullet Then
With oListLevel
.NumberFormat = "-"
.Font.Name = "Arial"
End With
End If
Next oListLevel
Next oListTemplate
在Delphi代码中:
uses ..., ComObj;
const
wdListNumberStyleBullet = 23;
var
vMSWord : variant;
Doc : Variant;
oListTemplate : Variant;
oListLevel : Variant;
iLoopTemplates, iMaxTemplates: Integer;
iLoopLevels, iMaxLevels : Integer;
begin
try
vMSWord := GetActiveOleObject('Word.Application');
vMSWord.Visible := True;
Doc := vMSWord.ActiveDocument;
iMaxTemplates := Doc.ListTemplates.Count;
for iLoopTemplates := 1 to iMaxTemplates do
begin
oListTemplate := Doc.ListTemplates.Item(iLoopTemplates);
iMaxLevels := oListTemplate.ListLevels.Count;
for iLoopLevels := 1 to iMaxLevels do
begin
oListLevel := oListTemplate.ListLevels.Item(iLoopLevels);
if (oListLevel.NumberStyle = wdListNumberStyleBullet)
and (oListLevel.NumberFormat = UTF8String(#61623))
and (oListLevel.Font.Name = 'Symbol') then
// if (oListLevel.NumberStyle = wdListNumberStyleBullet) then
begin
oListLevel.NumberFormat := UTF8String('-');
oListLevel.Font.Name := 'Arial';
end;
end;
end;
except
ShowMessage('Open a Word document before running this method');
end;
当前的IF正在检查它是否真的是一个带有•
如果不需要此检查,请注释此行(如果…)并取消注释下一行…我认为这是一个更好、更健壮的解决方案。前者看起来只是录制宏的结果,不能在多个列表或具有不同列表模板集的计算机上工作。@DorinDuminica:我不知道您是否只需要将项目符号替换为破折号。。。如果是,您需要添加第二个测试来检查当前字符是否为:.NumberFormat=ChrW(61623)和.Font.Name=“Symbol”+1非常感谢,仍然没有研究这个问题,但是如果您的代码是现成的,我将接受这个答案!(:我已经用Delphi XE2和Word 2010对其进行了测试…因此,这也应该对您有用…;o)感谢您为提供Delphi代码付出的额外努力,我接受了您的回答,因为付出了额外的努力。嗯,有一些细微的更改,但没有什么惊人的(:仅供参考,我用Delphi XE2和Word 2010对其进行了测试…;o)