- 上位レベルのしおり(doc.bookmarkRoot.children)の名前を replace(RegExp, newSubstr) で置き換える
var str = "(" +
(() => {
const doc = this;
const bkms = doc.bookmarkRoot.children;
if (bkms === null) {
app.alert({cMsg: "しおり がありません"});
return;
}
const flags = "g";
const pattern = app.response({
cQuestion: "RegExp(pattern, \"" + flags + "\")",
cTitle: "上位レベルのしおり の名前を置換",
cDefault: "^\\s+|\\s+$",
bPassword: false,
cLabel: "pattern"
});
if (pattern === null) {
app.alert({cMsg: "patternの入力がキャンセルされました"});
return;
}
const regex = new RegExp(pattern, flags);
const newSubstr = app.response({
cQuestion: ".replace(RegExp, newSubstr)",
cTitle: "上位レベルのしおり の名前を置換",
cDefault: "",
bPassword: false,
cLabel: "newSubstr"
});
if (newSubstr === null) {
app.alert({cMsg: "newSubstrの入力がキャンセルされました"});
return;
}
for (var bkm of bkms) {
bkm.name = bkm.name.replace(regex, newSubstr);
}
}).toString() + ")();";
app.addSubMenu({
cName: "MyBookmarkEdit",
cUser: "しおりを調整",
cParent: "Edit",
nPos: 0
});
app.addMenuItem({
cName: "MyBookmarkNameReplace",
cUser: "上位レベルのしおり の名前を置換",
cParent: "MyBookmarkEdit",
nPos: 0,
cExec: str,
cEnable: "event.rc = (event.target != null)",
cMarked: "event.rc = false",
bPrepend: false
});
IAC
- 上位レベルのしおりの名前を新しいワークシートに出力する
- セル選択範囲の文字列を上位レベルのしおりの名前に設定する
- Acrobatがバックグラウンドプロセスとして残るので練習以外で使用しない
Option Explicit
#Const DEBUG_ = False
Public Sub 上位レベルのしおりの名前を取得()
#If DEBUG_ Then
Dim avApp As Acrobat.AcroApp
Dim avDoc As Acrobat.AcroAVDoc
Dim pdDoc As Acrobat.AcroPDDoc
#Else
Dim avApp As Object
Dim avDoc As Object
Dim pdDoc As Object
#End If
Dim jso As Object
Dim bkmR As Object
Dim bkms() As Variant
Dim bkmNamesAndPages() As Variant
Dim i As Long
Dim pageNumTemp As Long
Dim xlBookNew As Excel.Workbook
Dim xlSheetNew As Excel.Worksheet
Dim xlRng As Excel.Range
Dim xlRngUL As Excel.Range, xlRngLR As Excel.Range
On Error GoTo ErrorHandler
Set avApp = VBA.Interaction.CreateObject(Class:="AcroExch.App")
Set avDoc = avApp.GetActiveDoc()
Set pdDoc = avDoc.GetPDDoc()
Set jso = pdDoc.GetJSObject()
Set bkmR = jso.bookmarkRoot
bkms = bkmR.Children
ReDim bkmNamesAndPages(1 To UBound(bkms) + 1, 1 To 2)
pageNumTemp = jso.pageNum
For i = 0 To UBound(bkms) Step 1
bkmNamesAndPages(i + 1, 1) = bkms(i).Name
jso.pageNum = 0
bkms(i).Execute
bkmNamesAndPages(i + 1, 2) = jso.pageNum + 1
Next i
jso.pageNum = pageNumTemp
For i = 0 To UBound(bkms) Step 1
Set bkms(i) = Nothing
Next i
Set xlBookNew = Excel.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet)
Set xlSheetNew = xlBookNew.ActiveSheet
xlSheetNew.Name = "上位レベルのしおり"
xlSheetNew.Cells(1, 1).Value = "上位レベルのしおりの名前"
xlSheetNew.Cells(1, 2).Value = "ページ番号"
Set xlRngUL = xlSheetNew.Cells(1 + LBound(bkmNamesAndPages, 1), 1)
Set xlRngLR = xlSheetNew.Cells(1 + UBound(bkmNamesAndPages, 1), 2)
Set xlRng = xlSheetNew.Range(xlRngUL, xlRngLR)
xlRng = bkmNamesAndPages
ErrorHandler:
Set bkmR = Nothing
Set jso = Nothing
Set pdDoc = Nothing
Set avDoc = Nothing
Set avApp = Nothing
If Not VBA.Information.Err.Number = 0 Then
Debug.Print _
VBA.Information.Err.Number & ": " & _
VBA.Conversion.Error(VBA.Information.Err.Number)
End If
End Sub
Public Sub 上位レベルのしおりに名前を設定()
#If DEBUG_ Then
Dim avApp As Acrobat.AcroApp
Dim avDoc As Acrobat.AcroAVDoc
Dim pdDoc As Acrobat.AcroPDDoc
#Else
Dim avApp As Object
Dim avDoc As Object
Dim pdDoc As Object
#End If
Dim jso As Object
Dim bkmR As Object
Dim bkms() As Variant
Dim i As Long
Dim xlRng As Excel.Range
On Error GoTo ErrorHandler
Set avApp = VBA.Interaction.CreateObject(Class:="AcroExch.App")
Set avDoc = avApp.GetActiveDoc()
Set pdDoc = avDoc.GetPDDoc()
Set jso = pdDoc.GetJSObject()
Set bkmR = jso.bookmarkRoot
bkms = bkmR.Children
Set xlRng = Excel.Application.ActiveWindow.RangeSelection
For i = 0 To UBound(bkms) Step 1
bkms(i).Name = Excel.WorksheetFunction.Clean(xlRng(i + 1, 1).Value)
Next i
For i = 0 To UBound(bkms) Step 1
Set bkms(i) = Nothing
Next i
ErrorHandler:
Set bkmR = Nothing
Set jso = Nothing
Set pdDoc = Nothing
Set avDoc = Nothing
Set avApp = Nothing
If Not VBA.Information.Err.Number = 0 Then
Debug.Print _
VBA.Information.Err.Number & ": " & _
VBA.Conversion.Error(VBA.Information.Err.Number)
End If
End Sub