• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 899
  • Last Modified:

Word 2010 Macros not working in Word 2013

Hi

Not normally something I would deal with but a client has just upgraded their laptop and upgrade to Word 2013.  They have a macro that inserts photos into a document, they click the macro button, select all the photos are they are automatically added to the document in 2 columns nicely laid out.  This macro has stopped working in Word 2013.  The macro is below, it fails with a syntax error on 'sNoDoc = MsgBox (" " & _'
Sub HCIInsertImages()
Dim fd As FileDialog
Dim oTable As Table
Dim iRow As Integer
Dim iCol As Integer
Dim oCell As Range
Dim i As Long
Dim sNoDoc As String
Dim picName As String
Dim scaleFactor As Long
Dim max_height As Single
'define resize constraints
max_height = 155
 
 
If Documents.Count = 0 Then
    sNoDoc = MsgBox(" " & _
    "No document open!" & vbCr & vbCr & _
    "Do you wish to create a new document to hold the images?", _
    vbYesNo, "Insert Images")
    If sNoDoc = vbYes Then
        Documents.Add
    Else
        Exit Sub
    End If
End If
 
'add a 1 row 2 column table to take the images
Set oTable = Selection.Tables.Add(Selection.Range, 1, 2)
'+++++++++++++++++++++++++++++++++++++++++++++
'oTable.AutoFitBehavior (wdAutoFitFixed)
oTable.Rows.Height = CentimetersToPoints(4)
oTable.Range.Cells.VerticalAlignment = wdCellAlignVerticalCenter
'++++++++++++++++++++++++++++++++++++++++++++++
 
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
    .Title = "Select image files and click OK"
    .Filters.Add "Images", "*.gif; *.jpg; *.jpeg; *.bmp; *.tif; *.png"
    .FilterIndex = 2
    If .Show = -1 Then
        For i = 1 To .SelectedItems.Count
            'find col,row #s
            If i Mod 2 = 0 Then 'even number right column
                iRow = i / 2
                iCol = 2
            Else 'odd number left column new row
                iRow = (i + 1) / 2
                iCol = 1
            End If
 
            'get filename
            picName = Right(.SelectedItems(1), Len(.SelectedItems(1)) - InStrRev(.SelectedItems(1), "\"))
            'remove extension from filename ****
            picName = Left(picName, InStrRev(picName, ".") - 1)
 
            'select cell
            Set oCell = oTable.Cell(iRow, iCol).Range
 
           'insert image
            oCell.InlineShapes.AddPicture FileName:= _
                .SelectedItems(i), LinkToFile:=False, _
                SaveWithDocument:=True, Range:=oCell
 
            'resize image
            If oCell.InlineShapes(1).Height > max_height Then
                scale_factor = oCell.InlineShapes(1).ScaleHeight * (max_height / oCell.InlineShapes(1).Height)
                oCell.InlineShapes(1).ScaleHeight = scale_factor
                'oCell.InlineShapes(1).ScaleWidth = scale_factor
            End If
 
            'center content
            oCell.ParagraphFormat.Alignment = wdAlignParagraphCenter
 
            'insert caption below image
            'oCell.InlineShapes(1).Range.InsertCaption Label:="Figure", TitleAutoText:="", _
             '   Title:=": " & picName, Position:=wdCaptionPositionBelow, ExcludeLabel:=True
 
            If i < .SelectedItems.Count And i Mod 2 = 0 Then 'add another row, more to go
                oTable.Rows.Add
            End If
        Next i
    End If
End With
 
Set fd = Nothing
End Sub

Open in new window


Can anyone help?

Thanks
Adam
0
Hosted247
Asked:
Hosted247
  • 2
3 Solutions
 
smithandandersenCommented:
Check the options in word under trust center settings. Under macros settings make sure enable all macros is set and trust access to vba project object model
0
 
Hosted247Author Commented:
Hi

Have done that but still get a Compile Error: Syntax error it then opens the VB console and the sNoDoc = MsgBox (" " & _ line is high lighted.

Thanks
0
 
GrahamSkanCommented:
The code syntax looks OK to me.

As a long shot, it might be worth looking at the References (Tools/Reference in the VBA IDE) to check that none are flagged as MISSING.
0
 
GrahamSkanCommented:
You could also try changing lines 16 to 26 with this code. It avoids trying to set the string variable sNoDoc to an integer value. It also tweaks the line in question to see if that makes any difference.
If Documents.Count = 0 Then
    If MsgBox(" No document open!" & vbCr & vbCr & _
    "Do you wish to create a new document to hold the images?", _
    vbYesNo, "Insert Images") = vbYes Then
        Documents.Add
    Else
        Exit Sub
    End If
End If

Open in new window

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now