使用string.copy

时间:2015-07-09 15:54:49

标签: vba

我正在编写一个循环遍历word文档文本框的代码。这些文本框包含图片和标题。到目前为止,我已经编写了一个代码来获取文本框中的标题(我通过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

2 个答案:

答案 0 :(得分:0)

在嵌入式形状中,Excel和Word VBA之间可能会有一些不同,但以下内容应该很容易采用单词:

modelBuilder.ForSqlServer().UseIdentity()

您可以通过将文本直接粘贴到即时窗口(或任何地方)来仔细检查文本是否在剪贴板中。

答案 1 :(得分:0)

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

您将TextFrame中的文字存储到caption变量中:

caption = objShape.TextFrame.TextRange.Text

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

如果要保留caption变量的值,请将值分配给另一个变量:

Dim someOtherVariable As String
someOtherVariable  = caption