Search code examples
excelvbams-wordlocationtrusted

this method or property is not available because this command is not available for reading


I have a word document written in an excel VBA program and then read in a Word VBA program. The directory is below a trusted set of directories which has "subfolders of this location are also trusted" ticked. Trust is set up in both the Excel and the Word VBA programs via the Options/Trust Center/Trust Center Settings/Trusted locations. I have also subsequently explicitly nominated the directory as trusted without solving the problem.

In the word VBA program I attempt to insert and adjust a logo .png file into the file but get the above message, and the file is, indeed, opened in read only as I can verify by attempting to modify it. If I save it under a different name it still remains read only, even though both files are in the same trusted directory.

The subroutine trying to modify the file is:

Sub InsertLogo(oDocument As Document)
'
' InsertLogo Macro
'
'
    oDocument.Tables(1).Cell(1, 1).Select
    Selection.InlineShapes.AddPicture FileName:=Replace(sOutputFilePath & sColourFileName, Chr(34), "") _
        , LinkToFile:=False, SaveWithDocument:=True
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.InlineShapes(1).Height = CentimetersToPoints(1.35)
    Selection.InlineShapes(1).Width = CentimetersToPoints(2.38)
    Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
    
End Sub

The MoveLeft command is the one giving the error.

These programs were originally written in Office 2010 and worked successfully in previous years. but last year I upgraded to Office 2016.

Can anyone help me please?


Solution

  • For example, without selecting anything:

    Sub InsertLogo(oDocument As Document, sOutputFilePath As String, sColourFileName As String)
    With oDocument.Tables(1).Cell(1, 1).Range
      .InlineShapes.AddPicture FileName:=Replace(sOutputFilePath & sColourFileName, Chr(34), ""), _
            LinkToFile:=False, SaveWithDocument:=True, Range:=.Duplicate
      With .InlineShapes(1)
        .Height = CentimetersToPoints(1.35)
        .Width = CentimetersToPoints(2.38)
      End With
      .ParagraphFormat.Alignment = wdAlignParagraphCenter
    End With
    End Sub
    

    Note that I've also adjusted the code to pass the sOutputFilePath & sColourFileName variables to it also.