[ 初めての方へ | 一覧(最新更新順) | 全文検索 | 過去ログ ]
『【VBA】ファイル名変更』(トリちゃん)
こんにちは。
マクロと同じ階層のフォルダ内にある、pdfファイルの名前を変更したいですが、★の箇所で"ファイルが見つかりません"とエラーが出てしまいます。
★より上は上手くいっているようですが、どこが間違っているのか分かりません。
どなたかご教授ください。
よろしくお願いいたします。
Sub test()
Dim fso As Object
Dim pathTxt As String, filePath As String, newFileName As String
pathTxt = ThisWorkbook.Path & "\PDF"
If Dir(pathTxt & "\" & "*.pdf") <> "" Then
filePath = Dir(pathTxt & "\" & "*.pdf") newFileName = "Google通信.pdf" Set fso = CreateObject("Scripting.FileSystemObject") '★ fso.GetFile(filePath).Name = newFileName End If Set fso = Nothing End Sub
< 使用 Excel:Microsoft365、使用 OS:Windows11 >
(abc) 2023/02/27(月) 16:03:54
(abc) 2023/02/27(月) 16:20:06
fso.GetFile(filePath) ↑ フルパスで (´・ω・`) 2023/02/27(月) 16:21:56
>(´・ω・`)さん
ヒントをありがとうございます。
フルパスに書き換えましたら、無事動きました。
少し改良しまして↓のように致しました。
解決いたしました。
お二方、ありがとうございました。
Sub test()
Dim fso As Object
Dim pathTxt As String, filePath As String, newFileName As String
pathTxt = ThisWorkbook.Path & "\PDF"
If Dir(pathTxt & "\" & "*.pdf") <> "" Then
filePath = Dir(pathTxt & "\" & "*.pdf") If filePath <> "Google通信.pdf" Then newFileName = "Google通信.pdf" Set fso = CreateObject("Scripting.FileSystemObject") fso.GetFile(pathTxt & "\" & filePath).Name = newFileName End If End If Set fso = Nothing End Sub
(トリちゃん) 2023/02/27(月) 16:44:46
# 私だったら、原因がわかって納得するまで調べるけどね。まあ。人それぞれですけど。
(abc) 2023/02/27(月) 20:33:53
[ 一覧(最新更新順) ]
YukiWiki 1.6.7 Copyright (C) 2000,2001 by Hiroshi Yuki.
Modified by kazu.