0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

WordVBA 現在の位置にフィールドコードで全角和暦で日付を挿入あるいは曜日付きで挿入 Insert Field Code byte Char And Jp Era with Weekday

Posted at
Sub InsertFldWgggeMd()
'For Word VBA
'ドキュメントの現在のカーソルの位置に全角、和暦で日付を挿入します。フィールドコードを使います。
Dim DT As Date
On Error Resume Next
DT = CDate(InputBox("全角和暦ggge年M月d日形式で日付を挿入します", "フィールドコードで日付を挿入します", Date))
If Err.Number <> 0 Then Debug.Print Err.Number, Err.Description: Err.Clear: Exit Sub

    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
        "MERGEFIELD """ & Format(DT, "yyyy\/M\/d") & """ \@""ggge年M月d日""  \* DBCHAR ", _
        PreserveFormatting:=True
 ActiveDocument.Activate
End Sub

全角和暦日付曜日

平成28年5月1日(日)のように和暦、全角、日付ありで現在の位置に挿入します。
かっこも全角になります。

Sub InsertFldWgggeMd_aaa_()
'For Word VBA
'ドキュメントの現在のカーソルの位置に全角、和暦で日付を挿入します、フィールドコードを使います。
Dim DT As Date
On Error Resume Next
DT = CDate(InputBox("全角和暦曜日つきで日付を挿入します", "フィールドコードで日付を挿入します", Date))
If Err.Number <> 0 Then Debug.Print Err.Number, Err.Description: Err.Clear: Exit Sub

    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
        "MERGEFIELD """ & Format(DT, "yyyy\/M\/dd") & """ \@""ggge年M月d日(aaa)""  \* DBCHAR ", _
        PreserveFormatting:=True
 ActiveDocument.Activate
End Sub

かっこは半角

フィールドコードを2つ作ります。VBAを使うと可能です。

Sub InsertFldWgggeMdaaaWithHalpParenthes()
'For Word VBA
'ドキュメントの現在のカーソルの位置に全角、和暦で日付を挿入します、フィールドコードを使います。かっこParenthesは半角です
Dim DT As Date
DT = CDate(InputBox("全角和暦曜日つきで日付を挿入します。曜日のかっこは半角です", "フィールドコードで日付を挿入します", Date))
If Err.Number <> 0 Then Debug.Print Err.Number, Err.Description: Err.Clear: Exit Sub

    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
        "MERGEFIELD """ & Format(DT, "yyyy\/M\/dd") & """ \@""ggge年M月d日""  \* DBCHAR ", _
        PreserveFormatting:=True
    Selection.TypeText Text:="("
      Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
        "MERGEFIELD """ & Format(DT, "yyyy\/M\/dd") & """ \@""aaa""  \* DBCHAR ", _
        PreserveFormatting:=True
    Selection.TypeText Text:=")"
    
End Sub

フィールドコードの日付はフィールドコードを表示させるか非表示を切り替えることで西暦と和暦で検索できる

フィールドコードで日付を挿入するのはVBAを使っても楽ではありませんが、強力な利点があります。それはフィールドコードを表示させていると西暦で、フィールドコードを表示させないと和暦で検索します。
今、すべてのフィールドコードが結果を表示しているとすると、次のVBAですべてコード表示になります。またフィールドコードのコードが表示されていれば、今度はコードが隠され、結果だけが表示されます。
これにより日付は西暦と和暦の両方でチェックすることができます。

Sub ToggleShowCodeOfFieldCodesInDocument()
' For Word VBA
' Documentのすべてのフィールドコードについてコード表示させるかどうかを反転させる
Dim wDoc As Document: Set wDoc = ThisDocument
Dim fc As Field, i As Long
If wDoc.Fields.Count > 0 Then
For i = wDoc.Fields.Count To 1 Step -1
Set fc = wDoc.Fields(i)
fc.ShowCodes = Not fc.ShowCodes
Next
End If
End Sub

余談

なおフィールドコード内の日付は全角2018/2/8 でも成功します。どうもCDateのような効果を持っているようです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?