【问题标题】:VB6 Word find and replace in header Late BoundVB6 Word在标题Late Bound中查找和替换
【发布时间】:2014-10-24 18:08:32
【问题描述】:

我found this code 用于在 Word 文件中使用 VB6 到处查找和替换,但它是早期绑定的。

但是我需要它来进行后期绑定,因为我的 EXE 将在不同的系统上使用,因此我不能使用对 Word Library 的引用。

我的代码需要做的是: 在所有 Word 文件中查找红色文本并将其替换为隐藏字体。

我让它为正文工作,但标题也包含红色文本,也需要隐藏。

这是我当前的代码,它不再替代任何东西。

Private Sub PREP_Click()
Const wdColorRed = 255
Dim oWordApp As Object
On Error Resume Next
Dim fs As Object
Dim rngStory As Object
Dim lngJunk As Long
Dim oFolder As Object
Dim tFolder As Object
Dim oFile As Object
Dim strDocName As String
Dim strPathName As String
Dim locFolder As String
    locFolder = InputBox("Enter the folder path to the file(s) your want to prepare.", "File Preparation", "Type your path here... Make sure it end with a back slash, e.g. C:\myfiles\")
Set fs = CreateObject("Scripting.FileSystemObject")
Set oFolder = fs.GetFolder(locFolder)
Set tFolder = fs.CreateFolder(locFolder & "PREP")
Set tFolder = fs.GetFolder(locFolder & "PREP")
Set oWordApp = CreateObject("Word.Application")
Set rngStory = CreateObject("Word.Range")
For Each oFile In oFolder.Files
oWordApp.Visible = False
oWordApp.Documents.Open (oFile.Path)
lngJunk = oWordApp.ActiveDocument.Sections(1).Headers(1).range.StoryType
'Iterate through all story types in the current document
For Each rngStory In oWordApp.ActiveDocument.StoryRanges
'Iterate through all linked stories
Do
  With oWordApp.rngStory.Find
oWordApp.rngStory.WholeStory
oWordApp.rngStory.Find.Font.Hidden = True
oWordApp.rngStory.Find.Replacement.Font.Hidden = False
oWordApp.rngStory.Find.Execute Replace:=2
End With
  'Get next linked story (if any)
  Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
strDocName = oWordApp.ActiveDocument.Name
oWordApp.ChangeFileOpenDirectory (tFolder)
oWordApp.ActiveDocument.SaveAs FileName:=strDocName
oWordApp.ChangeFileOpenDirectory (oFolder)
Next oFile
oWordApp.Quit
Set rngStory = Nothing
Set oWordApp = Nothing
End Sub

我认为问题出在 rngStory 部分。请帮忙!

【问题讨论】:

  • 请正确格式化您的代码。左对齐的代码不可读。

标签: replace vb6 header


【解决方案1】:

那个代码不是早绑定本。绑定晚了。

Dim oWordApp As Object
Set oWordApp = CreateObject("Word.Application")

是后期绑定,在你处理 Object 的每个地方都在处理后期绑定。

【讨论】:

    【解决方案2】:

    我使用了 Selection 而不是 Range,它现在可以工作了:

    Private Sub PREP_Click()
    Const wdColorRed = 255
        Dim oWordApp As Object
        On Error Resume Next
        Dim fs As Object
        Dim rngStory As Object
        Dim myDoc As Object
        Dim oFolder As Object
        Dim tFolder As Object
        Dim oFile As Object
        Dim strDocName As String
        Dim strPathName As String
        Dim locFolder As String
            locFolder = InputBox("Enter the folder path to the file(s) your want to prepare.", "File Preparation", "Type your path here... Make sure it end with a back slash, e.g. C:\myfiles\")
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set oFolder = fs.GetFolder(locFolder)
        Set tFolder = fs.CreateFolder(locFolder & "PREP")
        Set tFolder = fs.GetFolder(locFolder & "PREP")
        Set oWordApp = CreateObject("Word.Application")
        For Each oFile In oFolder.Files
        oWordApp.Visible = False
        oWordApp.Documents.Open (oFile.Path)
        oWordApp.ActiveDocument.Sections(1).Headers(1).Range.Select
        oWordApp.Selection.WholeStory
        oWordApp.Selection.Find.Font.Color = wdColorRed
        oWordApp.Selection.Find.Replacement.Font.Hidden = True
        oWordApp.Selection.Find.Execute Replace:=2
        oWordApp.ActiveDocument.Select
        oWordApp.Selection.WholeStory
        oWordApp.Selection.Find.Font.Color = wdColorRed
        oWordApp.Selection.Find.Replacement.Font.Hidden = True
        oWordApp.Selection.Find.Execute Replace:=2
    
        strDocName = oWordApp.ActiveDocument.Name
        oWordApp.ChangeFileOpenDirectory (tFolder)
        oWordApp.ActiveDocument.SaveAs FileName:=strDocName
        oWordApp.ChangeFileOpenDirectory (oFolder)
        Next oFile
        oWordApp.Quit
        Set oWordApp = Nothing
        End Sub
    

    【讨论】:

      猜你喜欢
      • 2014-04-06
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      • 2017-10-20
      • 2018-05-03
      • 2014-10-12
      • 2019-08-13
      • 2018-01-18
      相关资源
      最近更新 更多