0
votes

I am able to add multiple images to a word document using VBA and at the same time I am not getting to add multiple cross references using VBA.

Sub checking() Dim strFolderPath strFolderPath = "C:\images" Dim objWord

Dim objDoc

Dim objSelection

Dim objShapes

Dim objFSO

Dim objFolder

Set objWord = CreateObject("Word.Application")

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFSO.GetFolder(strFolderPath)

Set objDoc = objWord.Documents.Open("D:\myfile.docx")

objWord.Visible = True

Set objSelection = objWord.Selection

For Each Img In objFolder.Files

ImgPath = Img.Path

   objSelection.InlineShapes.AddPicture (ImgPath       

objSelection.insertbreak

Next

End Sub

1
For below code added images I need add the cross references for each images, if any body please suggest me. Sub add_images() FnInsertMultipleImages ("d:\Data\Images") End Subsrihari
function definitin is : Function FnInsertMultipleImages(strFolderPath) Dim objWord Dim objDoc Dim objSelection Dim objShapes Dim objFSO Dim objFolder Set objWord = CreateObject("Word.Application") Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFolder = objFSO.GetFolder(strFolderPath) Set objDoc = objWord.Documents.Open("D:\document.docx") objWord.Visible = True Set objSelection = objWord.Selection For Each Img In objFolder.Files ImgPath = Img.Path objSelection.InlineShapes.AddPicture (ImgPath) objSelection.insertbreak Next End Functionsrihari
Please edit your question and paste the code there.Siddharth Rout
I provided an answer to the 3rd question at stackoverflow.com/a/54794824/10908769 and suggest to delete this question here.Asger

1 Answers

0
votes

I am going to show you an example. You will have to amend it to suit your need. I am going to insert 2 images and create a Cross Reference. I have commented the code so you should not have a problem understanding it.

Logic:

  1. Add image
  2. Add caption to image
  3. Create the Cross Reference
  4. Insert Page Break?
  5. Repeat the above step

Code:

Sub Sample()
    Dim shp As InlineShape
    Dim n As Long

    '
    '~~> Insert Image 1
    '
    Set shp = Selection.InlineShapes.AddPicture(FileName:="C:\ImageA.Png", _
                                                LinkToFile:=False, _
                                                SaveWithDocument:=True)

    '~~> Adding a caption
    n = 1
    CaptionLabels.Add Name:="MyImage" & n

    shp.Select
    Selection.InsertCaption Label:="MyImage" & n, TitleAutoText:="", Title:="", _
    Position:=wdCaptionPositionBelow, ExcludeLabel:=0

    Selection.InsertBreak


    '
    '~~> Insert Image 2
    '
    Set shp = Selection.InlineShapes.AddPicture(FileName:="C:\ImageB.Png", _
                                                LinkToFile:=False, _
                                                SaveWithDocument:=True)

    '~~> Adding a caption
    n = n + 1
    CaptionLabels.Add Name:="MyImage" & n

    shp.Select
    Selection.InsertCaption Label:="MyImage" & n, TitleAutoText:="", Title:="", _
    Position:=wdCaptionPositionBelow, ExcludeLabel:=0

    Selection.InsertBreak

    '~~> Creating cross reference
    For n = 1 To 2
        Selection.InsertCrossReference ReferenceType:="MyImage" & n, ReferenceKind:= _
        wdEntireCaption, ReferenceItem:=1, InsertAsHyperlink:=True, _
        IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "
    Next n
End Sub