?
Solved

Word 2010 Macros not working in Word 2013

Posted on 2014-04-02
5
Medium Priority
?
851 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
[X]
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
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
smithandandersen earned 501 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 999 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 999 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
When asking a question in a forum or creating documentation, screenshots are vital tools that can convey a lot more information and save you and your reader a lot of time
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

765 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