Page 1 of 3 123 LastLast
Results 1 to 15 of 40
  1. #1
    New Lounger
    Join Date
    Aug 2002
    Location
    British Columbia, Canada
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Word 2010 macro fails when inserting text box

    After upgrading to Office 2010, I tried to record a macro like one I'd used in Word 2007:
    1) In an open document, click on "Developer" tab, click on Record Macro, name it "InsertTextBox" and give short description, and click "OK" ("Store..." shows "Normal.dotm".)
    2) Click on "Insert" tab, then on "Text Box" then on "Simple Text Box"
    3) Type "This is the text"
    3) Press "Escape" twice to regain regular cursor
    4) Click on "Developer" tab, then on "Stop Recording"

    When I try to run the macro, I get a Microsoft Visual Basic error window, saying:
    ====================================
    Run-Time Error
    '-2147024809(80070057)'
    The item with the specified name wasn't found.
    =====================================

    If I click on "Debug", I see:
    =====================================
    Sub InsertTextBox()
    '
    ' InsertTextBox Macro
    ' InsertsTextBox
    '
    ActiveDocument.Shapes.Range(Array("Text Box 2")).Select
    Application.Templates( _
    "C:\Users\Dave\AppData\Roaming\Microsoft\Docum ent Building Blocks\1033\14\Built-In Building Blocks.dotx" _
    ).BuildingBlockEntries(" Simple Text Box").Insert Where:=Selection.Range, _
    RichText:=True
    Selection.TypeText Text:="This is the text"
    Selection.EscapeKey
    Selection.Collapse
    End Sub
    =========================================
    Highlighted in yellow is the line "ActiveDocument .... Select"

    I've tried this several times, with the same result, regardless of whether I choose to insert "simple text box" or any of the others, or "draw text box" and create my own.

    Any ideas of what I'm doing wrong or why it doesn't work... since it did in Word 2007...?

    Many thanks.

    Dave
    Last edited by Gary Frieder; 2011-05-06 at 08:38. Reason: Removed e-mail address

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,772
    Thanks
    0
    Thanked 162 Times in 150 Posts
    Hi Dave,

    Try something along the lines of:
    Code:
    Sub InsertTextBox()
    Dim Shp As Shape
    Set Shp = ActiveDocument.Shapes.AddTextbox( _
      Orientation:=msoTextOrientationHorizontal, _
      Left:=72, Top:=72, Width:=288, Height:=72)
    Shp.TextFrame.TextRange.Text = "This is the text"
    Set Shp = Nothing
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  4. #3
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 27 Times in 26 Posts
    Dave,

    The code posted by macropod is generally going to be more reliable than the code you recorded, so you should give that a try.

    However, if you do want to get your recorded code running, it will work OK if you just delete the first line of code (the one you flagged as causing an error). Also, make sure there's no space in the middle of the word "Document" as there is in the code you posted here.
    One possible benefit of the code as you recorded it, is that the text box it brings in as a building block, contains a content control with explanatory text in it - that might be helpful to inexperienced users (or annoying overkill to experienced users!)

    Gary

    PS: I removed the e-mail address that you added - it might not be a good idea to post your personal e-mail address here.

  5. #4
    New Lounger
    Join Date
    Aug 2002
    Location
    British Columbia, Canada
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Paul,
    Thanks. I should probably have mentioned in my post that I never learned to "write" macros... just to "record" them in Word.
    So, I just tried replacing the code I'd posted with yours. It *does* work... but leaves me with two other quandaries: not knowing Basic, how do I make the changes I'll need (e.g., having my text in red and adding an "updating" date; and, why doesn't *recording* the macro actually produce a working macro?
    Cheers.
    Dave

  6. #5
    New Lounger
    Join Date
    Aug 2002
    Location
    British Columbia, Canada
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Gary,
    And, thanks to you, too.
    I tried what you suggested. It resulted in the words (no quotes) "This is the text" at the left margin, and a textbox anchored a short distance to its right, in which remains Word's field code, "[Type a quote from the document or the summary of an interesting point. You can position the text box anywhere in the document. Use the Drawing Tools tab to change the formatting of the pull quote text box.]".
    And, I'm still left with the quandaries I just posted as a reply to Paul.
    Again, though, many thanks.
    Cheers.
    Dave

  7. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,772
    Thanks
    0
    Thanked 162 Times in 150 Posts
    Hi Dave,

    The underlying reason your recorded code didn't work is because the line:
    ActiveDocument.Shapes.Range(Array("Text Box 2")).Select
    tells Word to select an exsiting textbox (which may or may not exist) and replace that with another one.

    As for:
    It *does* work... but leaves me with two other quandaries: not knowing Basic, how do I make the changes I'll need (e.g., having my text in red and adding an "updating" date
    it's never too late to start learning. The macro recorder can be used to create code showing you the basics of what's required to make the text red and insert a date field. It's then just a matter of adapting that code to your needs. You'll soon start to get the hang of it, especially if you do searches for code that does similar things (eg: Word vba font color red, or Word vba date field)
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  8. #7
    New Lounger
    Join Date
    Aug 2002
    Location
    British Columbia, Canada
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Paul,
    Thanks again.
    I'll give it a shot... :-)
    Dave
    p.s. I'm still wondering, though, why Word's own macro recorder produces a macro that doesn't work... when I used the same process to create a macro that *did* work in Word 2007.. or is that just another "Microsoft feature"... :-)

  9. #8
    Lounger
    Join Date
    Jul 2013
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Macropod, can you please help me expand that VBA coding? I am using that coding to insert 2 Textboxes (one horizontal in upper left corner of paper and one vertical along right side of paper) and I have changed the "This is the text" to suit what my company needs to have on the paper. The business process is date stamping (mail room gets claim forms and attachments one day before they show up on my desk and I have to date stamp all pages with Yesterday's date) (also, on Monday's, I have to date stamp Friday's date).

    Good news is that I already have the coding to Insert Friday's and Yesterday's dates by running the 2 macros respectively. I also have created an AutoOpen macro that puts 2 text boxes in the right positions on the page with the Text I need it to say, but the part I need help with is how to insert Yesterday's date and Friday's date within the text boxes after the text ("This is the text") and also be in the format I need it to be in.

    Here is the VBA coding I am using (wish I could just upload my file on this forum??):

    Sub AutoOpen()
    '
    ' AutoOpen Macro that places the cursor at the last position of edit (not default of top left) (will work with Protected View)
    If Application.ActiveProtectedViewWindow Is Nothing Then
    Application.GoBack
    End If

    ' InsertTextBox1 Macro
    Dim Shp As Shape
    Set Shp = ActiveDocument.Shapes.AddTextbox( _
    Orientation:=msoTextOrientationVertical, _
    Left:=22, Top:=252, Width:=25, Height:=170)
    Shp.TextFrame.TextRange.Text = "NMM RECEIVED: "
    Set Shp = Nothing
    Selection.InsertAfter Format(Now() - 1, "dddd, MMMM dd, yyyy")
    Selection.Font.Size = 8
    Selection.Font.Name = "Arial Narrow"
    Selection.Font.Bold = wdToggle
    Selection.Font.Bold = wdToggle

    ' InsertTextBox2 Macro
    Dim Shp2 As Shape
    Set Shp2 = ActiveDocument.Shapes.AddTextbox( _
    Orientation:=msoTextOrientationHorizontal, _
    Left:=432, Top:=752, Width:=170, Height:=18)
    Shp2.TextFrame.TextRange.Text = "NMM RECEIVED: "
    Set Shp2 = Nothing
    End Sub

    Let me know how to do this please??

  10. #9
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,432
    Thanks
    3
    Thanked 125 Times in 118 Posts
    Does this work?
    Code:
        Dim Shp As Shape, sDate As String
      If Format(Now(), "ddd") = "Mon" Then
        sDate = Format(Now() - 3, "dddd, MMMM dd, yyyy")
      Else
        sDate = Format(Now() - 1, "dddd, MMMM dd, yyyy")
      End If
      
      Set Shp = ActiveDocument.Shapes.AddTextbox(Orientation:=msoTextOrientationVertical, _
                Left:=22, Top:=252, Width:=25, Height:=170)
      Shp.TextFrame.TextRange.Style = "Normal"
      Shp.TextFrame.TextRange.Font.Size = 8
      Shp.TextFrame.TextRange.Font.Name = "Arial Narrow"
      Shp.TextFrame.TextRange.Text = "NMM RECEIVED: " & sDate
    Last edited by Andrew Lockton; 2013-07-03 at 19:04.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  11. #10
    Lounger
    Join Date
    Jul 2013
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you so much Andrew Lockton (my last name is Bourke like that city I heard about in Australia)

    Anyway, I would like 2 text boxes to populate automatically when this file opens. I plan on making 1 file for Yesterday's date, 1 for 3 day weekends (Now() - 4), 1 for 4 day weekends (Now() - 5), and 1 for 2 days back (like yesterday we got off of work for 4th of July Independence Day but have to work today so that is Now() - 2) unless there is a way to handle that if I let you know the days our work schedule meets those conditions??

    Here is the code I have that is doing exactly what I want it to do:

    Sub AutoOpen()
    If ActiveDocument.Name = "test AutoOpen Macro.docx" Then

    ' InsertTextBox1 (Vertical)
    Dim Shp As Shape, sDate As String
    If Format(Now(), "ddd") = "Mon" Then
    sDate = Format(Now() - 3, "dddd, MMMM dd, yyyy")
    Else
    sDate = Format(Now() - 1, "dddd, MMMM dd, yyyy")
    End If

    Set Shp = ActiveDocument.Shapes.AddTextbox(Orientation:=msoT extOrientationVertical, _
    Left:=7, Top:=252, Width:=25, Height:=170)
    Shp.TextFrame.TextRange.Style = "Normal"
    Shp.TextFrame.TextRange.Font.Size = 8
    Shp.TextFrame.TextRange.Font.Name = "Arial Narrow"
    Shp.TextFrame.TextRange.Text = "NMM RECEIVED: " & sDate
    Shp.Line.Visible = msoFalse

    ' InsertTextBox2 (Horizontal)
    Dim Shp2 As Shape, sDate2 As String
    If Format(Now(), "ddd") = "Mon" Then
    sDate2 = Format(Now() - 3, "dddd, MMMM dd, yyyy")
    Else
    sDate2 = Format(Now() - 1, "dddd, MMMM dd, yyyy")
    End If

    Set Shp2 = ActiveDocument.Shapes.AddTextbox(Orientation:=msoT extOrientationHorizontal, _
    Left:=462, Top:=765, Width:=142, Height:=18)
    Shp2.TextFrame.TextRange.Style = "Normal"
    Shp2.TextFrame.TextRange.Font.Size = 8
    Shp2.TextFrame.TextRange.Font.Name = "Arial Narrow"
    Shp2.TextFrame.TextRange.Text = "NMM RECEIVED: " & sDate
    Shp2.Line.Visible = msoFalse
    Shp2.IncrementRotation (180)
    End If
    End Sub

    1 Issue, though:

    I need both of the Text boxes to be transparent (50%) (because I superimpose the text boxes onto claim forms that have writing and form date that goes all the way to almost the edge of the paper already and if I make it not transparent, it will make the text unreadable. So I use transparency to make the text in the text box readable as well as the claim text that is already printed on there prior to running it them through the printer again).

    Besides that, perfection!!! Thank you so much!!!!
    Last edited by LukeB; 2013-07-05 at 12:44. Reason: better syntax

  12. #11
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,432
    Thanks
    3
    Thanked 125 Times in 118 Posts
    Luke
    This variation prompts the user to provide the number of lead days required. It provides the default starting point (1 day or 3 days) so you can usually just press enter unless there is a special holiday delay (in which case, you type in the number of days you need the date to roll back by).
    Code:
    Sub Temp1()
      Dim Shp As Shape, Shp2 As Shape, sDate As String, iDelay As Integer
      If Format(Now(), "ddd") = "Mon" Then
        iDelay = 3
      Else
        iDelay = 1
      End If
      iDelay = InputBox("How many days ago do you need the logging box to display", "Received x days ago", iDelay)
      sDate = Format(Now() - iDelay, "dddd, MMMM dd, yyyy")
      
      Set Shp = ActiveDocument.Shapes.AddTextbox(Orientation:=msoTextOrientationVertical, _
            Left:=7, Top:=252, Width:=25, Height:=170)
      Shp.TextFrame.TextRange.Style = "Normal"
      Shp.TextFrame.TextRange.Font.Size = 8
      Shp.TextFrame.TextRange.Font.Name = "Arial Narrow"
      Shp.TextFrame.TextRange.Text = "NMM RECEIVED: " & sDate
      Shp.Fill.ForeColor = RGB(255, 255, 255)
      Shp.Fill.Transparency = 0.7
      Shp.Line.Visible = msoFalse
      
      Set Shp2 = ActiveDocument.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _
            Left:=462, Top:=765, Width:=142, Height:=18)
      Shp2.TextFrame.TextRange.Style = "Normal"
      Shp2.TextFrame.TextRange.Font.Size = 8
      Shp2.TextFrame.TextRange.Font.Name = "Arial Narrow"
      Shp2.TextFrame.TextRange.Text = "NMM RECEIVED: " & sDate
      Shp2.Line.Visible = msoFalse
      Shp.Fill.ForeColor = RGB(255, 255, 255)
      Shp.Fill.Transparency = 0.7
      Shp2.IncrementRotation (180)
    End Sub
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  13. #12
    Lounger
    Join Date
    Jul 2013
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So I see now that you have parameterized the macro....very cool....(I didn't know how to do that in VBA, have been working in MS Access GUI using the [Enter parameter here] syntax in the Criteria row of the GUI)

    Proposal #1: Would it be possible to figure in parameters only for when holidays that my company has declared a day off of work (to check first the longest date range where business is closed due to holidays) and apply the parameter?

    Example1: I believe that the longest amount of days in the calendar year my company gets days off of work for is Thanksgiving (4 days off in a row: 4th Thursday and Friday of every November plus the Weekend days of Saturday and Sunday). So could we have the VBA check for specified dates in the calendar year and if those specified dates fall within a 5 day range, run the parameterized statement? If not, then it could just run the If statement checking for if it is Monday (Now() - 3 days) and if not, Now() - 1 day?
    I believe that we could put each specified holiday day off of work into a Case statement and show the parameterized Input Box....

    Proposal #2: Would there be a way to tie in those specific dates to the system calendar so that the code would
    1. First check if one of those specified dates falls into a weekday, if not just go to If statement for Monday (Now() - 3 days), else Now() - 1 day.
    2. If one of those specified dates happens to fall on a weekday between Tuesday and Thursday, apply a Now() - 2 days (since business would be open the day before and after one of those days)
    3. If one of those specified dates happens to fall on a Monday or a Friday, apply a Now() - 4 days (since it would be a 3 day weekend for employees)
    4. Could we specifiy a date range for a 4 day weekend (Thanksgiving weekend is always the 4th Thursday and Friday of November) and apply a Now() - 5 days
    5. I don't think that this one would apply but: If it is a holiday where employees get 2 days off, then go back to work the 3rd day, then apply a Now() - 3 days
    Again, I believe that a Case statement could handle this sort of thing....??

    Code:
    Sub AutoOpen()
      Dim Shp As Shape, Shp2 As Shape, sDate As String, iDelay As Integer
      If Format(Now(), "ddd") = "Mon" Then
        iDelay = 3
      Else
        iDelay = 1
      End If
      iDelay = InputBox("NMM RECEIVED How many day(s) ago?", "Received x days ago", iDelay)  I am not sure why "Received x days ago" is written here??
      sDate = Format(Now() - iDelay, "dddd, MMMM dd, yyyy")
      
      Set Shp = ActiveDocument.Shapes.AddTextbox(Orientation:=msoTextOrientationVertical, _
            Left:=7, Top:=252, Width:=25, Height:=170)
      Shp.TextFrame.TextRange.Style = "Normal"
      Shp.TextFrame.TextRange.Font.Size = 8
      Shp.TextFrame.TextRange.Font.Name = "Arial Narrow"
      Shp.TextFrame.TextRange.Text = "NMM RECEIVED: " & sDate
      Shp.Fill.ForeColor = RGB(255, 255, 255)  I am not sure what this line is doing (guessing it is White)??
      Shp.Fill.Transparency = 0.7                     This isn't making the text inside the text box transparent??
      Shp.Line.Visible = msoFalse
      
      Set Shp2 = ActiveDocument.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _
            Left:=462, Top:=765, Width:=142, Height:=18)
      Shp2.TextFrame.TextRange.Style = "Normal"
      Shp2.TextFrame.TextRange.Font.Size = 8
      Shp2.TextFrame.TextRange.Font.Name = "Arial Narrow"
      Shp2.TextFrame.TextRange.Text = "NMM RECEIVED: " & sDate
      Shp.Fill.ForeColor = RGB(255, 255, 255)     I am not sure what this line is doing (guessing it is White)??
      Shp.Fill.Transparency = 0.7                        This isn't making the text inside the text box transparent??
      Shp2.Line.Visible = msoFalse
      Shp2.IncrementRotation (180)
    End Sub
    For sure, this sort of capability would be amongst the greatest most useful templates that many many people's lives would be beneficiaries and of course, you would get all the CREDIT Andrew!!!
    Thank you so much for your help Andrew again. I really appreciate it!!!
    Last edited by LukeB; 2013-07-07 at 00:54.

  14. #13
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,432
    Thanks
    3
    Thanked 125 Times in 118 Posts
    To explain the red bits in the code
    1. The InputBox function is shown with three parameters: The first is the text shown inside the dialog, the second is the title of the dialog, the third is the string value which appears as the default answer.
    2. Yes, this is giving the textframe a white fill and the next line makes it partially transparent. If you made the text transparent then you won't be able to read it.

    Yes it is possible to get the macro to work completely off a calendar of your choosing but that is something that may need to be maintained and updated by yourself every year (since holiday dates vary with location and year). As such, this is beyond my control and so I haven't coded it that way. In addition, it would take a lot more work to put together and I only do little jobs for free . What I will do for free is point you towards google which does have examples where others have tried to solve similar problems. Standing on the shoulders of those giants might get you what you require.

    http://www.ozgrid.com/forum/showthread.php?t=75758
    http://www.vbaexpress.com/kb/getarticle.php?kb_id=42
    http://www.vbaexpress.com/forum/showthread.php?t=38740
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  15. #14
    Lounger
    Join Date
    Jul 2013
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for the references Andrew,

    I will definitely try to get it to work off of the calendar and maintain it every year (even though I am not good at VBA)...

    As far as the transparency, I am not sure why the text won't show as 50% transparent (or 70% transparent which is what it looks like the 0.7 means)?
    I mean, if I right click on the text within the text box and select Font, then hit the Text Effects button, then change the transparency to 50%, the text will show in a gray transparent color shade (which is what I need it to do since on many of the forms, printed letters are already present in black--so if I printed without transparency, it would make what was already printed and the new date stamp both unreadable). I need a way of making the text transparent at 50%, not so that it is totally 100% transparent which, like you are saying, would make it unreadable.
    I have actually tried a few different things from seeing examples on the internet and it looks like all the examples use the Selection.[something I forgot].Transparency = 0.5 (or whatever percentage) syntax (which is different from the syntax we are using here to get this thing to work). I don't know if I have to completely scrap all of the syntax and figure out a way to make it transparent using the Selection.[next property].[next property] syntax??

    Please let me know how to figure out this transparency issue I am faced with because I can't seem to find anything conclusive on the Internet examples I have seen.....

  16. #15
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,432
    Thanks
    3
    Thanked 125 Times in 118 Posts
    I can see how to make the text partially transparent using the Text Effects button but this is not captured by the Macro Recorder and the VBA editor's intellisense doesn't provide any assistance either. I was expecting something like the following would work but frustratingly it doesn't ...

    Shp2.TextFrame.TextRange.Font.Transparency = 0.5

    In the absence of any relevant VBA code being discovered, you may need to take another approach. For instance, place the text as a graphic or word art and make that partially transparent. Another alternative is to simply apply a style to that content which already has the attributes you require which you set using the GUI.
    Shp.TextFrame.TextRange.Style = "SlimShady"
    That second option sounds much simpler but it has a minor detail you would first need to overcome ie. Is the style already in the document!!. You would need to firstly copy this from the template where the macro resides before calling that line. There will be code to do this somewhere on this forum.
    Last edited by Andrew Lockton; 2013-07-08 at 00:41.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •