2017-04-27 1 views
0

WORD-VBAのAddPicture関数に問題があります。私はウェブを検索し、私が見つけた例を試しましたが、彼らはすべていくつかの点で失敗しました。私はいくつかの助けに感謝します。私は困惑している。私は何かを正しく定義していないと思う。私は以下のコードをコメントし、これが役立つことを願っています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 

答えて

0

は、ここに私のコードです。

Function FnImageInsert(strCompleteImagePath) 

    ' This code would be correct if you run Word from Excel 
    Dim objWord As Word.Application 
    Dim objDoc As Word.Document 
    Dim objSelection As Word.Object 
    ' If you run this code from Word you don't need to declare 
    ' the application and the objects don't need to specify Word 
' Dim objDoc As Document 
' Dim objSelection As Object 
    Dim Shp As Shape 

    ' omit this object if you run this code from within Word 
    Set objWord = CreateObject("Word.Application") 
    Set objDoc = objWord.Documents.Open("C:\test\testimage.docx") 

    ' it's the window you can make visible, not the application 
    ' the application is always invisible 
' objWord.Visible = True 

' Selection Class: This looks like a little code from VB (this is VBA) 

    ' I don't know if this actually works. I never saw this before. 
    ' in fact, there is one "Selection" in Word which is the currently 
    ' selected range in the ActiveDocument 
    Set objSelection = objWord.Selection 
    ' However, since you already have the 'Selection' object, why do you need 
    ' a copy of it in 'objSelection'? 

    ' the parentheses are superfluous here: 
    objSelection.TypeText (vbCrLf & "One Picture will be inserted here...." & vbCrLf) 

    ' Having a problem below... I get Object required error (424) 
    ' Of course: Shp is a Shape (see the Dim statement) 
    ' You are adding an InlineShape and assigning it to Shp, which cuses the error 
    Set Shp = ActiveDocument.InlineShapes.AddPicture(FileName:=strCompleteImagePath, SaveWithDocument:=True).ConvertToShape 
    ' If you wish to convert the Inlineshape to Shape, you should do this 
    ' by setting the WrapText property after creation. 
    ' But why not create a shape rightaway, if you don't want an InlineShape? 

    ' You can't "Close" the Selection object 
    ' In fact, there always is a selection while the document is open 
    ' and all you can do is move it or dete4rmine its size. 
' Close objSelection 
End Function 
+0

私はあなたのコードを使用し、それが割り当てられていないユーザー定義型を言って... –

+0

「Word.Applicationとして暗いobjWord」でコンパイル段階で失敗しました。 –

+0

私はExcel内からVBAを使用しています。元の問題はAddPicture関数です。残りはうまくいきます...その関数についてコメントしてください。私はShpがShapeであると言いました。MSDNによれば、inlineShapesはShapesのコレクションを返します...どうしてShpの割り当てを変更してエラーを修正するのですか? –