2016-06-12 69 views
0

我有這個Word VBA代碼,它刪除域代碼,但保留其值。這很好,但不是在標題中。我怎樣才能編輯它爲文檔主體(以及頁眉/頁腳)工作?MS WORD - 刪除域代碼,保留標題中的值

Sub RemoveFieldCodeButRetainValue() 
    Dim d As Document 
    Dim iTemp As Integer 
    Dim strTemp As String 

    Set d = ActiveDocument 

    For iTemp = d.Fields.Count To 1 Step -1 

     strTemp = d.Fields(iTemp).Result 

     d.Fields(iTemp).Select 

     With Selection 
      .Fields(1).Delete 
      .TypeText strTemp 
     End With 
    Next 
End Sub 

回答

0

好吧,我知道了:

使用兩個宏:

Sub CtrlAPlusFNine() 
    Selection.WholeStory 
    Dim oStory As Range 
    For Each oStory In ActiveDocument.StoryRanges 
     oStory.Fields.Update 
     If oStory.StoryType <> wdMainTextStory Then 
      While Not (oStory.NextStoryRange Is Nothing) 
       Set oStory = oStory.NextStoryRange 
       oStory.Fields.Update 
      Wend 
     End If 
    Next oStory 
lbl_Exit: 
    Set oStory = Nothing 
    Exit Sub 
End Sub 


Sub RemoveFieldCodeButRetainValue() 
    Dim d As Document 
    Dim iTemp As Integer 
    Dim strTemp As String 
    Set d = ActiveDocument 
    For iTemp = d.Fields.Count To 1 Step -1 
     strTemp = d.Fields(iTemp).Result 
     d.Fields(iTemp).Select 
     With Selection 
      .Fields(1).Delete 
      .TypeText strTemp 
     End With 
    Next 
End Sub 

..和使用Application.Run

+0

第三宏調用這兩個,但我有另一個查詢同樣的:如何刪除「交叉引用」(通過vba宏),但保留其價值? – vicki