2013-10-21 37 views
0

我有2個標題或標記是我的RTF文檔的一部分。在我的例子中,我顯示了一個句子,實際上它會是多個句子或段落。我使用了括號而不是小於和大於符號,因爲它們在我的問題中消失了。我想要做的就是用下面的句子「text goes here」替換2個標記之間的文本,而不用引號。刪除2個標題之間的所有文本Word 2010,使用VBA

[EmbeddedReport]大量文字,數千字的,多個段落[/ EmbeddedReport]

我想替換所有2個標記爲「文本到這裏」替代之間的文本。

它最終會變成這樣......

"[EmbeddedReport]text goes here[/EmbeddedReport]" 

我硬是花了2天嘗試解決這個問題。任何幫助,將不勝感激。


這是我想的最後一件事...

Sub RemoveReport() 

    Dim c As Range 
    Dim StartWord As String, EndWord As String 

    Selection.HomeKey Unit:=wdStory 



    StartWord = "<ImageTable>" 
    EndWord = "</ImageTable>" 

    Set c = ActiveDocument.Content 
    c.Find.ClearFormatting 
    c.Find.Replacement.ClearFormatting 
    With c.Find 
     .Text = StartWord & "*" & EndWord 
     ' MsgBox (.Text) 
     .Replacement.Text = "<ImageTable>text goes here</ImageTable>" 
     .Forward = True 
     .Wrap = wdFindStop 
     .Format = False 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = True 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 

    c.Find.Execute 

    While c.Find.Found 
     Debug.Print c.Text 
     '~~> I am assuming that the start word and the end word will only 
     '~~> be in the start and end respectively and not in the middle 
     Debug.Print Replace(Replace(c.Text, StartWord, ""), EndWord, "") 
     c.Find.Execute 
    Wend 



End Sub 
+0

有關您編寫的代碼問題的問題必須描述特定的問題 - 幷包含有效的代碼以進行重現。詢問代碼的問題必須證明對所解決問題的最小理解。包括嘗試解決方案,爲什麼他們沒有工作,以及預期的結果。 **我的解決方案**是使用[Find](http://msdn.microsoft.com/en-us/library/office/ff839118.aspx)對象與[MatchWildcards](http://msdn.microsoft .com/en-us/library/office/ff838695.aspx)屬性設置爲「True」。給它一個鏡頭,如果你無法弄清楚,我們可以提供幫助。 –

+0

@David Zemens ...我是StackOverFlow.com的新手。抱歉給你帶來不便。我編輯並添加了代碼。它只是沒有做任何事情。我沒有從我在網上發現的類似問題中借用代碼。這不是他們嘗試過的唯一的方法,但是我嘗試過的其他方法也不成功。這段代碼看起來對我來說最合乎邏輯,但我肯定錯過了一些東西。 –

+0

這段代碼與我的建議非常相似。給我一分鐘我會調整它。 –

回答

2

字VBA是不是我的專業領域,但似乎類似於我前幾天回答的問題。

發現通配符匹配沒有做我希望它做的事情,或者至少它不可靠。另外,我用角括號碰到了一些麻煩,所以這使用了方括號。我懷疑這個詞將尖括號視爲標記/語法,因此不會將它們解釋爲查找對象中的文本。可能有解決方法,但Word VBA不是我的專長。也有可能是一個更好的解決方案,但同樣,字VBA是不是我的專長:)

嘗試這樣:

Option Explicit 
Sub Test() 
Dim doc As Document 
Dim txtRange As Range 
Dim startTag As String 
Dim endTag As String 
Dim s As Long 
Dim e As Long 

startTag = "[EmbeddedReport]" 
endTag = "[/EmbeddedReport]" 
Set doc = ActiveDocument 
Set txtRange = doc.Content 
'Find the opening tag 
With txtRange.Find 
    .Text = startTag 
    .Forward = True 
    .Execute 
    If .Found Then 
     s = txtRange.Start 
    Else 
     GoTo EarlyExit 
    End If 
End With 
'Find the closing tag 
Set txtRange = doc.Range(txtRange.End, doc.Content.End) 
With txtRange.Find 
    .Text = endTag 
    .Forward = True 
    .Execute 
    If .Found Then 
     e = txtRange.End 
    Else 
     GoTo EarlyExit 
    End If 
End With 
Set txtRange = doc.Range(s, e) 
txtRange.Text = startTag & "text goes here" & endTag 

Exit Sub 


EarlyExit: 
MsgBox "Header not found in this document!", vbInformation 

End Sub 

這需要一些時間來弄明白在第一,但學習瀏覽VBA的object model reference文檔將使這些任務在將來更容易理解。

+0

David Zemens ...這看起來很有希望。它似乎在工作!我需要做進一步的測試。我可能會在測試後的今晚或明天發表評論。謝謝你讓我朝着正確的方向前進。 –

+0

@donaldadams您應該將答案標記爲接受,如果它工作 –

相關問題