2017-04-04 3 views
1

実行しようとすると、実行時エラー13(タイプの不一致)が表示されます。 開いているWord文書のテキストをヘッダー内のExcelで置き換えようとしています。VBA Excelのテキストを置換する際のエラー

Set WordApp = CreateObject("Word.Application") 
WordApp.Visible = True 
Set WordDoc = WordApp.Documents.Open(myPath & "\Armaturförteckning.docx") 

' Ändrar i Armaturförteckningen 
Dim rngStory As Range 
    Dim lngJunk As Long 
    'Fix the skipped blank Header/Footer problem as provided by Peter Hewett 
    lngJunk = WordApp.ActiveDocument.Sections(1).Headers(1).Range.StoryType 
    'Iterate through all story types in the current document 
    For Each rngStory In WordApp.ActiveDocument.StoryRanges 
    'Iterate through all linked stories 
    Do 
     With WordApp.rngStory.Find 
     .Text = "ELESTATUS01" 
     .Replacement.Text = "I'm found" 
     .Wrap = wdFindContinue 
     .Execute Replace:=wdReplaceAll 
     End With 
     'Get next linked story (if any) 
     Set rngStory = WordApp.rngStory.NextStoryRange 
    Loop Until rngStory Is Nothing 
    Next 
' Stänger dokumentet 
WordApp.Documents.Save 
WordApp.ActiveDocument.Close 
+0

どのライン/命令がエラーを投げていますか? –

答えて

1

私はあなたがVBAの検索と置換をしようとしていると信じています。私たちはたくさんの機能を使用しています。長年の洗練を経て、私たちが使用しているものは次のとおりです。これは純粋に検索と置換を行う関数です。

Function SimpleSearchAndReplace(SomeDocument As Word.Document, SearchString As String, ReplaceString As String) 
With SomeDocument.Content.Find 
    .Text = SearchString 
    .Replacement.Text = ReplaceString 
    .Forward = True 
    .Wrap = wdFindContinue 
    .Format = False 
    .MatchCase = False 
    .MatchWholeWord = False 
    .MatchWildcards = False 
    .MatchSoundsLike = False 
    .MatchAllWordForms = False 
    .Execute Replace:=wdReplaceAll 
End With 
End Function 
0

「WordApp.ActiveDocument」があります。おそらくあなたが必要とするのは「WordDoc」です。あなたの 'lngJunk'と 'For Each'の行に

+0

私はに私のラインを変更: lngJunk = WordDoc.ActiveDocument.Sections(1).Headersは、(1).Range.StoryType それは (エラー438オブジェクトは、このプロパティまたはメソッドをサポートしdoesnの)ここで私にエラーをスローします – ante011

関連する問題