0
votes

I have a VBA subroutine which performs miscellaneous formatting to Word documents. It relies on the Selection object (Selection.WholeStory) to apply the formatting.

This subroutine is called from VBA Outlook with a Word.Application object.

The problem that arises is: when another instance of Word is open when the macro is called, the Selection Object refers to the Word document already open, not the handler created in my macro.

VBA does not seem to qualify the selection objct, so when you write Selection.PageSetup (ie) and start applying changes, it is applied to the Document already open in Word, not the document you are handling from VBA.

I've looked around for the answer on MSDN and here, but to no luck. If anyone knows how to qualify this object, let me know. Thanks.

Basically,

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

Since "Selection" cannot be qualified, all these changes get made to whatever is already open.

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
Is scriptiing to automatically close any open Word instances an option for you?hammus
I've asked the end-user group this and am waiting for a response. Yeah, one work around I was considering is checking whether any Word docs are open and if so, prompt & end. But if anyone happens to know a more elegant solution, I'd appreciate it :)RMurphy

2 Answers

2
votes

There is probably confusion between Word's selection object and Outlook's selection object.

Use

WordApp.Selection

i.e.

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

etc.

(or e.g.

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

So that if WordApp is not in scope, you should be able to use something like

Set sel = doc.Application.Selection

)

Finally, if you can get away with using Word Range instead, I would do so (e.g. doc.Range or Doc.Content) and avoid the whole Selection thing.

0
votes

Have you tried something like this? It looks like you're getting a proper reference to the correct document at one stage in the game.

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