title: How to use Word Object when Protected View is on?
body: <p>I have some code in VB.net that cycles through a directory of documents (.docx) and updates them.</p>
  
  <p>The code is fine other than if there is one document in there that is considered as 'Originated from the Internet' then the code stops since Word will by default open it in Protected View (visibility is set to false deliberately).</p>
  
  <p>Now, I know how to change the trust center settings, but I need my application to run on anyones machine  and the users (my clientele) are not likely to know how to do it.</p>
  
  <p>Is there a way round this?</p>
  
  <p>Thanks. </p>
  
  <p>---EDIT---</p>
  
  <p>In a recent example, one of the files I tried to open with my code was added to the directory externally via an FTP (by me). so when my code was cycling through all the docs in the directory, the code halted at this file since it had now 'Originated form the internet'.</p>
  
  <p>My issue is that once the document is opened, the code needs to continue and edit the document but my code cannot do so as it opened in 'Protected view'. As the word object is set to not be visible, this results in the code pausing for use input (Ordinarily choosing to Edit the document) but they can't see it to do so.</p>
  
  <p>Manually changing the Trust center settings is unfortunately not an option here, as this code is being run on hundreds of different machines by people who are not comfortable with changing it themselves - their perception is just that my code does not work,</p>
  
  <p>Here's my code that cycle through a directory and inserts an image into the header of the document. It's sloppy I know but I'm quite new to this and learning every day. I've marked where the issue occurs with "'****THIS IS WHERE MY PROBLEM IS****"</p>
  
  <pre><code>Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
      Dim oWrd As Word.Application
      Dim oTok As Word.InlineShape
      Dim FSO As New Scripting.FileSystemObject
      Dim GUID, PRTY As Scripting.Folder
      Dim fl As Scripting.File
      Dim sSure As MsgBoxResult
      Dim p, a, b, x, y, aa As Integer
      Dim bln As Boolean
  
      sSure = MsgBox("Are you Sure?", vbYesNo, "Really?")
      If sSure = vbNo Then
          Exit Sub
      End If
      For Each GUID In FSO.GetFolder(TextBox1.Text).SubFolders
          For Each PRTY In FSO.GetFolder(GUID.Path).SubFolders
              For Each fl In FSO.GetFolder(PRTY.Path).Files
                  a = a + 1
              Next
          Next
      Next
      ToolStripStatusLabel1.Text = "Running..."
      oWrd = New Word.Application
      oWrd.ScreenUpdating = False
      oWrd.Visible = False
      ToolStripProgressBar1.Value = 0
      For Each GUID In FSO.GetFolder(TextBox1.Text).SubFolders
          For Each PRTY In FSO.GetFolder(GUID.Path).SubFolders
              For Each fl In FSO.GetFolder(PRTY.Path).Files
                  x = x + 1
                  If Not fl.Path Like "*\Land Registry\*" Then
                      If Not fl.Path Like "*\Billing\*" Then
                          If Not fl.Path Like "*\Attendance Notes\*" Then
  
                              If fl.Name Like "*(LH)*" Then
  
                                  aa = aa + 1
                                  GoTo c
                              End If
                              If FSO.FileExists(Split(fl.Path, ".")(0) &amp; " (LH).dot") Then
                                  aa = aa + 1
                                  GoTo c
                              End If
  
                              y = y + 1
  '****THIS IS WHERE MY PROBLEM IS****
                              oWrd.Documents.Open(fl.Path)
  'If MS WORD DECIDES THIS DOCUMENT "ORIGINATED FROM THE INTERNET", THE CODE CANNOT RUN PAST THIS POINT DUE TO THE "PROTECTED VIEW" AND THE USER CAN'T SEE IT.
  a:
                              Try
                                  oWrd.Selection.Find.Text = "Dear "
                              Catch ex As Exception
                                  p = p + 1
                                  If p = 20 Then
                                      MsgBox("Too Many Rertys")
                                  End If
                                  GoTo a
                              End Try
                              bln = oWrd.Selection.Find.Execute
                              If bln = True Then
  
                                  If CheckBox4.Checked = True Then
                                      If oWrd.ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = False Then
  b:
                                          Try
                                              oWrd.ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
                                          Catch ex As Exception
                                              oWrd.ActiveWindow.ActivePane.View.ShowAll = True
                                              oWrd.Selection.HomeKey()
  
                                              oWrd.Selection.Find.Text = "^b"
                                              bln = oWrd.Selection.Find.Execute
  
                                              If bln = True Then
                                                  oWrd.Selection.Delete()
                                                  oWrd.ActiveWindow.ActivePane.View.ShowAll = False
                                              Else
                                                  MsgBox("oops")
                                              End If
                                              GoTo b
                                          End Try
                                      End If
                                  Else
                                      If oWrd.ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True Then
                                          oWrd.ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = False
                                      End If
                                  End If
                                  oWrd.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekFirstPageHeader
  
  
                                  oWrd.Selection.WholeStory()
                                  If oWrd.Selection.InlineShapes.Count &gt; 0 Then
                                      Dim RSLT As VBA.MsgBoxResult
  
                                      RSLT = MsgBox("HEADER TOKEN DETECTED" &amp; vbNewLine &amp; vbNewLine &amp; "Would you like to continue?", vbYesNo, "WARNING")
                                      If RSLT = MsgBoxResult.No Then
                                          oWrd.Visible = True
                                          Exit Sub
                                      End If
                                  End If
  
                                  oTok = oWrd.Selection.InlineShapes.AddPicture("http://img.photobucket.com/albums/v353/catfinger/LetterheadToken_zps7f2a4851.png")
                                  oTok.AlternativeText = "replace_Database.OtherTemplate_" &amp; Split(ComboBox1.Text, " ")(0) &amp; ".Content"
  
                                  If CheckBox2.Checked = True Then
                                      If Not TextBox2.Text = "" Then
                                          If CheckBox3.Checked = True Then
                                              oWrd.Selection.HomeKey()
                                          End If
                                          If CheckBox5.Checked = True Then
                                              oWrd.Selection.EndKey()
                                          End If
                                          For b = 1 To TextBox2.Text
                                              oWrd.Selection.TypeParagraph()
                                          Next
                                      End If
                                  End If
  
                                  oWrd.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekMainDocument
                                  If CheckBox8.Checked = True Then
                                      oWrd.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekFirstPageFooter
  
  
                                      Dim footPath As String = IO.Path.GetFullPath(My.Resources.ResourceManager.BaseName)
                                      footPath = footPath.Substring(0, footPath.Length - 39) &amp; "Resources\LetterheadToken.png"
                                      oTok = oWrd.Selection.InlineShapes.AddPicture(footPath)
                                      oTok.AlternativeText = "replace_Database.OtherTemplate_" &amp; ComboBox2.Text &amp; ".Content"
  
                                      If CheckBox2.Checked = True Then
                                          If Not TextBox3.Text = "" Then
                                              If CheckBox7.Checked = True Then
                                                  oWrd.Selection.HomeKey()
                                              End If
                                              If CheckBox6.Checked = True Then
                                                  oWrd.Selection.EndKey()
                                              End If
                                              For b = 1 To TextBox2.Text
                                                  oWrd.Selection.TypeParagraph()
                                              Next
                                          End If
                                      End If
                                  End If
                                  If CheckBox1.Checked = True Then
                                      oWrd.ActiveDocument.SaveAs2(Split(fl.Path, ".")(0) &amp; " (LH).dot")
                                  Else
                                      oWrd.ActiveDocument.Save()
                                  End If
                              End If
                              oWrd.ActiveDocument.Close()
                          End If
                      End If
                  End If
  c:
                  ToolStripStatusLabel1.Text = (x &amp; " of " &amp; a &amp; " (" &amp; y &amp; " updates so far...) / (" &amp; aa &amp; " Skipped)")
                  ToolStripProgressBar1.Value = (x / a * 100)
                  Me.Refresh()
              Next
          Next
      Next
  
      ToolStripStatusLabel1.Text = "Finished..."
      MsgBox(y &amp; " Letterheads were created out of a possible " &amp; x &amp; ".")
      ToolStripStatusLabel1.Text = "Ready..."
  End Sub
  </code></pre>