2017-02-09 10 views
0

すべて、ビジュアルスタジオで「選択が宣言されていません」というエラーが表示されます。選択は宣言されていませんが、宣言する方法はありません

私は文書内の複数の値を見つけて置き換えることができるVBで簡単な単語アプリケーションを作ろうとしています。私はその選択が文書の内容全体でなければならないことを認識しています。私はMSDNについて研究していますが、検索する選択肢を宣言するために何をすべきかを知ることができなかったので、

は、私は私のプロジェクトに2つのアイテムを持っている:

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行目でエラーが発生しています。どんな助けもありがとう。 はあなたを:)ありがとう

EDIT:非に

エラーBC30469リファレンス:

Wordの名前空間の参照を追加した後、私は今、同じ行に、次のエラーを取得しています共有メンバーにはオブジェクト参照が必要です。

+1

「選択」は、Word.Applicationのメンバであり、グローバルオブジェクトはWord VBAのように使用できないため、Interopで修飾しないと使用できません。 – Comintern

+0

Word.Applicationの参照を追加する必要がある可能性があります。 – Deepesh

+0

次のエラーが表示されます。エラーBC30469非共有メンバーへの参照にはオブジェクト参照が必要です。 – Change

答えて

1

あなたは(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 
関連する問題