选择没有声明,但不知道如何声明它
问题描述:
所有,我在Visual Studio中收到以下错误:“选择没有声明。”选择没有声明,但不知道如何声明它
我想在vb中制作一个简单的word应用程序,它允许我在文档中查找和替换多个值。我知道选择应该是文档的全部内容,并且我一直在研究MSDN,但是我一定错过了一些东西,因为我一直无法知道我应该怎么做来声明要搜索哪个选择。
我在我的项目中的两个项目:
ThisDocument.vb:
Imports Microsoft.Office.Interop.Word.Range.Select
Public Class ThisDocument
Private Sub ThisDocument_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
Me.Paragraphs(1).Range.InsertParagraphAfter()
Me.Paragraphs(2).Range.Text = "This text was added programmatically."
End Sub
Private Sub ThisDocument_Shutdown() Handles Me.Shutdown
End Sub
End Class
和Charm.vb(这是一个带状项):
Option Explicit On
Imports Microsoft.Office.Tools.Ribbon
Imports Microsoft.Office.Interop.Word.WdFindWrap
Imports Microsoft.Office.Interop.Word.WdReplace
Imports Microsoft.Office.Interop.Word.WdFindMatch
Public Class Charm
Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click
Dim custNum As String = TB_CustNum.Text
With Selection.Find
.ClearFormatting()
.Text = "care"
.Replacement.ClearFormatting()
.Replacement.Text = custNum
.Execute(Replace:=wdReplaceAll, Forward:=True,
Wrap:=wdFindContinue)
End With
End Sub
End Class
错误发生在Charm.vb的第14行。任何帮助,将不胜感激。 谢谢:)
编辑:
添加一个字一个命名空间引用之后,我现在得到在同一行上出现以下错误:
错误BC30469参考非共享成员需要一个对象引用。
答
你可以选择喜欢的东西(reference)
Dim selection = Globals.ThisDocument.Application.Selection
但与选择的工作很容易出现许多错误,而不是总是Range
相反,你可以使用范围变量:
Dim doc as Document = Globals.ThisDocument
Dim range As Range = doc.Range ' this is the main story Range without Headers and Footers
With range.Find
.Execute(FindText:="care", ReplaceWith:=custNum, Replace:=wdReplaceAll,
Forward:=True, Format:=False, Wrap:=wdFindContinue)
End With
'Selection'是Word.Application'的'成员 - 你不能没有通过互操作限定它使用它,因为全球的对象不能像他们在Word VBA。 – Comintern
您可能需要为Word.Application添加引用 – Deepesh
我现在正在收到以下错误:错误BC30469对非共享成员的引用需要对象引用。 – Change