VBA for Word operation

Option Explicit

'[ツール] → [参照設定] → [Microsoft Word XX.X Object Library]をチェック → [OK]

Sub OpenDoc()
    Dim app As New Word.Application
    app.Visible = True
    
    'Documentを開く
    Dim doc As Word.Document
    Set doc = app.Documents.Open("C:\Users\kazuya\Desktop\folder1" & "\" & "sdxs.docx")
    'doc.Windows(1).Visible = False
    
    'Set app = Nothing 'オブジェクトを開放
End Sub

Sub DocsVisible()
   Dim doc As Word.Document
   Dim docs As Word.Documents

   
   Set docs = GetDocsOpen
   If docs Is Nothing Then Exit Sub
   
   For Each doc In docs
      doc.Windows(1).Visible = True
   Next
End Sub

Sub ReadWordToSheet()
   Dim i As Integer, p As Long, cnt As Integer
   Dim doc As Word.Document, docs As Word.Documents
   Dim para As Word.Paragraph, table As Word.table
   Dim shThis As Worksheet
   Dim ceOutPara  As Range, ceOutTable As Range
   Dim txt As String
   
   Set shThis = ActiveSheet
   
   Set docs = GetDocsOpen
   If docs Is Nothing Then Exit Sub
   Set doc = docs(1)
    
   shThis.Cells.Clear
   
   Set ceOutPara = shThis.Cells(2, 2)
   Set ceOutTable = shThis.Cells(2, 6)
   
   Application.ScreenUpdating = False
   p = 1
   ceOutPara.Value = "Paragraph"
   For Each para In doc.Paragraphs
      If para.Range.Information(wdWithInTable) = False Then
         ceOutPara.Offset(p, 0).Value = p
         
         txt = para.Range.Text
         If Left(txt, 1) = Chr(10) Or Left(txt, 1) = Chr(13) Then
            ceOutPara.Offset(p, 1).Value = ""
         Else
            ceOutPara.Offset(p, 1).Value = para.Range.Text
         End If
         p = p + 1
      End If
      
      If p > 1000 Then Exit For
   Next
   
   cnt = 0
   For Each table In doc.Tables
      cnt = cnt + 1
      ceOutTable.Value = "Table" & cnt
      table.Range.Copy
      ceOutTable.Offset(1, 0).Select
      shThis.PasteSpecial Format:="HTML", NoHTMLFormatting:=True
      
      For i = 1 To 50
         Set ceOutTable = ceOutTable.Offset(0, 1)
         If ceOutTable.End(xlDown).Row >= 1000 Then
            Set ceOutTable = ceOutTable.Offset(0, 1)
            Exit For
         End If
      Next i
   Next table
   Application.ScreenUpdating = True
End Sub

Sub WordOpe()
    'Wordを立ち上げ
    Dim app As New Word.Application
    app.Visible = True
    
    'Documentを開く
    Dim doc As Word.Document
    Set doc = app.Documents.Open(ThisWorkbook.Path & "\" & "word1.docx")
    
    Set app = Nothing 'オブジェクトを開放
End Sub

Function GetDocsOpen() As Word.Documents
   Dim app As New Word.Application
   
   On Error GoTo Label1
   Set app = GetObject(Class:="Word.Application")
   Set GetDocsOpen = app.Documents
   Set app = Nothing
   Exit Function
   
Label1:
   Set GetDocsOpen = Nothing
   Set app = Nothing
End Function


Function GetDocParas(doc As Word.Document) As String()
   Dim para As Word.Paragraph
   Dim p As Integer
   Dim pMax As Integer
   
   pMax = 1000
   ReDim aryPara(1 To pMax) As String
   p = 0
   For Each para In doc.Content.Paragraphs
      p = p + 1
      aryPara(p) = para.Range.Text
      If p > pMax Then Exit For
   Next
   ReDim Preserve aryPara(1 To p)
   GetDocParas = aryPara
   Set para = Nothing
End Function

Functions of BKedit

Add-Type -AssemblyName Microsoft.VisualBasic
$pathTop = [Environment]::GetFolderPath('Desktop')

. .\Func_Files.ps1
. .\Func_Word.ps1

function RenameBK($pathBK){
    WriteLog "Start Rename."
    $CI = Get-ChildItem $pathBK -File -Name | sort { [string]$_ }
    $cnt=0
    foreach($strOld in $CI){
        # 半角にする
        $strNew = [Microsoft.VisualBasic.Strings]::StrConv( `
                    $strOld,[Microsoft.VisualBasic.VbStrConv]::Narrow)
        # Replace 
        $strNew = $strNew.Replace("_","-").Replace(" ","").Replace("-.doc",".doc")
        # 6文字目は"-"か"."
        if($strNew.Length -ge 6){
            $subs=$strNew.Substring(5,1)
            if($subs -ne "-" -and $subs -ne "."){
                $strNew=$strNew.Insert(5,"-")
            }        
        }
        # --間は消す,()は消す
        $strNew=$strNew -replace '-.*-','-'
        $strNew=$strNew -replace '\(.*\)',''
        # Z追加
        if($strNew -match '-\w\w\.'){
            $strNew=$strNew.Replace("-","-z")
        }

        #Rename        
        if( ($strNew -match "^C\d{4}\.docx?$")-or($strNew -match "^C\d{4}-\w\w*.docx?$") ){
            if($strOld -ne $strNew){
                MyRename $strOld $strNew -RemoveByDate; $cnt++
            }
        }else{
            WriteLog ($strOld + " not-match.")
        }
    }
    WriteLog "Exec Rename $cnt Count.`r`n"
}

function RemoveBK($pathBK){
    WriteLog "Start Remove."
    $cntExec = 0;$cntError = 0
    $CI= Get-ChildItem $pathBK -File -Name | sort -Descending
    $nmKeep = ""
    foreach($nm in $CI){
        $ret=""       
        if($nm -match "C\d{4}.*\.docx?$"){
            if( (MySubstring $nmKeep 0 5)-eq(MySubstring $nm 0 5) ){
                $ret=MyRemove $nm -Force
            }else{
                $nmKeep = $nm
            }
        }else{
            if($nm -notlike "*.log"){ $ret=MyRemove $nm -Force }
        }
        if([bool]$ret){
            WriteLog $ret
            if($ret -like "Exec*"){$cntExec++}
            if($ret -like "Error"){$cntError++}
        }
    }
    WriteLog "Finished Remove. Exec:$cntExec Error:$cntError .`r`n"
}
function DocsToPdfBK($pathBK){    
    $pathPdf = "$pathBK\Pdf" 
    if(-not(Test-Path $pathPdf)){
        New-Item 'Pdf' -itemType Directory
    }
    DocsToPdfAtCurDir $pathPdf $pathBK
}

Functions of Word

#Functions_Word
$pathTop = [Environment]::GetFolderPath('Desktop')

. .\Func_Files.ps1

function sampleWord{
    $wordApp = New-Object -ComObject Word.Application
    $wordApp.Visible = $true
    $doc = $wordApp.Documents.Add()
    $doc.Content.Text = "foo, bar"
    $doc.SaveAs($fullOut)
    $doc.Close()
    $wordApp.Quit()
    $wordApp = $null
    #Start-Sleep -s 1
}

function DocOpen{
    param(
        $nmDoc,
        $AppIn,
        [switch]$ReadOnly,
        [switch]$WindowMinimized,[switch]$WindowNomal,
        [switch]$UnVisible,
        [switch]$Silence
        )
    if($AppIn -ne $null){
        [Microsoft.Office.Interop.Word.ApplicationClass]$App = $AppIn 
    }else{
        $App = New-Object -ComObject Word.Application
    }
    $App.Visible = $true
    $App.WindowState=[Windows.Forms.FormWindowState]::Normal

    $full= Convert-Path $nmDoc
    try{
        [Microsoft.Office.Interop.Word.DocumentClass]$doc = $App.Documents.Open( `
            $full,$false,[bool]$ReadOnly,$true,"","",$false,"","",0,50001,-Not([bool]$UnVisible)) 
            #FileName,confirmconversions,ReadOnly,addtorecentfiles,passworddocument,
            #passworddocument,Revert,writepassworddocument,writepassworddocument,__Format,
            #Encoding,Visible,OpenConflictDocument,openandrepair,documentdirection,NoEncodingDialog)
    }
    catch{
        if($Silence -eq $false){ Write-Host "Error - Fail Open $nmDoc" }
        return $null
    }
    
    if($WindowNomal){
        $App.ActiveWindow.Height=700
        $App.ActiveWindow.Width=800   
    }
    if($WindowMinimized){
        $App.ActiveWindow.Height=50
        $App.ActiveWindow.Width=200       
        $App.WindowState=[Windows.Forms.FormWindowState]::Maximized #名前変
    }

    $App = $null
    return $doc
}

# pathOut 指定無しなら同一フォルダ
function DocToPdf{
    param(
        [Microsoft.Office.Interop.Word.DocumentClass]$doc,
        $pathOut,
        [switch]$OverWriteByDate        
    )

    if([bool]$pathOut -eq $true){
        $fullOut= Join-Path $pathOut $doc.Name

        $fullOut= [System.IO.Path]::ChangeExtension($fullOut, ".pdf")
    }else{
        $fullOut= [System.IO.Path]::ChangeExtension($doc.FullName, ".pdf")
    }

    ExitByPasteDrive $fullOut

    if(Test-Path $fullOut){
        if($OverWriteByDate){
            $dateMoto = (Get-ItemProperty $doc.FullName).LastWriteTime
            $dateSaki = (Get-ItemProperty $fullOut).LastWriteTime
            if($dateMoto -lt $dateSaki){
                return "Cancel: NoExec by date $fullOut ."
            }
        }
        $ret = "Exec: OverWrite $fullOut ."
    }else{
        $ret = "Exec: Create $fullOut ."
    }
    $doc.ExportAsFixedFormat(     # void
        [string]$fullOut, #OutputFileName  nameだけならドキュメントに保存
        [Microsoft.Office.Interop.Word.WdExportFormat]::wdExportFormatPDF,
        [bool]$false ,#OpenAfterExport
        [Microsoft.Office.Interop.Word.WdExportOptimizeFor]::wdExportOptimizeForPrint,
        [Microsoft.Office.Interop.Word.WdExportRange]::wdExportAllDocument,
        [int]1, [int]1, 
        [Microsoft.Office.Interop.Word.WdExportItem]::wdExportDocumentContent,
        [bool]$false, #IncludeDocProps
        [bool]$false, # KeepIRM
        [Microsoft.Office.Interop.Word.WdExportCreateBookmarks]::wdExportCreateNoBookmarks,
        [bool]$true, # DocStructureTags
        [bool]$true, # BitmapMissingFonts
        [bool]$true) # UseISO19005_1

    return $ret
}

function DocsToPdfAtCurDir($pathOut,$pathLog){
    $boLog = MyTestPath $pathLog
    if($boLog){WriteLog "Start Doc-to-Pdf." $pathLog}
    else{Write-Host "Start Doc-to-Pdf."}    

    [Microsoft.Office.Interop.Word.ApplicationClass]$App = `
        New-Object -ComObject Word.Application
    $App.Visible = $true

    $nms = Get-ChildItem -Name | where{$_ -like "*.doc*"} | sort

    $nmsExec =@()
    foreach($nm in $nms){
        $fullOut= Join-Path $pathOut $nm
        $fullOut= [System.IO.Path]::ChangeExtension($fullOut, ".pdf")

        if(Test-Path $fullOut){
            $dateMoto = (Get-ItemProperty $nm).LastWriteTime
            $dateSaki = (Get-ItemProperty $fullOut).LastWriteTime
            if($dateMoto -gt $dateSaki){
                $nmsExec += $nm
            } 
        }else{
            $nmsExec += $nm
        }
    }
    $cntExec = $nmsExec.Length 
    $rate = 0.4
    $msg = "$cntExec files will exec. Wait. Remain " + (MyRound ($rate*$cntExec/60) -1) +" minutes."
    if($boLog){WriteLog $msg $pathLog}else{Write-Host $msg}
    $tSta = Get-Date

    if($cntExec -gt 0){
        $cnt = 0
        foreach($nm in $nmsExec){
            [Microsoft.Office.Interop.Word.DocumentClass]$doc = DocOpen $nm $App -ReadOnly -UnVisible -Silence
            if([bool]$doc){
                $ret = DocToPdf $doc $pathOut -OverWriteByDate
                if($boLog){WriteLog $ret $pathLog}else{Write-Host $ret}
                $doc.Close()

                $cnt++
                if($cnt % 20 -eq 0){
                    $tNow = Get-Date
                    if($cnt -gt 0){ $rate = (($tNow-$tSta).TotalSeconds)/$cnt }else{$rate = 0.4 }
                    Write-Host ([string]$cnt + "/" + [string]$cntExec + " files execed. Remain " `
                                    + (MyRound ($rate*($cntExec-$cnt)/60) -1) +" minutes.")
                }
            }else{
                WriteLog "Error: Fail to OpenDoc $nm ."
                $flagErrOpen=$true
            }
        }
        $tNow = Get-Date
        if($cnt -gt 0){ $rate = (($tNow-$tSta).TotalSeconds)/$cnt }else{$rate = 0.4 }
    }
    $App.Quit()
    $App=$null
    [System.GC]::Collect()

    $msg = ("Finished Doc-to-Pdf. Taked " + (MyRound (($tNow-$tSta).TotalSeconds/60) -1) `
                + " minutes. Rate is " + (MyRound $rate -1) + " seconds.")
    if($boLog){WriteLog $msg $pathLog}
    else{Write-Host $msg }

    if($flagErrOpen){
        $msg="OpenDocエラー有り。以下の何れかを試して。`r`n `
                ・Wordのオプション⇒セキュリティセンターでファイル制限機能のチェックを外す。`r`n・Docを最新形式で保存する。"
        ResByMsgbox $msg 
    }


}

VBA for OpenSelectionFile

Option Explicit

Sub 行非表示()
   'Rows.RowHeight = 18.75
   Dim va As Variant, ro As Range
   Dim words As Variant
   words = Array("aa", "ab")
   For Each va In words
      Set ro = Cells.Find(va, , , xlWhole, xlByColumns).EntireRow
      ro.Hidden = True
   Next
End Sub
Sub 行再表示()
    Rows.EntireRow.Hidden = False
End Sub
Function GetBtnCaller() As Button
   Dim sp As Shape
   Set sp = ActiveSheet.Shapes(Application.Caller)
   sp.Select
   Set GetBtnCaller = Selection
End Function
Sub 行追加()
   Dim i As Integer, ceBuf As Range
   Dim flag1 As Boolean, flag2 As Boolean
   Dim txtBtn As String, strFind As String, ceFind As Range
   Dim rgCopy As Range
   行再表示
   行非表示

   txtBtn = GetBtnCaller.Characters.Text
   
   strFind = Mid(txtBtn, InStr(txtBtn, " ") + 1)
   Set ceFind = Cells.Find(strFind, , , xlWhole, xlByColumns)
   
   For i = 1 To 100
      Set ceBuf = ceFind.Offset(0, i)
      If ceBuf.Borders(xlEdgeTop).LineStyle = xlNone Then
         Exit For
      End If
   Next
   Set rgCopy = Range(ceFind.Offset(0, 1), ceBuf.Offset(0, -1))
   
   For i = 1 To 100
      Set ceBuf = ceFind.Offset(i, 1)
      If ceBuf.Borders(xlEdgeLeft).LineStyle = xlNone Then
         flag1 = True
      End If
      If flag1 = True And ceBuf.Borders(xlEdgeLeft).LineStyle <> xlNone Then
         flag2 = True
      End If
      If flag2 = True And ceBuf.Borders(xlEdgeLeft).LineStyle = xlNone Then
         Exit For
      End If
   Next
   
   rgCopy.Copy ceBuf
   ceBuf.Select
   
End Sub

Sub OpenSelectionFile()
   Dim va As Variant
   Dim sIn As String, pathSerch As String, fullFi As String
   Dim ceFol As Range
   sIn = Selection.Value
   If sIn = "" Then Exit Sub
   Set ceFol = Cells.Find("Folder", , , xlWhole).Offset(0, 1)
   pathSerch = GetPathDesktop & "\" & ceFol.Value
   fullFi = pathSerch & "\" & "*" & sIn & "*"
   
   Dim ListFiles() As String
   ListFiles = GetFilesList(pathSerch, ("*" & sIn & "*"))
   
   Call ArySortByS(ListFiles)
   Dim nmOpen As String
   
   nmOpen = ListFiles(UBound(ListFiles))
   fullFi = pathSerch & "\" & nmOpen
   Call OpenOtherApp(fullFi)
   
End Sub
Public Function GetPathDesktop()
    Dim WSH As Object
    Set WSH = CreateObject("Wscript.Shell")
    Dim PathTop As String
    PathTop = WSH.SpecialFolders("Desktop")
    Set WSH = Nothing
    GetPathDesktop = PathTop
End Function
Public Sub ArySortByS(ByRef ary() As String)
    Dim i, j As Byte
    Dim judg As Integer
    Dim buf As String
    
    For j = 0 To UBound(ary)
    For i = 1 To UBound(ary) - 1 - j
        judg = StrComp(ary(i), ary(i + 1), vbTextCompare)
        If judg = 1 Then    'str1がstr2より大きい
            buf = ary(i + 1)
            ary(i + 1) = ary(i)
            ary(i) = buf
        End If
    Next
    Next
End Sub
Sub OpenOtherApp(fiFull As String)
    CreateObject("Wscript.Shell").Run """" & fiFull & """"
End Sub
Public Function GetFilesList(pathFolder As String, Optional nmPattern1 As String = "" _
        , Optional nmPattern2 As String = "", Optional nmPattern3 As String = "") As String()
   Dim n, p As Integer
   ReDim aryList(0) As String
   Dim patterns As Variant
   patterns = Array(nmPattern1, nmPattern2, nmPattern3)
   
   n = 0
   For p = LBound(patterns) To UBound(patterns)
     If patterns(p) <> "" Then
       aryList(n) = Dir(pathFolder & "\" & patterns(p))
       Do While Len(aryList(n)) > 0
           n = n + 1
           ReDim Preserve aryList(0 To n)
           aryList(n) = Dir()
       Loop
     End If
   Next
   ReDim Preserve aryList(0 To n - 1)
   GetFilesList = aryList
End Function

Exection for DocToPdf

$pathTop = [Environment]::GetFolderPath('Desktop')
cd "$pathTop\ps"
. .\Func_FormDialog.ps1
. .\Func_Word.ps1

$pathsInOut = GetFolderPathsByFormInOut -InitPathInput "" -InitPathOutput ""

while($true){
    if($pathsInOut -eq $null){
        exit
    }

    $pathIn= $pathsInOut[0]
    $pathOut= $pathsInOut[1]

    # message作成
    $CI = Get-ChildItem $pathIn -Name | where{$_ -like "*.doc*"} | sort
    $nokori =$CI.Count - 5    
    $msg = ($CI |select -First 5) -join "`n"
    if($nokori -gt 0){ $msg = "$msg`n他 $nokori 件" }
    $msg = "$msg `n`n実行するよ?"

    #実行確認
    $result = [System.Windows.Forms.MessageBox]::Show($msg,"実行確認","YesNo","Question","Button2")
    #実行
    if($result -eq $resYes){
        cd $pathIn
        DocsToPdfAtCurDir($pathOut)
        break
    }else{
        $pathsInOut = GetFolderPathsByFormInOut -InitPathInput $pathIn -InitPathOutput $pathOut
    }
}

Functions of FormDialog

Add-Type -AssemblyName System.Windows.Forms

$pathTop = [Environment]::GetFolderPath('Desktop')
cd "$pathTop\ps"
. .\Func_Files.ps1
. .\Func_Form.ps1

# memo
<#
WindowHandle
ウィンドウごとに附番される番号。
画面上にあるウィンドウやオブジェクトを認識するため、この数値(ハンドル)を用いる。

メッセージボックス
[System.Windows.Forms.MessageBox]::Show(メッセージ,タイトル,ボタン,アイコン,初期フォーカスボタン)
#>


function GetFileNameByDialog{
    param(
        [string]$Title = "ファイルを選択",
        [switch]$FilterImage, 
        [switch]$InitialDesktop
    )
    $dialog = New-Object System.Windows.Forms.OpenFileDialog
    $dialog.Title = $Title
    $dialog.Multiselect = $false # 複数選択

    if($FilterImage -eq $true){
        $dialog.Filter = "画像ファイル(*.PNG;*.JPG;*.GIF)|*.PNG;*.JPG;*.JPEG;*.GIF"
    }

    $dialog.InitialDirectory = "C:\"
    if($InitialDesktop -eq $true){
        $dialog.InitialDirectory = [Environment]::GetFolderPath('Desktop')
    }

    # ダイアログを表示
    if($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK){
        return $dialog.FileName  # MultiselectならFileNames
    }
}

function GetFolderPathByDialog{
    param(
        [string]$Description = "フォルダを選択",
        [string]$PathRoot,
        [switch]$RootIsCurrent, 
        [switch]$RootIsDesktop
    )    

    # pathRoot
    if( (MyTestPath $PathRoot) -eq $false ){
        $pathRoot = "C:"
    }
    if($RootIsCurrent -eq $true){
        $pathRoot = $PWD.Path
    }
    if($RootIsDesktop -eq $true){
        $pathRoot = [Environment]::GetFolderPath('Desktop')
    }    

    # ダイアログ表示
    $shell = New-Object -com Shell.Application 
    $folderPath = $shell.BrowseForFolder(0,$Description,0,$pathRoot) 
    
    # return
    if($folderPath -eq $null){
        return $null
    }else{
        return $folderPath.Self.Path
    }
}

function GetFolderPathByDialog2{
    param(
        [string]$Description = "フォルダを選択",
        [switch]$DefaultCurrent, 
        [switch]$DefaultDesktop
    )

    # メインウィンドウ
    $window = New-Object Windows.Forms.NativeWindow
    $handle = ([Diagnostics.Process]::GetCurrentProcess()).MainWindowHandle    
    $window.AssignHandle($handle)
    

    # ダイアログ
    $dialog = New-Object System.Windows.Forms.FolderBrowserDialog
    $dialog.Description = $Description
    if($DefaultCurrent -eq $true){
        $dialog.SelectedPath = $PWD.Path
    }
    if($DefaultDesktop -eq $true){

    }    

    # ダイアログ表示
    $res = $dialog.ShowDialog($window)

    if($res -eq [System.Windows.Forms.DialogResult]::OK){
        return $dialog.SelectedPath
    }
    else{
        return $null
    }
}

function GetFolderPathsByFormInOut{
    param(
        [string]$InitPathInput = "",
        [string]$InitPathOutput = ""
    )

    # Table,Controls 定義
    #region

    # Table 定義
    $Table1=GetNewTable -nCol 4 -nRow 6 -hRow 30 -SideMargin 10

    # Controls 定義
    $Lab1= GetNewLabel "Input Folder" -BottomLeft | SetToTable $Table1 -nRow 0 -spanCol 3
    $Table1.RowStyles[0].Height=20

    $Txb1 = GetNewTextBox -MojiSize 9 -Wrap | SetToTable $Table1  -nRow 1 -spanCol 3 -spanRow 1
    $Btn1= GetNewButton "Ref" | SetToTable $Table1 -nRow 1 -nCol 3 -spanCol 1

    $Lab2= GetNewLabel "Output Folder" -BottomLeft | SetToTable $Table1 -nRow 2 -spanCol 3

    $Txb2 = GetNewTextBox -MojiSize 9  -Wrap | SetToTable $Table1 -nRow 3 -spanCol 3 -spanRow 1
    $Btn2= GetNewButton "Ref" | SetToTable $Table1  -nRow 3 -nCol 2 -spanCol 1

    $Table1.RowStyles[4].Height=15

    $BtnOK= GetNewButton "OK" | SetToTable $Table1 -nRow 5 -nCol 1 -spanCol 1
    $BtnCancel= GetNewButton "Cancel" | SetToTable $Table1  -nRow 5 -nCol 2 -spanCol 1


    $Txb1.Text=$InitPathInput
    $Txb2.Text=$InitPathOutput

    #endregion


    # function for Event
    function ClickRef($txb){    
        $pathGet = GetFolderPathByDialog -RootIsDesktop
        if([bool]$pathGet){            
            $txb.text=$pathGet
        }
    }   
    function ClickCancel{
        $Form1.DialogResult = [Windows.Forms.DialogResult]::Cancel
        $Form1.Close()
    }
    function ClickOK{
        if( ((MyTestPath $Txb1.Text) -eq $true) -and ((MyTestPath $Txb2.Text) -eq $true) ){
            $Form1.DialogResult = [Windows.Forms.DialogResult]::OK
            $Form1.Close()
        }else{
            [System.Windows.Forms.MessageBox]::Show("Error - paths not found ", "Error") 
        }
    }
    # Event
    $Btn1.Add_Click({ClickRef $Txb1})
    $Btn2.Add_Click({ClickRef $Txb2})
    $BtnOK.Add_Click({ClickOK})
    $BtnCancel.Add_Click({ClickCancel})


    # Form 定義
    $Form1 = GetNewForm -text "Folder Select" -PositionCenter
    # Form Event
    $Form1.Add_Shown({
        $Form1.Activate()  
        $BtnCancel.Select()   
    })

    # Form 表示
    $Form1.Controls.Add($Table1)

    $res = $Form1.ShowDialog() 
    if($res -eq [Windows.Forms.DialogResult]::OK){
        return $Txb1.Text,$Txb2.Text
    }
}

Functions of NamesAndPaths

$pathTop = [Environment]::GetFolderPath('Desktop')
cd "$pathTop\ps"
. .\Func_Files.ps1

$kugiri = "`t"

# ps\Data に出力
function OutTxtNamesAndPaths{
    param(
        [string]$pathRoot,
        [string]$keyWild        
        )
    
    # ファイル名作成
    $nmRoot = Split-Path $pathRoot -Leaf
    $nmFileOut = "NamesAndPaths-$nmRoot.txt"
    $nmFileOut = CorrectName $nmFileOut

    # cd
    CdMyData
    # Out Root
    ("Root :" + $pathRoot) | Out-File -FilePath $nmFileOut
    # CI
    Write-Host "--------------------------------------"
    Write-Host "Wait. Now Getting ChildItem."
    $tSta = Get-Date
    $cnt =0
    $CI = Get-ChildItem $pathRoot -File -Recurse |where{$_.Name -like $keyWild} |
        ForEach-Object{
            ($_.Name + $kugiri + ($_.DirectoryName).Replace($pathRoot,"") ) `
             | Out-File -FilePath $nmFileOut -Append
             $cnt++
             if(($cnt % 500) -eq 1 ){Write-Host "$cnt Count"}         
        }
    $tSpan = ((Get-Date)-$tSta).totalseconds
    $strOut = ("$cnt Count "+[string](MyRound $tSpan 2)+" sec") 
    $strOut+"`r`n"+(Get-Date) | Out-File -FilePath $nmFileOut -Append
    Write-Host $strOut
    Write-Host "Exec - Out to $nmFileOut"
}

function SerchFilePathByTxt{
    param(
        $nameWild,
        $pathRoot        
        )
    # ファイル名作成
    $nmRoot = Split-Path $pathRoot -Leaf
    $nmTxt = "NamesAndPaths-$nmRoot.txt"
    $nmTxt = CorrectName $nmTxt

    CdMyData

    #get file-content
    $Contents = Get-Content $nmTxt

    foreach($el in $Contents){
        $nm = ($el -split $kugiri )[0]
        if($nm -like $nameWild){
            $aryPaths += ,@( ($el -split $kugiri )[1] )
        }
    }
    
    $aryPaths = $aryPaths | Sort-Object | Get-Unique # 重複を削除
    foreach($el in $aryPaths){
        $aryPathsOut += ,@($pathRoot + $el)
    }
    return $aryPathsOut
}