WordVBAの初心者です。
宜しくお願い致します。
RTFファイルのフィールドコードにページ数があります。
それを固定し、RTFファイル複数を結合した時
フィールドコードが更新されないようにしたいです。
下記のコードを試し、上書き保存をしても変更になりませんでした。
なので、結合する際ページ数が振り直されてしまいました。。
■ActiveDocument.Fields.Unlink
■ActiveDocument.Fields.Locked = True
やりたいことは
[Ctrl]+[Shift]+[F9]キーを押した時の処理をVBAで行いたいです。
参考URL
https://www.moug.net/tech/woopr/0110040.html
Sub RTFファイル結合()
Dim app As Excel.Application
Dim doc_name As String
pathname = "C:\Users\9A150\Desktop\RTF格納"
'Excelを起動する Set app = CreateObject("Excel.Application") 'abc.xlsを開く Dim book As Excel.Workbook Set book = app.Workbooks.Open("C:\Users\9A150\Desktop\RTF格納\bbb.xlsx") app.Visible = True 'ファイル名一覧シートを指定 Dim sht As Object Set sht = book.Worksheets("Sheet1") 'A列を基準に昇順で並べ替えます sht.Range("A2:B10").Sort Key1:=sht.Range("A1"), Order1:=xlAscending, Header:=xlYes For r = 2 To sht.Cells(sht.Rows.Count, "A").End(xlUp).Row '行を2から最終行まで If sht.Cells(r, "A").Value <> "" Then 'A列注目行の値が""でなければ 'B列からファイル名を取得する doc_name = sht.Cells(r, "B").Value Documents.Open FileName:=pathname & doc_name Selection.Fields.Unlink 'Selection.Fields.Locked = True 'SetAttr ActiveDocument, vbNormal ActiveDocument.SaveAs2 FileName:=pathname & doc_name, _ FileFormat:=wdFormatRTF ActiveDocument.Close ChDir ThisDocument.Path 'Wordファイルと同じフォルダ
Dim doc As Document
For Each doc In Documents
doc.PageSetup.Orientation = wdOrientLandscape
Next doc
With Selection '.TypeText "ファイル名 = " & doc_name & vbCr '.InsertBreak wdPageBreak .InsertFile doc_name .InsertBreak wdSectionBreakNextPage End With
End If
Next
'RTFにて保存
ActiveDocument.SaveAs2 FileName:=pathname & "Text.rtf", _
FileFormat:=wdFormatRTF
ActiveDocument.Close
book.Close
Call PDFに変換
End Sub
あなたの回答
tips
プレビュー