Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-02-2016, 04:16 AM
rpb925 rpb925 is offline Insert and Filling Textbox using VBA Windows 7 64bit Insert and Filling Textbox using VBA Office 2010 64bit
Novice
Insert and Filling Textbox using VBA
 
Join Date: Mar 2016
Location: Sydney
Posts: 17
rpb925 is on a distinguished road
Default Insert and Filling Textbox using VBA

I assume this is straight forward but it isn't really happening for me. I want to:



Add a Text Box to the Right hand Side and have written vertically the word MEMO font size 48 Arial in grey. I have been working from a few things online but to no avail. I am running the code from access in a newly created word document. It always comes up specified value is out of Range and it's starting to get repetitive. Any help is appreciated. P.S I realise I haent added text yet and the box is too small but I haven't gotten that far yet...
Code:
Sub Textbox()
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wdShp As Word.Shape
Set wdApp = CreateObject("Word.Application")
    
With wdApp
.Visible = True
.ScreenUpdating = False

Set wdDoc = .Documents.Add

    With wdDoc
    Set wdShp = .Shapes.AddTextbox(Orientation:=msoTextOrientationVertical, Left:=10, Top:=10, Width:=10, Height:=10)
    End With
 
.ScreenUpdating = True

End With

Set wdDoc = Nothing: Set wdApp = Nothing: Set wdShp = Nothing

End Sub
Reply With Quote
  #2  
Old 04-02-2016, 04:58 AM
gmayor's Avatar
gmayor gmayor is offline Insert and Filling Textbox using VBA Windows 10 Insert and Filling Textbox using VBA Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Maybe something like:

Code:
Sub Textbox()
Dim wdApp As Object
Dim wdDoc As Object
Dim wdShp As Object
Dim oRng As Object
    On Error Resume Next
    Set wdApp = GetObject(, "Word.Application") 'It is always faster to get the running Word if available
    If Err Then
        Set wdApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    With wdApp
        .Visible = True
        .ScreenUpdating = False

        Set wdDoc = wdApp.Documents.Add
        Set wdShp = wdDoc.Shapes.AddTextbox(Orientation:=msoTextOrientationUpward, _
                                            Left:=10, _
                                            Top:=10, _
                                            Width:=wdApp.CentimetersToPoints(1.25), _
                                            Height:=wdApp.CentimetersToPoints(10))
        With wdShp
            Set oRng = .TextFrame.TextRange
            With oRng
                .Text = "This is the text box text"
                .Font.Name = "Times New Roman"
                .Font.Size = 14
                .Font.Italic = True
                .ParagraphFormat.Alignment = 1 'Centred
            End With
        End With
        .ScreenUpdating = True
    End With
lbl_exit:
    Set wdDoc = Nothing: Set wdApp = Nothing: Set wdShp = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 04-02-2016, 05:11 AM
gmaxey gmaxey is offline Insert and Filling Textbox using VBA Windows 7 32bit Insert and Filling Textbox using VBA Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

The following runs fine when run from within Word. What line of your code is throwing the error?

Code:
Sub Textbox()
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wdShp As Word.Shape
 'Set wdApp = CreateObject("Word.Application")
 With Application
    .Visible = True
    .ScreenUpdating = False
    Set wdDoc = .Documents.Add
    Set wdShp = wdDoc.Shapes.AddTextbox(Orientation:=msoTextOrientationVertical, Left:=10, Top:=10, Width:=70, Height:=200)
    With wdShp.TextFrame
      .TextRange = "MEMO"
      .TextRange.Font.Name = "Arial"
      .TextRange.Font.Size = "48"
    End With
    .ScreenUpdating = True
  End With
  Set wdDoc = Nothing: Set wdShp = Nothing
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #4  
Old 04-02-2016, 05:48 AM
rpb925 rpb925 is offline Insert and Filling Textbox using VBA Windows 7 64bit Insert and Filling Textbox using VBA Office 2010 64bit
Novice
Insert and Filling Textbox using VBA
 
Join Date: Mar 2016
Location: Sydney
Posts: 17
rpb925 is on a distinguished road
Default

It seems like it doesn't like the AddTextbox Command it keeps coming up with following Error Run-time eror '-2147024809(80070057) The Specified value is out of range. I've copy and pasted the vba into a macro in word and it runs like a charm. I believe it is something to do with running the addTextbox from Access that is flipping it out.
If you have access I assume you'll get the same error. Not sure if I have to set the variable as something other than a "as Word.Shape"

PS Thanks Graham for the tip on running already open word application will try it out!!
Reply With Quote
  #5  
Old 04-02-2016, 06:14 AM
gmaxey gmaxey is offline Insert and Filling Textbox using VBA Windows 7 32bit Insert and Filling Textbox using VBA Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

I would assume you hare using early binding and ave a reference to the Word Object module set in your project. I used late binding as shown and it worked without issue from Access 2016:

Code:
Sub Textbox()
Dim wdApp As Object
Dim wdDoc As Object
Dim wdShp As Object
Set wdApp = CreateObject("Word.Application")
  With wdApp
    .Visible = True
    .ScreenUpdating = False
    Set wdDoc = .Documents.Add
    With wdDoc
      Set wdShp = .Shapes.AddTextbox(Orientation:=5, Left:=10, Top:=10, Width:=10, Height:=10)
    End With
   .ScreenUpdating = True
  End With
  Set wdDoc = Nothing: Set wdApp = Nothing: Set wdShp = Nothing
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #6  
Old 04-02-2016, 02:35 PM
rpb925 rpb925 is offline Insert and Filling Textbox using VBA Windows 7 64bit Insert and Filling Textbox using VBA Office 2010 64bit
Novice
Insert and Filling Textbox using VBA
 
Join Date: Mar 2016
Location: Sydney
Posts: 17
rpb925 is on a distinguished road
Talking

Legendary. It was the text direction command it was tripping on. Once replaced with msoTextDirection with a numerical value as you did it works well. Definitely would not have worked that one out on my own. It seems to run both as a Object and Word.Shape with no issues.
Thanks Greg.
Appreciate it.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert and Filling Textbox using VBA Restrict Editing function disable insert textbox function IanM_01 Word 5 11-21-2015 02:29 AM
Insert superscript into a textbox Deltaj Word VBA 3 11-30-2014 04:23 PM
insert text in freeform graphic (make it a textbox) fzr Word 2 09-03-2014 05:54 AM
Insert and Filling Textbox using VBA Display result in textbox based on the input of another textbox scarymovie Word VBA 5 05-16-2012 07:05 PM
How to insert a hyperlink in activex textbox Joe Patrick Word VBA 1 10-03-2011 06:03 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:44 AM.


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