以下の内容はhttps://htn20190109.hatenablog.com/entry/2026/03/21/145920より取得しました。


VBA(Word 新規ファイル作成)


https://akira55.com/excel_word/
https://morinokabu.com/2025/08/09/excel-vba-create-word-document/


-- 1. 開発タブの挿入でボタンを作成
※ActiveXコントロールのものを使用する

-- 2. デザインモードONで作成したボタンをダブルクリックするとエディタが開くので下記コードを記載。デザインモードOFFで実行

※ 下記設定必要
「Microsoft Word 16.0 Object Library」の参照設定


Option Explicit


Private Sub CommandButton1_Click()

    
    '画面を更新しない
    Application.ScreenUpdating = False
    '確認メッセージを表示しない
    Application.DisplayAlerts = False
    
    Dim Wd As New Word.Application
    Dim Doc As Word.Document
    

    If Dir(ThisWorkbook.Path & "\" & "test.docx") <> "" Then
        Kill ThisWorkbook.Path & "\" & "test.docx"
    End If
    
    Set Doc = Wd.Documents.Add
    
    Wd.Selection.Font.Size = 11
    Wd.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    
    Wd.Selection.TypeText "test1" & vbCrLf
    
    
    Wd.Selection.Font.Size = 18
    Wd.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
    
    Wd.Selection.TypeText "test2" & vbCrLf
    
    Wd.Selection.Font.Size = 11
    Wd.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    
    Wd.Selection.TypeText vbCrLf
    Wd.Selection.TypeText vbCrLf
    
    Doc.SaveAs ThisWorkbook.Path & "\test.docx"
    Doc.Close
    


    Set Doc = Wd.Documents.Open(ThisWorkbook.Path & "\test.docx")
    
    Doc.Bookmarks("\EndOfDoc").Select
    
    Wd.Selection.TypeText "hoge2" & vbCrLf
    
    
    Dim tbl As Table
    Set tbl = Doc.Tables.Add(Range:=Wd.Selection.Range, NumRows:=3, NumColumns:=4)
    
    
    Dim Cell As Cell
    Dim count As Long
    
    count = 1
    
    For Each Cell In tbl.Range.Cells
        Cell.Range.InsertAfter "セル " & count
        count = count + 1
    Next Cell
    
    Doc.Close wdSaveChanges
    
    
    
    
    Wd.Quit
    Set Doc = Nothing
    Set Wd = Nothing
    
    
    MsgBox "処理完了"
    
    
    '確認メッセージを表示する
    Application.DisplayAlerts = True
    '画面を更新する
    Application.ScreenUpdating = True


End Sub

 




以上の内容はhttps://htn20190109.hatenablog.com/entry/2026/03/21/145920より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14