Arrays 无法获取从文本文件读取到word的有效文件路径。双引号不起作用。(具有InlineShapes的运行时错误“5152”)
我需要替换word中的108个图像。我编写了VBA代码,以便对于文档中读取的每个内联图像,该图像将被一个新图像替换。新映像由每个元素中都有文件路径的数组指定。数组来自文本文件 出于某种原因,如果我有变量strPath as,我的代码将无法工作Arrays 无法获取从文本文件读取到word的有效文件路径。双引号不起作用。(具有InlineShapes的运行时错误“5152”),arrays,vba,ms-word,Arrays,Vba,Ms Word,我需要替换word中的108个图像。我编写了VBA代码,以便对于文档中读取的每个内联图像,该图像将被一个新图像替换。新映像由每个元素中都有文件路径的数组指定。数组来自文本文件 出于某种原因,如果我有变量strPath as,我的代码将无法工作 strPath = dataArray(i) 或 如果我输入,什么才有效 dataArray(0) = "C:\IMGS\G.2.1\NZ_DWH_v_SIMAP_AR1_biovalbox1_1100-1400m_Apr20-May26.png" 文
strPath = dataArray(i)
或
如果我输入,什么才有效
dataArray(0) = "C:\IMGS\G.2.1\NZ_DWH_v_SIMAP_AR1_biovalbox1_1100-1400m_Apr20-May26.png"
文本文件中的路径为
C:\IMGS\G.2.1\NZ_DWH_v_SIMAP_AR1_biovalbox1100-1400m_Apr20-May26.png
我在文本文件中有108行,每行用于需要替换的图像。
我已经在一个消息框中显示了路径,它看起来像上面的,所以我不确定为什么我不能从数组中获取文件路径。有人能帮我吗
'1-loop thru all figs
'2-bring up box to select figure
'3-add figure
Dim intChoice As Integer
Dim strPath As String
Dim objPic As InlineShape
Dim intCount As Integer
'import text
Dim dataArray() As String
Dim i As Integer
Dim g As Integer
strFileName = "C:\Users\cturner\Desktop\filesimgs_order.txt"
Open strFileName For Input As #1
dataArray = Split(Input$(LOF(1), #1), vbLf)
Close #1
g = 0
intCount = ActiveDocument.InlineShapes.Count
'loop through inline shapes
For i = 0 To intCount
strPath = Chr(34) & dataArray(i) & Chr(34)
MsgBox (TypeName(strPath))
g = g + 1
'check if valid filepath
'Debug.Print FileExists(strPath)
MsgBox strPath
'check if the current shape is an picture
If ActiveDocument.InlineShapes.Item(g).Type = wdInlineShapePicture Then
Set objPic = ActiveDocument.InlineShapes.Item(g)
objPic.Select
'insert the image
Selection.InlineShapes.AddPicture FileName:= strPath, _
LinkToFile:=False, SaveWithDocument:=True
End If
Next i
End Sub
令人惊讶的是,我忘了修剪车厢返回vbCr。在我把它拿出来之后,代码工作得很好,我可以替换掉大量的图片 第一部分。我检查了我的字符串是否有回车符。主要的问题是我没有清理我的字符串,因此将它放入一个函数中,使得vba抛出错误消息no bueno。如果有,UBound应该=1
For i = 0 To 2
strPath = dataArray(i)
checkcr = Split(strPath, vbCr)
firstIndex = LBound(checkcr)
lastIndex = UBound(checkcr)
MsgBox (firstIndex)
MsgBox (lastIndex)
MsgBox checkcr(lastIndex)
newstrPath = Replace(strPath, vbCr, "")
MsgBox newstrPath
Next i
第二部分。修正代码+奖金。Bouns:一些代码,用于检查文件是否存在
修正:
更新代码:
Sub replacefigs()
' replacefigs Macro
' To replace old figures with new figures (updated disclosure)
'0-get full file paths from textfile
'1-loop thru all figs
'2-bring up box to select figure
'3-add figure
Dim intChoice As Integer
Dim strPath As String
Dim objPic As InlineShape
Dim intCount As Integer
'import text
Dim dataArray() As String
Dim i As Integer
Dim g As Integer
strFileName = "C:\Users\cturner\Desktop\filesimgs_order.txt"
Open strFileName For Input As #1
dataArray = Split(Input$(LOF(1), #1), vbLf)
Close #1
g = 0
intCount = ActiveDocument.InlineShapes.Count
'loop through inline shapes
For i = 0 To intCount
strPath = dataArray(i)
'Get rid of carriage returns
newstrPath = Replace(strPath, vbCr, "")
g = g + 1
'to check if file exists
'Debug.Print FileExists(newstrPath)
'check if the current shape is an picture
If ActiveDocument.InlineShapes.Item(g).Type = _
wdInlineShapePicture Then
Set objPic = ActiveDocument.InlineShapes.Item(g)
objPic.Select
'insert the image
Selection.InlineShapes.AddPicture FileName:= _
newstrPath, LinkToFile:=False, _
SaveWithDocument:=True
End If
Next i
End Sub
通常,文本文件具有回车符和换行符,因此字符串变量中可能仍然包含vbCR字符?也许你可以循环使用你的变量并测试每个字符的ASCII值?@Chipsleten,你这个了不起的人,你,我想我有vbCr,我没想过要找它!我将尝试用替换它,看看会发生什么。对于那些想知道如何检查它的人,我使用vbCr分割数组。如果没有字符匹配,LBoundsplitarray和UBoundsplitarray应等于零。否则你会得到像UBound这样的东西=1@ChipsLetten,这就是问题所在。我不知道如何提交注释作为解决方案,所以我只是用修复程序更新了代码。谢谢你的帮助!
newstrPath = Replace(strPath, vbCr, "")
Sub replacefigs()
' replacefigs Macro
' To replace old figures with new figures (updated disclosure)
'0-get full file paths from textfile
'1-loop thru all figs
'2-bring up box to select figure
'3-add figure
Dim intChoice As Integer
Dim strPath As String
Dim objPic As InlineShape
Dim intCount As Integer
'import text
Dim dataArray() As String
Dim i As Integer
Dim g As Integer
strFileName = "C:\Users\cturner\Desktop\filesimgs_order.txt"
Open strFileName For Input As #1
dataArray = Split(Input$(LOF(1), #1), vbLf)
Close #1
g = 0
intCount = ActiveDocument.InlineShapes.Count
'loop through inline shapes
For i = 0 To intCount
strPath = dataArray(i)
'Get rid of carriage returns
newstrPath = Replace(strPath, vbCr, "")
g = g + 1
'to check if file exists
'Debug.Print FileExists(newstrPath)
'check if the current shape is an picture
If ActiveDocument.InlineShapes.Item(g).Type = _
wdInlineShapePicture Then
Set objPic = ActiveDocument.InlineShapes.Item(g)
objPic.Select
'insert the image
Selection.InlineShapes.AddPicture FileName:= _
newstrPath, LinkToFile:=False, _
SaveWithDocument:=True
End If
Next i
End Sub