other

mura vba insert word into a specific place in microsoft word

Sub CopyToWord()

   Dim wordApp As Word.Application
   Set wordApp = CreateObject("Word.Application")
   wordApp.Visible = True

   Dim wordDoc As Word.Document

   Dim target_doc As String
   target_doc = Dir(ThisWorkbook.Path & "" & "*.doc")
      
   Do While target_doc <> ""
   Set wordDoc = wordApp.Documents.Open(ThisWorkbook.Path & "" & target_doc)

   '文字列を検索
    Dim x As Variant
    
    'placeholderを検索し、そこにカーソルを移動
    With wordApp.Selection.Find
    .Text = "placeholder"
    .Execute
    End With
    
    '1行下にダウン
    wordApp.Selection.MoveDown
    
    '1行全体を選択
    wordApp.Selection.Expand Unit:=wdSection
    
    '1行消去
    wordApp.Selection.Delete
    
    '文字の入力
    wordApp.Selection.TypeText "置換後の文字列1"
    

   'ワードファイルを閉じる
    wordDoc.Close
    wordApp.Visible = False
   'Set wordApp = Nothing
   
   target_doc = Dir()
   Loop


End Sub
Was this helpful?