title: Qualify Selection Object in VBA Word
body: <p>I have a VBA subroutine which performs miscellaneous formatting to Word documents.  It relies on the Selection object (Selection.WholeStory) to apply the formatting.</p>
  
  <p>This subroutine is called from VBA Outlook with a Word.Application object.  </p>
  
  <p>The problem that arises is: <strong>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.</strong></p>
  
  <p>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.</p>
  
  <p>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.    </p>
  
  <p>Basically,</p>
  
  <pre><code>create word handler
  open attachment in word
  Selection.WholeStory
  With  Selection.PageSetup
   .LineNumbering.Active = False
   .Orientation = wdOrientPortrait
   /* etc */
  End with
  </code></pre>
  
  <p>Since "Selection" cannot be qualified, all these changes get made to whatever is already open.</p>
  
  <pre><code>if numTextFiles &gt;= 1 then
      for each textFile in textFileNames
  
          'Open text file in word
          Set doc = WordApp.Documents.Open(outReportFullDir &amp; "\" &amp; textFile)
  
          'Set the output name of word doc (change .txt to .docx)
          reportWordName = left(textFile, len(textFile) - 4)
          reportWordName = reportWordName &amp; ".docx"
  
          'Check if out word document already exists
          preventOverwrite(outReportFullDir &amp; "\" &amp; reportWordName)
  
          'Format Reports
          formatReport()
  'etc
  </code></pre>
  
  <p>_</p>
  
  <pre><code>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
  </code></pre>