我正在尝试编写一个将形状粘贴到点位置的宏,而不是将默认的ppt行为粘贴到复制的对象旁边的宏。
我为Get_Cursor_Pos宏分配了键盘快捷键,该快捷键保存了当前的点位置,然后尝试将其粘贴到Paste宏中。
但是,后者会将其粘贴到与保存的光标位置不同的位置。我怀疑这是由于两个宏中使用的定位单元不同。如何解决此问题?
' Access the GetCursorPos function in user32.dll
Declare Function GetCursorPos Lib "user32" _
(lpPoint As POINTAPI) As Long
' Access the GetCursorPos function in user32.dll
Declare Function SetCursorPos Lib "user32" _
(ByVal x As Long, ByVal y As Long) As Long
' GetCursorPos requires a variable declared as a custom data type
' that will hold two integers, one for x value and one for y value
Type POINTAPI
X_Pos As Long
Y_Pos As Long
End Type
' Dimension the variable that will hold the x and y cursor positions
Dim Hold As POINTAPI
' Main routine to dimension variables, retrieve cursor position,
' and display coordinates
Sub Get_Cursor_Pos()
' Place the cursor positions in variable Hold
GetCursorPos Hold
End Sub
Sub Paste()
ActivePresentation.Slides(1).Shapes.Paste
With ActiveWindow.Selection.ShapeRange
.Left = Hold.X_Pos
.Top = Hold.Y_Pos
End With
End Sub
- - - 编辑 - - - -
为了帮助其他有相同问题的人,这里提供了一个结合了Shyam和Steve的解答的解决方案。由于PPT不允许您将快捷键分配给宏(除非您使用付费的加载项),所以我不得不使用工具栏创建加载项,如此处所述http://www.pptfaq.com/FAQ00031_Create_an_ADD -IN_with_TOOLBARS_that_run_macros.htm。
Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
' Access the GetCursorPos function in user32.dll
Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
' GetCursorPos requires a variable declared as a custom data type
' that will hold two integers, one for x value and one for y value
Type POINTAPI
X_Pos As Long
Y_Pos As Long
End Type
' Dimension the variable that will hold the x and y cursor positions
Dim Hold As POINTAPI
Function GetSlideOriginOnScreen(Window As DocumentWindow) As POINTAPI
Dim pt As POINTAPI
With Window
pt.X_Pos = .PointsToScreenPixelsX(0)
pt.Y_Pos = .PointsToScreenPixelsY(0)
End With
GetSlideOriginOnScreen = pt
End Function
Function ConvertPixelToPointX(x As Long) As Single
Const LOGPIXELSX = 88
Const POINTSPERINCH = 72
Dim hDC As Long
Dim sngX As Long
hDC = GetDC(0)
sngX = GetDeviceCaps(hDC, LOGPIXELSX)
Call ReleaseDC(0, hDC)
ConvertPixelToPointX = (x / sngX) * POINTSPERINCH
End Function
Function ConvertPixelToPointY(y As Long) As Single
Const LOGPIXELSY = 90
Const POINTSPERINCH = 72
Dim hDC As Long
Dim sngY As Long
hDC = GetDC(0)
sngY = GetDeviceCaps(hDC, LOGPIXELSY)
Call ReleaseDC(0, hDC)
ConvertPixelToPointY = (y / sngY) * POINTSPERINCH
End Function
Sub Auto_Open()
Dim oToolbar As CommandBar
Dim oButton As CommandBarButton
Dim MyToolbar As String
' Give the toolbar a name
MyToolbar = "Paste Tools"
On Error Resume Next
' so that it doesn't stop on the next line if the toolbar's already there
' Create the toolbar; PowerPoint will error if it already exists
Set oToolbar = CommandBars.Add(Name:=MyToolbar, _
Position:=msoBarFloating, Temporary:=True)
If Err.Number <> 0 Then
' The toolbar's already there, so we have nothing to do
Exit Sub
End If
On Error GoTo ErrorHandler
' Now add a button to the new toolbar
Set oButton = oToolbar.Controls.Add(Type:=msoControlButton)
' And set some of the button's properties
With oButton
.DescriptionText = "Get cursor position"
'Tooltip text when mouse if placed over button
.Caption = "Get cursor position"
'Text if Text in Icon is chosen
.OnAction = "Button1"
'Runs the Sub Button1() code when clicked
.Style = msoButtonIcon
' Button displays as icon, not text or both
.FaceId = 38
' chooses icon #52 from the available Office icons
End With
' Now add a button to the new toolbar
Set oButton2 = oToolbar.Controls.Add(Type:=msoControlButton)
' And set some of the button's properties
With oButton2
.DescriptionText = "Paste at cursor"
'Tooltip text when mouse if placed over button
.Caption = "Paste at cursor"
'Text if Text in Icon is chosen
.OnAction = "Button2"
'Runs the Sub Button1() code when clicked
.Style = msoButtonIcon
' Button displays as icon, not text or both
.FaceId = 40
' chooses icon #52 from the available Office icons
End With
' Repeat the above for as many more buttons as you need to add
' Be sure to change the .OnAction property at least for each new button
' You can set the toolbar position and visibility here if you like
' By default, it'll be visible when created. Position will be ignored in PPT 2007 and later
oToolbar.Top = 150
oToolbar.Left = 150
oToolbar.Visible = True
NormalExit:
Exit Sub ' so it doesn't go on to run the errorhandler code
ErrorHandler:
'Just in case there is an error
MsgBox Err.Number & vbCrLf & Err.Description
Resume NormalExit:
End Sub
Sub Button1()
GetCursorPos Hold
End Sub
Sub Button2()
Dim zoom As Double
zoom = ActiveWindow.View.zoom / 100
With ActivePresentation.Slides(1).Shapes.Paste
.Left = ConvertPixelToPointX((Hold.X_Pos - GetSlideOriginOnScreen(ActiveWindow).X_Pos) / zoom)
.Top = ConvertPixelToPointY((Hold.Y_Pos - GetSlideOriginOnScreen(ActiveWindow).Y_Pos) / zoom)
End With
End Sub
正如史蒂夫所说,PointsToScreenPixelX(0)和PointsToScreenPixelY(0)属性将在幻灯片/普通视图中提供幻灯片左上边缘的屏幕坐标。如果您在下面添加了代码,则它将把形状定位在您存储的任何光标位置。
请注意,此代码段适用于窗口的缩放级别100。对于其他值,您将必须进行相应缩放。
Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
Function GetSlideOriginOnScreen(Window As DocumentWindow) As POINTAPI
Dim pt As POINTAPI
With Window
pt.X_Pos = .PointsToScreenPixelsX(0)
pt.Y_Pos = .PointsToScreenPixelsY(0)
End With
GetSlideOriginOnScreen = pt
End Function
Function ConvertPixelToPointX(X As Long) As Single
Const LOGPIXELSX = 88
Const POINTSPERINCH = 72
Dim hDC As Long
Dim sngX As Long
hDC = GetDC(0)
sngX = GetDeviceCaps(hDC, LOGPIXELSX)
Call ReleaseDC(0, hDC)
ConvertPixelToPointX = (X / sngX) * POINTSPERINCH
End Function
Function ConvertPixelToPointY(Y As Long) As Single
Const LOGPIXELSY = 90
Const POINTSPERINCH = 72
Dim hDC As Long
Dim sngY As Long
hDC = GetDC(0)
sngY = GetDeviceCaps(hDC, LOGPIXELSY)
Call ReleaseDC(0, hDC)
ConvertPixelToPointY = (Y / sngY) * POINTSPERINCH
End Function
现在将您的代码更改为以下调用:
Sub Paste()
With ActivePresentation.Slides(1).Shapes.Paste(1)
.Left = ConvertPixelToPointX(Hold.X_Pos - GetSlideOriginOnScreen(ActiveWindow).X_Pos)
.Top = ConvertPixelToPointY(Hold.Y_Pos - GetSlideOriginOnScreen(ActiveWindow).Y_Pos)
End With
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句