在VBA Word中限定选择对象

时间:2013-11-14 23:12:53

标签: vba ms-word

我有一个VBA子例程,它对Word文档执行各种格式化。它依赖于Selection对象(Selection.WholeStory)来应用格式化。

使用Word.Application对象从VBA Outlook调用此子例程。

出现的问题是:当调用宏时另一个Word实例打开时,选择对象引用已打开的Word文档,而不是我宏中创建的处理程序。

VBA似乎没有限定选择对象,因此当您编写Selection.PageSetup(即)并开始应用更改时,它将应用于已在Word中打开的文档,而不是您从VBA处理的文档。

我在MSDN和这里寻找答案,但没有运气。如果有人知道如何限定这个对象,请告诉我。谢谢。

基本上,

create word handler
open attachment in word
Selection.WholeStory
With  Selection.PageSetup
 .LineNumbering.Active = False
 .Orientation = wdOrientPortrait
 /* etc */
End with

由于“选择”无法合格,所有这些更改都会对已经打开的内容进行。

if numTextFiles >= 1 then
    for each textFile in textFileNames

        'Open text file in word
        Set doc = WordApp.Documents.Open(outReportFullDir & "\" & textFile)

        'Set the output name of word doc (change .txt to .docx)
        reportWordName = left(textFile, len(textFile) - 4)
        reportWordName = reportWordName & ".docx"

        'Check if out word document already exists
        preventOverwrite(outReportFullDir & "\" & reportWordName)

        'Format Reports
        formatReport()
'etc

_

Private Sub formatReport()

documents(docToFormat).select

Selection.WholeStory

'Added by Ryan to make single-spaced
WordBasic.OpenOrCloseParaBelow
WordBasic.OpenOrCloseParaBelow

Selection.Font.Name = "Courier New"
Selection.Font.Size = 8
With Selection.PageSetup
    .MirrorMargins = False
    .TwoPagesOnOne = False
    .BookFoldPrinting = False
    .BookFoldRevPrinting = False
    .BookFoldPrintingSheets = 1
    .GutterPos = wdGutterPosLeft
End With
End Sub

2 个答案:

答案 0 :(得分:2)

Word的选择对象和Outlook的选择对象之间可能存在混淆。

使用

WordApp.Selection

WordApp.Selection.WholeStory
WordApp.Selection.Font.Name = "Courier New"

(或者例如

Dim sel as Word.Selection
Set sel = WordApp.Selection
sel.WholeStory
sel.Font.Name = "Courier New"
Set sel = Nothing

因此,如果WordApp不在范围内,您应该可以使用类似

的内容
Set sel = doc.Application.Selection

最后,如果您可以使用Word Range,我会这样做(例如doc.Range或Doc.Content)并避免整个Selection事件。

答案 1 :(得分:0)

你尝试过这样的事吗?看起来你在游戏的某个阶段正确引用了正确的文档。

if numTextFiles >= 1 then
    for each textFile in textFileNames

        'Open text file in word
        Set doc = WordApp.Documents.Open(outReportFullDir & "\" & textFile)

        'Set the output name of word doc (change .txt to .docx)
        reportWordName = left(textFile, len(textFile) - 4)
        reportWordName = reportWordName & ".docx"

        'Check if out word document already exists
        preventOverwrite(outReportFullDir & "\" & reportWordName)

        'Format Reports
        Call formatReport(doc)
'etc



Private Sub formatReport(ByRef doc)

    documents(doc).select

    Selection.WholeStory

    'Added by Ryan to make single-spaced
    WordBasic.OpenOrCloseParaBelow
    WordBasic.OpenOrCloseParaBelow

    Selection.Font.Name = "Courier New"
    Selection.Font.Size = 8
    With Selection.PageSetup
        .MirrorMargins = False
        .TwoPagesOnOne = False
        .BookFoldPrinting = False
        .BookFoldRevPrinting = False
        .BookFoldPrintingSheets = 1
        .GutterPos = wdGutterPosLeft
    End With
End Sub