Use VB to call VBA to insert Image Code in Word

Source: Internet
Author: User

Process name: wdout

Purpose: Use the defined template to automatically shape the template {????} And {photo} is replaced with the photo. If there is no photo, the replacement character is deleted.

Parameter: photofile -- the path string of the photo file, which is the complete absolute path. It does not determine whether the file exists. If the file does not exist, an error will occur.

Insert an image with only one sentence
Wdapp. selection. inlineshapes. addpicture filename: = _
Photofile, linktofile: = false, savewithdocument: = _
True
You can use the word macro to get the corresponding code.

Private function wdout (byval photofile as string)
''{Unit} {fee name} {capital amount} {appraisal unit} {operator} {date}

Dim wdapp as object, wddoc as object
Dim I as integer

If checkword = false then
Msgbox "No wordsoftware or software installation error! ", Vbexclamation
Exit Function
End if

If dotname = "" or not fileexist (dotname) then
Msgbox "the print template is not found and cannot be printed !! ", Vbexclamation
Exit Function
End if

Msgwinshow "Generating document from template ..."


''If not wddoc is nothing then
''On error resume next
''Wddoc. Close wddonotsavechanges
''Set wddoc = nothing
''Wdapp. Quit
''Set wdapp = nothing
''On error goto 0
''End if
''

Set wdapp = Createobject ("word. application ")
With wdapp
'. Visible = true
Set wddoc =. Documents. Add (dotname, false, 0, true) ''wdnewblankdocument = 0
End

For I = 0 to adors. Fields. Count-1
'With. content. Find

Select case adors. Fields (I). Name
Case "photo"
Wdapp. selection. Find. clearformatting
With wdapp. selection. Find
. Text = "{photo }"
. Replacement. Text = ""
. Forward = true
. Wrap = wdfindcontinue
. Format = false
. Matchcase = false
. Matchwholeword = false
. Matchbyte = true
. Matchwildcards = false
. Matchsoundslike = false
. Matchallwordforms = false
End

Wdapp. selection. Find. Execute
Wdapp. selection. Delete unit: = 1, Count: = 1' delete 1 = wdcharacter

If photofile> "" then
Wdapp. selection. inlineshapes. addpicture filename: = _
Photofile, linktofile: = false, savewithdocument: = _
True
Wdapp. selection. moveleft unit: = wdcharacter, Count: = 1
Wdapp. selection. moveright unit: = wdcharacter, Count: = 1, extend: = wdextend
Wdapp. selection. inlineshapes (1). Fill. Visible = 0 ''0 = msofalse
Wdapp. selection. inlineshapes (1). lockaspectratio =-1 ''-1 = msotrue
Wdapp. selection. inlineshapes (1). Height = 28*4.1
Wdapp. selection. inlineshapes (1). width = 28*2.8
End if
Case else

With wdapp. selection. Find
. Clearformatting
. Replacement. clearformatting

. Text = "{" & adors. Fields (I). Name &"}"
. Replacement. Text = adors. Fields (I). Value &""
. Forward = true
. Wrap = 1' 1 = wdfindcontinue
. Format = false
. Matchcase = false
. Matchwholeword = false
. Matchbyte = true
. Matchwildcards = false
. Matchsoundslike = false
. Matchallwordforms = false
. Execute Replace: = 2' 2 = wdreplaceall
End

End select
Next
Wdapp. Visible = true

Set wddoc = nothing
Set wdapp = nothing


Msgwinhide

End Function

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.