プロが教えるわが家の防犯対策術!

VBA有識者の方にご相談です。
以下の作業をマクロ化したいのですが、コードのご教授お願いいたします。

エクセルファイルのシート1のA列に文章と数値が入力されています。
1.wordを立ち上げて、A列のデータをwordに転記したいです。
2.転記したwordの余白を狭くしたいです。
3.wordのヘッダー右側に【常勤役員会】、フッター中央には【1、2】とページ番号を入れたいです。
4.可能であれば、wordのファイル名は【“転記した日”_常勤役員会】としたいです。

下記のVBAで、1のA列のデータを転記するところまではできましたが、2~4はどうしたらよいか分かりません。
よろしくお願いいたします。
ーーーーーーーーーーーーーーーーーーー
Sub
Dim WordApp As Object
Dim WordDoc As Word.Document
Dim I, lRow As Long
Dim ExcelText
Set WordApp = CreateObject("Word.Application")

lRow = Cells(Rows.Count, "A").End(xlUp).Row
With WordApp
.Visible = True
.Documents.Add

For I = 2 To lRow
ExcelText = Cells(I, "A").Text & vbLf
.Selection.TypeText ExcelText
Next I
End With
End Sub

A 回答 (2件)

こんにちは


'2.転記したwordの余白を狭くしたいです。
回答は左だけでしたね。上下左右の場合は

With WordApp
WordDoc.TopMargin = .MillimetersToPoints(10)
WordDoc.BottomMargin = .MillimetersToPoints(10)
WordDoc.LeftMargin = .MillimetersToPoints(10)
WordDoc.RightMargin = .MillimetersToPoints(10)

数値はポイント数(適時調整してください)

・・Margin はActiveDocument (WordDoc)オブジェクト
・・Millimeters はWord.Application(WordApp)オブジェクトです

WordDoc.PageSetup.LeftMargin =MillimetersToPoints(10)のように
命令するとWord参照が残り、続けて2回以降実行すると
実行時エラー462が返りますので注意してください
    • good
    • 0

こんばんは


1度投稿したのですが・・時間がかかったからか、どこかに消えました

また、書くの大変なので 割愛して コードを示しますので
ステップ実行やコードのリファレンスなどで確認してください

applicationで分けています
中途半端なWith括りは、内容が分かりましたら整理してください(気を付ける部分に注意と入れています)

Dim WordApp As Object
Dim WordDoc As Word.Document
Dim I, lRow As Long
Dim ExcelText
Dim strHead As String
Set WordApp = CreateObject("Word.Application")

'Excel
With ActiveSheet
lRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For I = 2 To lRow
ExcelText = ExcelText & .Cells(I, "A").Text & vbLf
Next I
End With

strHead = "【常勤役員会】"

'Word
With WordApp
.Visible = True
.Documents.Add
.Selection.TypeText ExcelText
Set WordDoc = .ActiveDocument
'2.転記したwordの余白を狭くしたいです。
WordDoc.PageSetup.LeftMargin = .MillimetersToPoints(10) '注意
'3.wordのヘッダー右側に【常勤役員会】、フッター中央には【1、2】とページ番号を入れたいです。
WordDoc.PageSetup.DifferentFirstPageHeaderFooter = True
With WordDoc.Sections(1).Headers(wdHeaderFooterPrimary)
.Range.InsertAfter (strHead)
.Range.Paragraphs.Alignment = wdAlignParagraphLeft
.PageNumbers.Add _
PageNumberAlignment:=wdAlignPageNumberCenter, FirstPage:=True
End With
'4.可能であれば、wordのファイル名は【“転記した日”_常勤役員会】としたいです。
WordDoc.SaveAs2 Filename:=ThisWorkbook.Path & "\" & Format(Date, "yyyy-mm-dd") & strHead
' WordDoc.Close savechanges:=False
' .Quit
End With
Set WordDoc = Nothing
Set WordApp = Nothing

End Sub

保存・閉じるはコメントアウトしています
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!