Solved

Word 2010 Macros not working in Word 2013

Posted on 2014-04-02
5
782 Views
Last Modified: 2014-04-06
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
Comment
Question by:Hosted247
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
smithandandersen earned 167 total points
ID: 39971885
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
 

Author Comment

by:Hosted247
ID: 39971915
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
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 333 total points
ID: 39972131
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
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 333 total points
ID: 39972172
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A high-level exploration of how our ever-increasing access to information has changed the way we do our jobs.
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now