AddPicture功能

时间:2017-04-27 02:04:10

标签: word-vba

我在WORD-VBA中遇到AddPicture函数问题。我在网上搜索并尝试了我发现的例子,但在某些方面它们都失败了。我将不胜感激。我很难过。我怀疑我没有正确定义。

这是我的代码:

Function FnImageInsert(strCompleteImagePath)

   Dim objWord

   Dim objDoc

   Dim objSelection

   Dim Shp As shape

   Set objWord = CreateObject("Word.Application")

   Set objDoc = objWord.Documents.Open("C:\test\testimage.docx")

   objWord.Visible = True

' Selection Class
   Set objSelection = objWord.Selection

   objSelection.TypeText (vbCrLf & "One Picture will be inserted here...." & vbCrLf)

'
'Having a problem below... I get Object required error (424)
'
   Set Shp = ActiveDocument.InlineShapes.AddPicture(FileName:=strCompleteImagePath, SaveWithDocument:=True).ConvertToShape
'
'
'

   Close objSelection

End Function

1 个答案:

答案 0 :(得分:0)

我已在下面评论了您的代码,希望这会有所帮助。

import re
patterns=[
    r'[^1368A]+', r'[^2479B]+', r'[^0358A]+', r'[^1469B]+',
    r'[^0257A]+', r'[^1368B]+', r'[^02479]+', r'[^1358A]+',
    r'[^2469B]+', r'[^0357A]+', r'[^1468B]+', r'[^02579]+'
]

def match_patterns(string):
    for pattern in patterns:
        yield from re.finditer(pattern, string)

def find_longest_match(string):
    match=max(match_patterns(string), key=lambda m: len(m.group(0)))
    if match:
        return match.start(), match.group(0)
    else:
        return None

print(find_longest_match('12A34B32A43')) # prints: (2, 'A34B3')