如何从Excel VBA更改Word页脚的变量?

时间:2019-04-12 18:59:02

标签: excel vba ms-word

我有几个变量可以在Excel的Word页脚中进行更新。 我只能在页脚外面更改变量。

Sub Internal_Offer()
Dim datos(1 To 100) As String
Dim reemp(1 To 100) As String

wArch = Hoja1.Range("B2").Text & Hoja1.Range("B1").Text & ".docx"

Set objWord = CreateObject("Word.Application")
objWord.Visible = True

objWord.Documents.Add Template:=wArch, NewTemplate:=False, DocumentType:=0

lenght = Hoja1.Range("B3").Value
For i = 1 To lenght - 1 'celda dónde está la cuenta
    datos(i) = Hoja1.Range("A" & i + 3).Text  'dónde están los datos
    reemp(i) = Hoja1.Range("B" & i + 3).Text 'dónde están las etiquetas
Next i
objWord.Activate 'Activa el documento de word

For i = 1 To lenght - 1 'celda dónde está la cuenta

     With objWord.Selection.Find
            .Text = datos(i) 'busca el texto de datos
            .Replacement.Text = reemp(i) 'reemplaza por el texto
            .Forward = True
            .Wrap = 1
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Execute Replace:=2 
     End With
Next i

End Sub

1 个答案:

答案 0 :(得分:0)

至少有两种方法可以处理Word文档的所有页脚:

  • StoryRanges
  • 部分。脚

请尝试以下操作(ActiveDocument是您的objWord):

Private Sub CheckAllDocumentFooters()
    Dim r As Word.Range
    Dim s As Word.Section
    Dim hf As Word.HeaderFooter

    ' either all story ranges:
    For Each r In ActiveDocument.StoryRanges
        Select Case r.StoryType
        Case wdEvenPagesFooterStory, wdPrimaryFooterStory, wdFirstPageFooterStory
            r.WholeStory
            Debug.Print r.Text
        End Select
        ' further sections:
        While Not (r.NextStoryRange Is Nothing)
            Set r = r.NextStoryRange
            Select Case r.StoryType
            Case wdEvenPagesFooterStory, wdPrimaryFooterStory, wdFirstPageFooterStory
                r.WholeStory
                Debug.Print r.Text
            End Select
        Wend
    Next r

    ' or all sections:
    For Each s In ActiveDocument.Sections
        For Each hf In s.Footers
            Debug.Print hf.Index
            Debug.Print hf.Range.Text
        Next hf
    Next s

End Sub