0

Word 文書にさまざまな書式設定を実行する VBA サブルーチンがあります。書式設定の適用は、Selection オブジェクト (Selection.WholeStory) に依存しています。

このサブルーチンは、Word.Application オブジェクトを使用して VBA Outlook から呼び出されます。

発生する問題は、マクロが呼び出されたときに Word の別のインスタンスが開いている場合、選択オブジェクトは、マクロで作成されたハンドラーではなく、既に開いている Word 文書を参照することです。

VBA は選択オブジェクトを修飾していないようです。そのため、Selection.PageSetup (つまり) を記述して変更の適用を開始すると、VBA から処理しているドキュメントではなく、Word で既に開いているドキュメントに適用されます。

MSDN と here で答えを探しましたが、うまくいきませんでした。誰かがこのオブジェクトを修飾する方法を知っているなら、私に知らせてください. ありがとう。

基本的、

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
4

2 に答える 2

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 全体を回避します。

于 2013-11-15T15:55:00.487 に答える
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
于 2013-11-15T02:55:02.840 に答える