启用“受保护的视图”时如何使用Word对象?

The*_*tor 3 vb.net ms-word

我在VB.net中有一些代码可以循环浏览文档目录(.docx)并更新它们。

该代码很好,除非其中有一个文档被视为“来自互联网”,然后该代码将停止,因为Word默认情况下会在“受保护的视图”中将其打开(故意将可见性设置为false)。

现在,我知道如何更改信任中心设置,但是我需要我的应用程序在任何人的计算机上运行,​​并且用户(我的客户)不太可能知道如何执行此操作。

有办法解决吗?

谢谢。

- -编辑 - -

在最近的示例中,我尝试用代码打开的文件之一是通过FTP从外部添加到该目录的(由我执行)。因此,当我的代码遍历目录中的所有文档时,由于该代码现在“来自互联网原始版本”,因此该代码停止在该文件中。

我的问题是,一旦打开文档,代码就需要继续并编辑文档,但是我的代码无法这样做,因为它是在“保护视图”中打开的。由于将单词对象设置为不可见,这会导致代码暂停使用输入(通常选择编辑文档),但他们看不到这样做。

遗憾的是,手动更改“信任中心”设置不是一种选择,因为该代码是由不适应自己更改的人在数百台不同的计算机上运行的,他们只是认为我的代码无法正常工作,

这是我的代码,它在目录中循环,并将图像插入文档的标题中。我知道这很草率,但我对此很陌生,每天都在学习。我已用“'****这是我的问题所在****”标记了发生问题的位置

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) & " (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 > 0 Then
                                    Dim RSLT As VBA.MsgBoxResult

                                    RSLT = MsgBox("HEADER TOKEN DETECTED" & vbNewLine & vbNewLine & "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_" & Split(ComboBox1.Text, " ")(0) & ".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) & "Resources\LetterheadToken.png"
                                    oTok = oWrd.Selection.InlineShapes.AddPicture(footPath)
                                    oTok.AlternativeText = "replace_Database.OtherTemplate_" & ComboBox2.Text & ".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) & " (LH).dot")
                                Else
                                    oWrd.ActiveDocument.Save()
                                End If
                            End If
                            oWrd.ActiveDocument.Close()
                        End If
                    End If
                End If
c:
                ToolStripStatusLabel1.Text = (x & " of " & a & " (" & y & " updates so far...) / (" & aa & " Skipped)")
                ToolStripProgressBar1.Value = (x / a * 100)
                Me.Refresh()
            Next
        Next
    Next

    ToolStripStatusLabel1.Text = "Finished..."
    MsgBox(y & " Letterheads were created out of a possible " & x & ".")
    ToolStripStatusLabel1.Text = "Ready..."
End Sub
Run Code Online (Sandbox Code Playgroud)

The*_*tor 5

所以这最终就是我想要的:

要将文档强制进入受保护的视图并在此处进行编辑,请执行以下操作:

Sub OpenPVThenEdit

ProtectedViewWindows.Open("DOCUMENT PATH HERE")
ProtectedViewWindows(1).Edit

End Sub
Run Code Online (Sandbox Code Playgroud)

- - - - - - 要么 - - - - - - -

如果您没有其他Word文档打开,并且想要检查文档是否已在“受保护的视图”中打开:

Sub TestForPVw

Documents.Open("DOCUMENT PATH HERE")
    If ProtectedViewWindows.Count > 0 Then
            ProtectedViewWindows(1).Edit
    End If

End Sub
Run Code Online (Sandbox Code Playgroud)

- - - - - - 要么 - - - - - - -

如果确实有其他Word文档打开,并且想要进行检查以查看是否已在“受保护的视图”(未测试)中打开了该特定文档:

Sub TestForPVw
Dim dDoc as Document
Set dDoc = Documents.Open("DOCUMENT PATH HERE")
    If ProtectedViewWindows.Count > 0 Then
            ProtectedViewWindows(dDoc.Name).Edit
    End If

End Sub
Run Code Online (Sandbox Code Playgroud)