Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-04-2023, 04:35 PM
puff puff is offline Insert a canvas at current cursor location [code attached] Windows 10 Insert a canvas at current cursor location [code attached] Office 2016
Advanced Beginner
Insert a canvas at current cursor location [code attached]
 
Join Date: Apr 2017
Posts: 60
puff is on a distinguished road
Question Insert a canvas at current cursor location [code attached]

Hi! I simply want to insert a canvas as an inline at the current location of my cursor but my code keeps inserting it at the 1st paragraph of a page. Below attached my code:
Code:
Sub InsertCanvas()
    Dim shpCanvas As Shape
    Dim CanvasWidth As Double
    Dim CanvasHeight As Double
    Dim currentParagraph As Paragraph
      

    
    Call FigureTools.ExitCompatibilityMode 'Exit the compatibility mode 
    
    ' Get the current paragraph
    Set currentParagraph = Selection.Range.Paragraphs(1)
    
    CanvasWidth = 450 
    CanvasHeight = 252 
   
    
    ' Add a drawing canvas to the active document at the current paragraph
    Set shpCanvas = ActiveDocument.Shapes.AddCanvas(Left:=100, Top:=75, Width:=CanvasWidth, Height:=CanvasHeight, Anchor:=currentParagraph.Range)
 
    
    With shpCanvas
        .WrapFormat.Type = wdWrapInline

        .Visible = msoTrue 
    End With
    
End Sub
According to the documentation, the Anchor of AddCanvas() is "A Range object that represents the text to which the canvas is bound. If Anchor is specified, the anchor is positioned at the beginning of the first paragraph in the anchoring range. If this argument is omitted, the anchoring range is selected automatically and the canvas is positioned relative to the top and left edges of the page." I don't understand as AddCanvas seems to ignore the Anchor I provided. Could someone give some suggestions on how to fix this? Thanks!
Reply With Quote
  #2  
Old 06-04-2023, 07:23 PM
Guessed's Avatar
Guessed Guessed is online now Insert a canvas at current cursor location [code attached] Windows 10 Insert a canvas at current cursor location [code attached] Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,966
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I think you need to position the canvas at the location of the selected paragraph before setting it to inline.
Code:
Sub InsertCanvas2()
  Dim shpCanvas As Shape, CanvasWidth As Double, iTop As Integer, iLeft As Integer
  Dim CanvasHeight As Double, aRng As Range
    
  'Call FigureTools.ExitCompatibilityMode 'Exit the compatibility mode
  
  ' Get the current paragraph
  Set aRng = Selection.Range.Paragraphs(1).Range
  iTop = aRng.Information(wdVerticalPositionRelativeToPage)
  iLeft = aRng.Information(wdHorizontalPositionRelativeToPage)
  CanvasWidth = 450
  CanvasHeight = 252
  
  ' Add a drawing canvas to the active document at the current paragraph
  Set shpCanvas = ActiveDocument.Shapes.AddCanvas(Left:=iLeft, Top:=iTop, Width:=CanvasWidth, Height:=CanvasHeight, Anchor:=aRng)
  
  With shpCanvas
    '.Fill.ForeColor.RGB = RGB(24, 100, 30)
    .WrapFormat.Type = wdWrapInline
    .Visible = msoTrue
  End With
    
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 06-05-2023, 06:45 AM
puff puff is offline Insert a canvas at current cursor location [code attached] Windows 10 Insert a canvas at current cursor location [code attached] Office 2016
Advanced Beginner
Insert a canvas at current cursor location [code attached]
 
Join Date: Apr 2017
Posts: 60
puff is on a distinguished road
Default

Thanks for the response! On my side your code somehow always inserts the canvas into the paragraph that is above the paragraph that the cursor is currently in. I used "Set currentParagraphRange = Selection.Range.Paragraphs(1).Range.Next" to solve the issue but ran into errors if the current paragraph is the last paragraph in the document so there's no "Next". Is there a way to solve this?
Reply With Quote
  #4  
Old 06-05-2023, 03:24 PM
Guessed's Avatar
Guessed Guessed is online now Insert a canvas at current cursor location [code attached] Windows 10 Insert a canvas at current cursor location [code attached] Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,966
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I would try fiddling with the iTop number. If you add some extra to it then it will make the frame sit lower on the page and ideally start at the same location as your selected paragraph. For example, try adding a fixed number to it and try different values until you consistently get the frame appearing against the paragraph you like.

iTop = aRng.Information(wdVerticalPositionRelativeToPage) + 40
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 06-05-2023, 06:03 PM
puff puff is offline Insert a canvas at current cursor location [code attached] Windows 10 Insert a canvas at current cursor location [code attached] Office 2016
Advanced Beginner
Insert a canvas at current cursor location [code attached]
 
Join Date: Apr 2017
Posts: 60
puff is on a distinguished road
Default

Thanks a lot! This works.

For future reference, I used + 10 with font size 12 and Times New Roman.
Reply With Quote
Reply

Tags
canvas

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Determine Style of paragraph at current location exoson Word VBA 2 01-25-2022 11:15 AM
Insert a canvas at current cursor location [code attached] How do I insert a drawing canvas in Office Professional 2010? laredotornado Word 2 02-22-2017 02:28 PM
Insert a canvas at current cursor location [code attached] Cursor location on first click -- insertion point stevec5088 Word 1 12-14-2016 02:45 PM
Insert a canvas at current cursor location [code attached] Hightlight cells based on cursor location megads Excel Programming 2 07-31-2014 06:10 PM
Insert a canvas at current cursor location [code attached] Auto insert current month's name and current year Styler001 Word 4 01-25-2010 06:40 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:33 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft