我正在编写一段代码,它循环遍历word文档的文本框。这些文本框包含图片和标题。到目前为止,我已经编写了一个从textbox获取标题的代码(我通过MsgBox caption进行了检查)。
我想复制标题,清除所有内容的文本框,然后粘贴旧的标题(因为我试图用更新的图片替换图片)。但是,我一直在caption.Copy中出错,并且不知道原因。它说标题是“无效限定符”。我在网上做了一些调查,但没有解决我的问题。
这是我发现的最相关的东西:Invalid Qualifier error in Visual Basic 6.0
总之,这是我的密码。任何帮助都将不胜感激!
Sub ReplaceImages()
Dim str As String
Dim captionTag As String
Dim imageTag As String
'Dim objShape As Variant Type Mismatch?
Dim fileName As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
'Select directory to match .PNG to figure in document
Set SelectFolder = Application.FileDialog(msoFileDialogFolderPicker)
With SelectFolder
.Title = "Select Directory"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo ResetSettings
sPath = .SelectedItems(1) & "\"
End With
sFile = Dir(sPath & "*png")
Do While sFile <> ""
fileName = sFile
MsgBox fileName
imageTag = BetweenParentheses(fileName)
For Each objShape In ActiveDocument.Shapes
If objShape.Type = msoTextBox Then
Set shapePicture = objShape
str = objShape.TextFrame.TextRange.Text
If InStr(str, "(") > 0 Then
captionTag = BetweenParentheses(objShape.TextFrame.TextRange)
If captionTag = imageTag Then
If InStr(str, "Figure") > 0 Then
Dim firstTerm As String
Dim secondTerm As String
Dim caption As String
firstTerm = "F"
secondTerm = ")"
Dim startPos As Long
Dim stopPos As Long
Dim nextPosition As Long
nextPosition = 1
caption = objShape.TextFrame.TextRange.Text
Do Until nextPosition = 0
startPos = InStr(nextPosition, caption, firstTerm, vbTextCompare) - 1
stopPos = InStr(startPos, caption, secondTerm, vbTextCompare) + 1
caption = Mid$(caption, startPos + Len(firstTerm), stopPos - startPos - Len(firstTerm))
nextPosition = InStr(stopPos, caption, firstTerm, vbTextCompare)
Loop
caption.Copy 'This is where the error is
End If
End If
End If
End If
Next objShape
sFile = Dir
Loop
ResetSettings:
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False发布于 2015-07-09 16:11:07
caption变量是一个字符串,而Copy方法仅应用于Word对象模型中的对象。
将来自TextFrame的文本存储到caption变量中:
caption = objShape.TextFrame.TextRange.Text然后在你的循环中操纵它。
如果希望保留caption变量的值,则将该值赋值给另一个变量:
Dim someOtherVariable As String
someOtherVariable = caption发布于 2015-07-09 16:10:34
Excel和Word VBA在嵌入的形状上可能有一些不同,但以下内容应该很容易被word采用:
Sub test()
Dim shp As Shape, s As String
Set shp = ActiveSheet.Shapes(1)
s = shp.TextFrame2.TextRange.Text ' this is a string which doesn't have a Copy method
Debug.Print s
'but:
shp.TextFrame2.TextRange.Copy 'copies to clipboard!
End Sub您可以通过将文本直接粘贴到即时窗口(或其他地方),再次检查剪贴板中的文本是否在剪贴板中。
https://stackoverflow.com/questions/31322523
复制相似问题