Solved

Word 2010 Macros not working in Word 2013

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Lately there has been a variety of news related to U.S. employment.  Stories about worker productivity, automobile and airline unions, low employment and foreign laborers have frequented the news.  Each story has good and bad attributes we might arg…
As freelancing is becoming more and more common in the tech industry, certain obstacles are proving to be a challenge to those who are used to more traditional, structured employment. This article is meant to help identify such obstacles and offer a…
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

17 Experts available now in Live!

Get 1:1 Help Now