vba 删除 Outlook 前后的空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15522349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:08:39  来源:igfitidea点击:

Remove space before and after in Outlook

vbaoutlook-2010outlook-vba

提问by James Hill

I'm attempting to write a macro for Outlook (never written a macro, or VBA for that matter) that will remove the space before and after the text that I have selected.

我正在尝试为 Outlook 编写一个宏(从未编写过宏或 VBA),它将删除我选择的文本前后的空格。

This is what I've cobbled together from examples that I've found:

这是我从我发现的例子中拼凑出来的:

Sub FixParagraphSpacing()
    Dim objOL As Application
    Dim objDoc As Object
    Dim objSel As Object

    Set objOL = Application
    Set objDoc = objOL.ActiveInspector.WordEditor
    Set objSel = objDoc.Windows(1).Selection

    objSel.ParagraphFormat.SpaceBefore = 0
    objSel.ParagraphFormat.SpaceAfter = 0

    Set objOL = Nothing
    Set objDoc = Nothing
    Set objSel = Nothing
End Sub

The problem is that the code executes and almostnothing happens. The body of the email isn't affected, but I'm not able to remove the spacing before and after manually anymore because Outlook thinks it's already been done.

问题是代码执行后几乎没有任何反应。电子邮件的正文不受影响,但我无法再手动删除前后的间距,因为 Outlook 认为它已经完成了。

What am I missing here?

我在这里缺少什么?

Update

更新

Here is my updated code, based on @KevinPope's answer:

这是我更新的代码,基于@KevinPope 的回答:

Sub FixParagraphSpacing()
    Dim objOL As Application
    Dim sel As Object

    Set objOL = Application
    Set sel = objOL.ActiveInspector().WordEditor.Application.Selection

    For Each para In sel.Paragraphs
        para.SpaceBefore = 0
        para.SpaceAfter = 0
    Next para
End Sub

Before I run the code, here's what I see under Line and Paragraph Spacing:

在我运行代码之前,这是我在行和段落间距下看到的:

Remove Spacing

删除间距

And here's what I see after I run the macro:

这是我运行宏后看到的:

Add Spacing

添加间距

Unfortunately, other than this, no visible change is made in the body of the email.

不幸的是,除此之外,电子邮件正文中没有任何明显的变化。



Screenshot of text per request:

每个请求的文本截图:

enter image description here

在此处输入图片说明

回答by Rohit Mundhra

I too faced the same problem. When running the macro, it does seem to update the values (space before/after to 0), but doesnt apply the settings to the selected text.

我也面临同样的问题。运行宏时,它似乎确实更新了值(前后空格为 0),但不会将设置应用于所选文本。

But then adding the SpaceBeforeAuto = False worked...

但是然后添加 SpaceBeforeAuto = False 工作......

Sub FixParagraphSpacing()
    Dim objOL As Application
    Dim objDoc As Object
    Dim objSel As Object

    Set objOL = Application
    Set objDoc = objOL.ActiveInspector.WordEditor
    Set objSel = objDoc.Windows(1).Selection

    objSel.ParagraphFormat.SpaceBefore = 0
    objSel.ParagraphFormat.SpaceBeforeAuto = False
    objSel.ParagraphFormat.SpaceAfter = 0
    objSel.ParagraphFormat.SpaceAfterAuto = False

    Set objOL = Nothing
    Set objDoc = Nothing
    Set objSel = Nothing
End Sub    

回答by Kevin Pope

Something like this should sort out the line spacing before and after a selected Paragraph:

这样的事情应该整理出所选段落之前和之后的行距:

Sub Test()
    Dim objOL As Application
    Dim sel As Object

    Set objOL = Application
    Set sel = objOL.ActiveInspector().WordEditor.Application.Selection

    sel.Paragraphs(1).SpaceBefore = 0
    sel.Paragraphs(1).SpaceAfter = 0
End Sub

Let me know if this doesn't work and we can iterate on it.

如果这不起作用,请告诉我,我们可以对其进行迭代。

回答by Bryce

Try using "Selection.WholeStory" before you set the paragraph formatting. It worked for me.

在设置段落格式之前尝试使用“Selection.WholeStory”。它对我有用。