首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用string.copy的VBA无效限定符

使用string.copy的VBA无效限定符
EN

Stack Overflow用户
提问于 2015-07-09 15:54:48
回答 2查看 862关注 0票数 0

我正在编写一段代码,它循环遍历word文档的文本框。这些文本框包含图片和标题。到目前为止,我已经编写了一个从textbox获取标题的代码(我通过MsgBox caption进行了检查)。

我想复制标题,清除所有内容的文本框,然后粘贴旧的标题(因为我试图用更新的图片替换图片)。但是,我一直在caption.Copy中出错,并且不知道原因。它说标题是“无效限定符”。我在网上做了一些调查,但没有解决我的问题。

这是我发现的最相关的东西:Invalid Qualifier error in Visual Basic 6.0

总之,这是我的密码。任何帮助都将不胜感激!

代码语言:javascript
运行
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-09 16:11:07

caption变量是一个字符串,而Copy方法仅应用于Word对象模型中的对象。

将来自TextFrame的文本存储到caption变量中:

代码语言:javascript
运行
复制
caption = objShape.TextFrame.TextRange.Text

然后在你的循环中操纵它。

如果希望保留caption变量的值,则将该值赋值给另一个变量:

代码语言:javascript
运行
复制
Dim someOtherVariable As String
someOtherVariable  = caption
票数 0
EN

Stack Overflow用户

发布于 2015-07-09 16:10:34

Excel和Word VBA在嵌入的形状上可能有一些不同,但以下内容应该很容易被word采用:

代码语言:javascript
运行
复制
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

您可以通过将文本直接粘贴到即时窗口(或其他地方),再次检查剪贴板中的文本是否在剪贴板中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31322523

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档