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