Questo codice "Selection.CreateTextbox" funziona in un file di versione di versione 93-2001, ma non in un file di parola 2010

Ho una routine di Excel che trasferisce i dati dal file di Excel in un documento di Word. Il programma funziona quando i file vengono salvati come Office 97-2003, ma si bloccano quando aggiorgo i file a Office 2010. Il problema si riferisce alla parte della programmazione che mette una casella di text intorno alle foto e aggiunge una didascalia. Questa parte della programmazione viene chiamata da Excel ma la subroutine è in Word. La subroutine "ResizePic" che ridimensiona le immagini funziona, ma la subroutine "AddPictureBox" non lo fa. Può qualcuno fornire me codice che funziona in Office 2010 e 2013. Non mi import se functionrà ancora nella versione precedente. Tieni presente che non ho originariamente scritto questo codice e non sono un utente avanzato. Sono mostrate solo le parti rilevanti della subroutine CreateDocumment.

sub CreateReport() Set wdApp = GetObject("", "Word.Application") wdApp.Documents.Open FileName:=strDefaultPath & "\tempReport.doc", ReadOnly:=True Excel.Sheets("Export").Activate 'add line items from Excel i = 1 Do Until IsEmpty(Excel.Sheets("Export").Cells(i, 5)) wdApp.Selection.Goto What:=-1, Name:="WorkItemList" strItemName = Excel.Sheets("Export").Range("b" & i).Value wdApp.Selection.Style = wdApp.activedocument.Styles("Heading 3") wdApp.Selection.TypeText Text:=strItemName wdApp.Selection.InlineShapes.AddPicture FileName:=Excel.Sheets("Export").Range("a" & i).Text, LinkToFile:=False, SaveWithDocument:=True If Excel.Sheets("Export").Range("a" & i).Value <> "" Then wdApp.Selection.InlineShapes.AddPicture FileName:=Excel.Sheets("Export").Range("a" & i).Text, LinkToFile:=False, SaveWithDocument:=True End If wdApp.Selection.TypeParagraph If Excel.Sheets("Export").Range("c" & i).Value > 1 Then strItemName = Excel.Sheets("Export").Range("c" & i).Value wdApp.Selection.Style = wdApp.activedocument.Styles("Body Text") wdApp.Selection.TypeText Text:=strItemName wdApp.Selection.TypeParagraph End If i = i + 1 Loop wdApp.activedocument.ResizePic wdApp.activedocument.AddPictureBox 

Le seguenti sottoprogrammi sono nel file di Word

  • aggiungere il valore di una cella ad un'altra cella e quindi cancellarla
  • Come fare un argomento facoltativo?
  • Excel VBA - Selezione di più opzioni per ciascuna cella
  • Come autogenerare determinate stringhe excel da determinati input come indicato nell'esempio?
  • Excel VBA Loop sulle colonne
  • Esport gli elementi HTML in Excel in C #
  •  Sub ResizePic() NumPic = ActiveDocument.InlineShapes.Count For i = 1 To NumPic origWidth = ActiveDocument.InlineShapes(i).Width origHeight = ActiveDocument.InlineShapes(i).Height scaleVal = (200 / origWidth) With ActiveDocument.InlineShapes(i) .Height = origHeight * scaleVal .Width = origWidth * scaleVal End With Next i End Sub Sub AddPictureBox() NumPic = ActiveDocument.InlineShapes.Count Dim currentText As Variant For i = 1 To NumPic ActiveDocument.InlineShapes(1).Select Selection.CreateTextbox Selection.ShapeRange.Fill.Visible = msoFalse Selection.ShapeRange.Line.Visible = msoFalse Selection.ShapeRange.LockAspectRatio = msoFalse Selection.ShapeRange.Height = 180 Selection.ShapeRange.Width = 200 Selection.ShapeRange.TextFrame.MarginLeft = 0 Selection.ShapeRange.TextFrame.MarginRight = 0 Selection.ShapeRange.TextFrame.MarginTop = 3.69 Selection.ShapeRange.TextFrame.MarginBottom = 3.69 Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionLine Selection.ShapeRange.Left = wdShapeRight Selection.ShapeRange.Top = wdShapeTop Selection.ShapeRange.LockAnchor = True Selection.ShapeRange.WrapFormat.AllowOverlap = True Selection.ShapeRange.WrapFormat.Side = wdWrapBoth Selection.ShapeRange.WrapFormat.DistanceTop = CentimetersToPoints(0) Selection.ShapeRange.WrapFormat.DistanceBottom = CentimetersToPoints(0) Selection.ShapeRange.WrapFormat.DistanceLeft = CentimetersToPoints(0.32) Selection.ShapeRange.WrapFormat.DistanceRight = CentimetersToPoints(0.32) Selection.ShapeRange.WrapFormat.Type = wdWrapSquare Selection.ParagraphFormat.Alignment = wdAlignParagraphRight 'insert caption Selection.MoveRight Unit:=wdCharacter, Count:=1 Selection.TypeParagraph Selection.TypeText Text:="Caption " & i Selection.HomeKey Unit:=wdLine, Extend:=wdExtend Selection.Style = ActiveDocument.Styles("Caption") Next I Fine con Sub ResizePic() NumPic = ActiveDocument.InlineShapes.Count For i = 1 To NumPic origWidth = ActiveDocument.InlineShapes(i).Width origHeight = ActiveDocument.InlineShapes(i).Height scaleVal = (200 / origWidth) With ActiveDocument.InlineShapes(i) .Height = origHeight * scaleVal .Width = origWidth * scaleVal End With Next i End Sub Sub AddPictureBox() NumPic = ActiveDocument.InlineShapes.Count Dim currentText As Variant For i = 1 To NumPic ActiveDocument.InlineShapes(1).Select Selection.CreateTextbox Selection.ShapeRange.Fill.Visible = msoFalse Selection.ShapeRange.Line.Visible = msoFalse Selection.ShapeRange.LockAspectRatio = msoFalse Selection.ShapeRange.Height = 180 Selection.ShapeRange.Width = 200 Selection.ShapeRange.TextFrame.MarginLeft = 0 Selection.ShapeRange.TextFrame.MarginRight = 0 Selection.ShapeRange.TextFrame.MarginTop = 3.69 Selection.ShapeRange.TextFrame.MarginBottom = 3.69 Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionLine Selection.ShapeRange.Left = wdShapeRight Selection.ShapeRange.Top = wdShapeTop Selection.ShapeRange.LockAnchor = True Selection.ShapeRange.WrapFormat.AllowOverlap = True Selection.ShapeRange.WrapFormat.Side = wdWrapBoth Selection.ShapeRange.WrapFormat.DistanceTop = CentimetersToPoints(0) Selection.ShapeRange.WrapFormat.DistanceBottom = CentimetersToPoints(0) Selection.ShapeRange.WrapFormat.DistanceLeft = CentimetersToPoints(0.32) Selection.ShapeRange.WrapFormat.DistanceRight = CentimetersToPoints(0.32) Selection.ShapeRange.WrapFormat.Type = wdWrapSquare Selection.ParagraphFormat.Alignment = wdAlignParagraphRight 'insert caption Selection.MoveRight Unit:=wdCharacter, Count:=1 Selection.TypeParagraph Selection.TypeText Text:="Caption " & i Selection.HomeKey Unit:=wdLine, Extend:=wdExtend Selection.Style = ActiveDocument.Styles("Caption") Next I End Sub Sub ResizePic() NumPic = ActiveDocument.InlineShapes.Count For i = 1 To NumPic origWidth = ActiveDocument.InlineShapes(i).Width origHeight = ActiveDocument.InlineShapes(i).Height scaleVal = (200 / origWidth) With ActiveDocument.InlineShapes(i) .Height = origHeight * scaleVal .Width = origWidth * scaleVal End With Next i End Sub Sub AddPictureBox() NumPic = ActiveDocument.InlineShapes.Count Dim currentText As Variant For i = 1 To NumPic ActiveDocument.InlineShapes(1).Select Selection.CreateTextbox Selection.ShapeRange.Fill.Visible = msoFalse Selection.ShapeRange.Line.Visible = msoFalse Selection.ShapeRange.LockAspectRatio = msoFalse Selection.ShapeRange.Height = 180 Selection.ShapeRange.Width = 200 Selection.ShapeRange.TextFrame.MarginLeft = 0 Selection.ShapeRange.TextFrame.MarginRight = 0 Selection.ShapeRange.TextFrame.MarginTop = 3.69 Selection.ShapeRange.TextFrame.MarginBottom = 3.69 Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionLine Selection.ShapeRange.Left = wdShapeRight Selection.ShapeRange.Top = wdShapeTop Selection.ShapeRange.LockAnchor = True Selection.ShapeRange.WrapFormat.AllowOverlap = True Selection.ShapeRange.WrapFormat.Side = wdWrapBoth Selection.ShapeRange.WrapFormat.DistanceTop = CentimetersToPoints(0) Selection.ShapeRange.WrapFormat.DistanceBottom = CentimetersToPoints(0) Selection.ShapeRange.WrapFormat.DistanceLeft = CentimetersToPoints(0.32) Selection.ShapeRange.WrapFormat.DistanceRight = CentimetersToPoints(0.32) Selection.ShapeRange.WrapFormat.Type = wdWrapSquare Selection.ParagraphFormat.Alignment = wdAlignParagraphRight 'insert caption Selection.MoveRight Unit:=wdCharacter, Count:=1 Selection.TypeParagraph Selection.TypeText Text:="Caption " & i Selection.HomeKey Unit:=wdLine, Extend:=wdExtend Selection.Style = ActiveDocument.Styles("Caption") Next I Selection.ShapeRange.LockAspectRatio = msoFalse Sub ResizePic() NumPic = ActiveDocument.InlineShapes.Count For i = 1 To NumPic origWidth = ActiveDocument.InlineShapes(i).Width origHeight = ActiveDocument.InlineShapes(i).Height scaleVal = (200 / origWidth) With ActiveDocument.InlineShapes(i) .Height = origHeight * scaleVal .Width = origWidth * scaleVal End With Next i End Sub Sub AddPictureBox() NumPic = ActiveDocument.InlineShapes.Count Dim currentText As Variant For i = 1 To NumPic ActiveDocument.InlineShapes(1).Select Selection.CreateTextbox Selection.ShapeRange.Fill.Visible = msoFalse Selection.ShapeRange.Line.Visible = msoFalse Selection.ShapeRange.LockAspectRatio = msoFalse Selection.ShapeRange.Height = 180 Selection.ShapeRange.Width = 200 Selection.ShapeRange.TextFrame.MarginLeft = 0 Selection.ShapeRange.TextFrame.MarginRight = 0 Selection.ShapeRange.TextFrame.MarginTop = 3.69 Selection.ShapeRange.TextFrame.MarginBottom = 3.69 Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionLine Selection.ShapeRange.Left = wdShapeRight Selection.ShapeRange.Top = wdShapeTop Selection.ShapeRange.LockAnchor = True Selection.ShapeRange.WrapFormat.AllowOverlap = True Selection.ShapeRange.WrapFormat.Side = wdWrapBoth Selection.ShapeRange.WrapFormat.DistanceTop = CentimetersToPoints(0) Selection.ShapeRange.WrapFormat.DistanceBottom = CentimetersToPoints(0) Selection.ShapeRange.WrapFormat.DistanceLeft = CentimetersToPoints(0.32) Selection.ShapeRange.WrapFormat.DistanceRight = CentimetersToPoints(0.32) Selection.ShapeRange.WrapFormat.Type = wdWrapSquare Selection.ParagraphFormat.Alignment = wdAlignParagraphRight 'insert caption Selection.MoveRight Unit:=wdCharacter, Count:=1 Selection.TypeParagraph Selection.TypeText Text:="Caption " & i Selection.HomeKey Unit:=wdLine, Extend:=wdExtend Selection.Style = ActiveDocument.Styles("Caption") Next I 

    Il codice Excel si blocca a "wdApp.activedocument.AddPictureBox" e da quello che posso dire, il codice di codice si blocca in "Selection.CreateTextbox"

    Gradirei qualsiasi aiuto.

  • ricevi un file excel come risposta in javascript da un servizio di rest
  • Test per NULL
  • Trovare le celle vuote nella colonna A e conteggiarle
  • Se i dati in colonna si visualizzano nell'area di data, evidenziare
  • EXCEL: Come get il sumproduct di due colonne che è filtrato
  • SaveAs non accetterà le stringhe che contengono "." In Excel VBA
  • One Solution collect form web for “Questo codice "Selection.CreateTextbox" funziona in un file di versione di versione 93-2001, ma non in un file di parola 2010”

    Quindi, come principiante, vedo due cose.

    1. manca End Sub istruzione End Sub in fondo a AddPictureBox() . Suppongo che fosse un problema di copia / incolla.
    2. questa row: ActiveDocument.InlineShapes(1).Select , proprio sotto l'inizio del ciclo for, indici 1 e non i. Potrebbe essere la ragione per far saltare in aria, ansible sul secondo ciclo.

    EDIT : Così ho messo (uno alla volta) un GIF, JPG e PNG in un documento di parole. Facendo passare attraverso il codice, ottengo un errore di runtime 5 Invalid chiamata di procedura o argomento. Ciò accade su questa dichiarazione: Selection.ShapeRange.Fill.Visible = msoFalse

    Potrei scendere un foro di rabbitmq con le immagini che ho messo nel documento di parola, quindi mi sto inchinando. Vi suggerisco di passare attraverso il codice e vedere quale dichiarazione si è errato, e poi google che per suggerimenti.

    In bocca al lupo

    Microsoft Office Excel Spreadsheet è il miglior software di Office, Excel VBA e formule Excel rendono il foglio di calcolo più veloce.