在職証明のマクロで
① 日付の位置を矢印の位置にずらしたい(画像赤丸の位置から矢印の先に位置変更)
② 本日の日付に”年”が2つあるので削除したいです。
今のプログラムはこちらなのですがどうしたら良いかご教示いただけませんでしょうか?
ここに実現したいことを箇条書きで書いてください。
在職証明の本日の日付を一番下の位置から規定の位置に記載できるようにしたい
その際大本の文字は削除したい
イメージ説明
該当のソースコード
Private Sub ToggleButton1_Click()
Dim wordApp As Object
Dim wordDoc As Object
' Wordアプリケーションを起動する
Set wordApp = CreateObject("Word.Application")
' 既存のWordファイルを開く
Set wordDoc = wordApp.Documents.Open("C:\Users\boy_a\Downloads\zaishoku.docx")
' Word文書の最後にカーソルを移動
wordApp.Selection.EndKey Unit:=wdStory
' テキストボックスの値を取得
Dim zipCode As String
zipCode = TextBox1.Value
Dim address As String
address = TextBox2.Value
Dim name As String
name = TextBox3.Value
Dim birth As String
birth = TextBox4.Value
Dim shokui As String
name = TextBox6.Value
' WordTableの操作を追加する
Dim WordTable As Object
Set WordTable = wordDoc.Tables(1)
With WordTable.Cell(1, 2).Range
.MoveEnd wdCharacter, -1
.InsertAfter Me.TextBox2.Value
End With
With WordTable.Cell(2, 2).Range
.MoveEnd wdCharacter, -1
.InsertAfter Me.TextBox3.Value
End With
With WordTable.Cell(3, 2).Range
.MoveEnd wdCharacter, -1
.InsertAfter Me.TextBox4.Value
End With
With WordTable.Cell(4, 2).Range
.MoveEnd wdCharacter, -1
.InsertAfter Me.TextBox6.Value
End With
Set WordTable = Nothing
' 本日の日付を取得
Dim currentDate As Date
currentDate = Date
' 令和の年号と年数を取得
Dim warekiYear As Integer
Dim warekiEra As String
warekiYear = Year(currentDate) - 2018 ' 令和元年が2019年なので、年数から2018を引く
If warekiYear = 1 Then
warekiEra = "令和元年"
Else
warekiEra = "令和" & warekiYear & "年"
End If
' 指定した位置に令和の日付を挿入(年の部分を削除)
Dim modifiedDate As String
modifiedDate = warekiEra & Replace(Format(currentDate, "年mm月dd日"), Year(currentDate) & "年", "")
' Wordドキュメントの一番下の行に移動
wordApp.Selection.EndKey 6
wordApp.Selection.MoveDown 5, 1
' 令和の日付を挿入
wordApp.Selection.TypeText modifiedDate
' Wordの文書を保存して閉じる
wordDoc.Save
wordDoc.Close
' Wordアプリケーションを終了する
wordApp.Quit
' オブジェクトを解放する
Set wordDoc = Nothing
Set wordApp = Nothing
End Sub
追記分
ご教示いただきました様に修正しましたがコンテンツコントロールに記載ができません。
下記のようなプログラムを書きエラーは出ていませんが記載がされません。
Private Sub ToggleButton3_Click()
Dim strTemplateDocumentPath As String
Dim dtToday As Date
strTemplateDocumentPath = "C:\Users\boy_a\Downloads\zaishoku.docx"
If Dir(strTemplateDocumentPath) = "" Then
MsgBox "在職証明書のWordテンプレート文書""" & strTemplateDocumentPath & """が見つかりません!", _
vbCritical, _
"ファイル参照エラー"
Exit Sub
End If
If MsgBox("在職証明書の作成を開始しますか?", _
vbQuestion + vbYesNo + vbDefaultButton2, _
"実行確認") = vbNo Then
Exit Sub
End If
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
On Error Resume Next
Set WordApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Err.Clear
On Error GoTo 0
Set WordApp = CreateObject("Word.Application")
End If
On Error GoTo 0
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Open(strTemplateDocumentPath)
dtToday = Date
Dim WordContentControl As Word.ContentControl
For Each WordContentControl In WordDoc.ContentControls
Select Case WordContentControl.Title
Case "発行年月日"
WordContentControl.Range.Text = Format(Date, "yyyy年MM月dd日")
Case Else
'何もしない
End Select
Next
End Sub

回答1件
あなたの回答
tips
プレビュー