Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Word 2010 Macros not working in Word 2013

Posted on 2014-04-02
5
798 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
This article will shed light on the latest trends when it comes to your resume building needs. For far too long, the traditional CV format has monopolized the recruitment market.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

808 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