【发布时间】:2019-06-26 13:48:32
【问题描述】:
我有一个 PowerApp,它可以更新 OneDrive 中托管的 Excel 文件中的单元格。 Excel 文件包含一个在 PowerApp 更改 Excel 文件时应该运行的宏。但是,它不会那样做。如果我手动更新单元格,宏就可以正常工作。当文件被 PowerApps 更新时,它只是没有被激活。
当 PowerApp 更改文件时,我可以使用不同的功能吗?
如果不可能,我可以使用 Flow 来激活宏吗?
这是适用于手动更改的当前脚本,但不适用于自动 PowerApps 更改。
Private Sub Worksheet_Change(ByVal Target As Range)
Call InsertImageTest
End Sub
这是我想使用上面的代码触发的宏。
Sub InsertImageTest()
' This macro inserts an image from a set location to a set cell.
Dim ws As Worksheet
Dim imagePath As String
Dim cell As String
Dim posText As String
Dim imgLeft As Double
Dim imgTop As Double
Dim rngX As Range
Dim activeSheetName As String
' Customizable variables
imagePath = ActiveWorkbook.Path & Range("$B$2").Value
posText = "Signature"
activeSheetName = "Data" ' Set to "Data" by default, but will change to the Active sheets name, if the active sheet is not called "Data"
' For i = 1 To Sheets.Count
' If CStr(Sheets(i).Name) Is CStr(activeSheetName) Then
' Debug.Print "Code can be executed! Data tab was found"
' End If
' Next i
cell = "$A$1"
Set ws = ActiveSheet
Set rngX = Worksheets(activeSheetName).Range("A1:Z1000").Find(posText, lookat:=xlPart)
If Not rngX Is Nothing Then
cell = rngX.Address
Debug.Print cell
Debug.Print rngX.Address & " cheating"
Worksheets(activeSheetName).Range(cell).Value = ""
Debug.Print rngX.Address & " real"
imgLeft = Range(cell).Left
imgTop = Range(cell).Top
' Width & Height = -1 means keep original size
ws.Shapes.AddPicture _
Filename:=imagePath, _
LinkToFile:=msoFalse, _
SaveWithDocument:=msoTrue, _
Left:=imgLeft, _
Top:=imgTop, _
Width:=-1, _
Height:=-1
End If
' The code beaneath will resize the cell to fit the picture
For Each Picture In ActiveSheet.DrawingObjects
PictureTop = Picture.Top
PictureLeft = Picture.Left
PictureHeight = Picture.Height
PictureWidth = Picture.Width
For N = 2 To 256
If Columns(N).Left > PictureLeft Then
PictureColumn = N - 1
Exit For
End If
Next N
For N = 2 To 65536
If Rows(N).Top > PictureTop Then
PictureRow = N - 1
Exit For
End If
Next N
Rows(PictureRow).RowHeight = PictureHeight
Columns(PictureColumn).ColumnWidth = PictureWidth * (54.29 / 288)
Picture.Top = Cells(PictureRow, PictureColumn).Top
Picture.Left = Cells(PictureRow, PictureColumn).Left
Next Picture
End Sub
【问题讨论】:
-
尝试
Worksheet_Calculate事件处理程序。 -
@Dean 不幸的是没有工作。我只是在更新一个字符串单元格值,所以不调用Calculate。
-
PowerApp 打开文件并修改它?
-
不清楚 PowerApps 如何操作单元格中的数据。使用 PowerApps 操作工作簿时是否打开?
-
@DavidZemens PowerApps 通过 API 编辑工作簿,因此在操作时不会打开。