gpt4 book ai didi

vb.net - 当保护 View 打开时如何使用Word对象?

转载 作者:行者123 更新时间:2023-12-02 02:48:35 25 4
gpt4 key购买 nike

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

代码没有问题,只是如果其中有一个文档被视为“源自 Internet”,则代码会停止,因为 Word 默认情况下会在 protected View 中打开它(可见性故意设置为 false)。

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

有办法解决这个问题吗?

谢谢。

---编辑---

在最近的示例中,我尝试使用代码打开的文件之一已通过 FTP(由我)从外部添加到目录中。因此,当我的代码循环遍历目录中的所有文档时,代码会在该文件处停止,因为它现在“源自互联网”。

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

不幸的是,手动更改信任中心设置在这里不是一个选项,因为这些代码正在数百台不同的机器上运行,而这些人不愿意自己更改它 - 他们的看法只是我的代码不起作用,

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

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

最佳答案

所以这最终就是我正在寻找的:

要强制将文档置于 protected View 中并从那里进行编辑:

Sub OpenPVThenEdit

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

End Sub

------------或-------------

如果您没有打开其他 Word 文档,并且想要检查该文档是否已在 protected View 中打开:

Sub TestForPVw

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

End Sub

------------或-------------

如果您确实打开了其他 Word 文档并想要检查此特定文档是否已在 protected View 中打开(未经测试):

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

关于vb.net - 当保护 View 打开时如何使用Word对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26917156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com