是否可以使用之前代码生成的文件名作为下一行代码的变量?

Is it possible to use the filename generated by previous code as the variable for next line of code?

我已经使用 vb 创建了两个文件,它们的名称基于每个文件内容的第一行。现在,我希望将两个 windows 垂直排列,从而使用 "Windows.CompareSideBySideWith"。但这是问题所在---因为这两个新文件是 "parent" 文件的 "children" 文件,我希望每当 "parent" 文件更改时, "Children" 文件名"Windows.CompareSideBySideWith" 调用也可以更改。

这里是Windows.CompareSideBySideWith

的VBs代码
Sub Macro5()
'
' Macro5 Macro
'
'
    Windows.CompareSideBySideWith _
        "Asbestos Exposures during Reprocessing of Automobile Brakes and Clutches"
End Sub

和两个子文件的vb: 第一

Sub savedoc()
'
' savedoc Macro
'
'

Dim oSection As Section
Dim r As Range
Dim TempDoc As Document
Dim FirstPara As String

For Each oSection In ActiveDocument.Sections
    Set r = oSection.Range
    r.End = r.End - 1
    Set TempDoc = ActiveDocument
    With TempDoc
        .Range = r
        FirstPara = r.Paragraphs(1).Range.text
        FirstPara = Left(FirstPara, Len(FirstPara) - 1)
        .SaveAs FileName:=FirstPara & ".doc"
    End With
    Set r = Nothing
    Set TempDoc = Nothing
Next

End Sub

第二个

Sub chinesenufi()
'
' chinesenufi Macro
'
'
Selection.WholeStory
Selection.Copy

Dim oSection As Section
Dim r As Range
Dim TempDoc As Document
Dim FirstPara As String

For Each oSection In ActiveDocument.Sections
    Set r = oSection.Range
    r.End = r.End - 1
    Set TempDoc = Documents.Add
    With TempDoc
        .Range = r
        Selection.PasteAndFormat (wdFormatOriginalFormatting)
        Selection.HomeKey Unit:=wdStory
        Selection.EndKey Unit:=wdLine
        Selection.EndKey Unit:=wdStory, Extend:=wdExtend
        Selection.Delete Unit:=wdCharacter, Count:=1
        FirstPara = r.Paragraphs(1).Range.text
        FirstPara = Left(FirstPara, Len(FirstPara) - 1)
        .SaveAs FileName:=FirstPara & "chinese" & ".doc"
    End With
    Set r = Nothing
    Set TempDoc = Nothing
Next

End Sub

我希望“汽车制动器和离合器后处理过程中的石棉暴露”能够随着两个 "children" 文件名中的任何一个的改变而改变 提前致谢:)

无意中得到如下解决方法。

 Sub chinesenufi()
    '
    ' chinesenufi Macro
    '
    '
        Selection.WholeStory
        Selection.Copy

        Dim oSection As Section
        Dim r As Range
        Dim TempDoc As Document
        Dim FirstPara As String

        For Each oSection In ActiveDocument.Sections
        Set r = oSection.Range
        r.End = r.End - 1
        Set TempDoc = Documents.Add
        With TempDoc
        .Range = r
        Selection.PasteAndFormat (wdFormatOriginalFormatting)
        Selection.HomeKey Unit:=wdStory
        Selection.EndKey Unit:=wdLine
        Selection.EndKey Unit:=wdStory, Extend:=wdExtend
        Selection.Delete Unit:=wdCharacter, Count:=1
        FirstPara = r.Paragraphs(1).Range.text
        FirstPara = Left(FirstPara, Len(FirstPara) - 1)
        .SaveAs FileName:=FirstPara & "中文檔" & ".doc"
        End With
        Set r = Nothing
        Set TempDoc = Nothing
        Next

        Dim objDoc1 As Word.Document
        Dim objDoc2 As Word.Document

        Set objDoc1 = Documents(FirstPara & ".doc")
        Set objDoc2 = Documents(FirstPara & "中文檔" & ".doc")
        objDoc1.Activate
        objDoc1.Windows.CompareSideBySideWith objDoc2
        Windows.ResetPositionsSideBySide


    End Sub

看来@ProfoundlyOblivious 认为 "setting each "child" 作为 "parent" 中的对象变量是有效的。