Consulting

Results 1 to 3 of 3

Thread: how to insert some new lines

  1. #1

    Question how to insert some new lines

    I've got a VBA macro that calls up a company letterhead (saved in my documents folder) and adds the current date. Everything works fine but then I modified the macro so that it adds a line before the date and 2 new lines after the date. I'm no VBA expert but I thought the Selection.InsertParagraph would work for me... Sadly I'm getting an error "Object variable not set". Not really sure how to fix this.


    Sub NewLetterhead_Template()
        Dim wdApp As Word.Application
        Dim MyRange As Object
        Set wdApp = GetObject(, "Word.Application")
        Set MyRange = Selection.Range
        wdApp.Documents.add Template:="C:\Users\{my user id}\Documents\good letterhead 2014.dotx"
       ' insert a new line before date
       Selection.InsertParagraph
       ' Selection Example:
       Selection.InsertDateTime DateTimeFormat:="MMMM dd, yyyy", _
       InsertAsField:=False
       ' Range Example:
       MyRange.InsertDateTime DateTimeFormat:="MMM dd, yyyy", _
       InsertAsField:=False
       ' insert 2 new lines after the date
       Selection.InsertParagraph
    Set wdApp = Nothing
    Set MyRange = Nothing
    End Sub
    I should add that I'm no expert on this. The above code was just snippets that I found on the internet and I just did a mish mash job of putting it together.

  2. #2
    Presumably you are calling this macro from another Office application? That being the case there are a number of issues.
    If you are using late binding to Word, then wdApp must also be defined as an object.
    You have set the range to the selection before the document is opened, and in any case it is better to work with ranges and not the selection.
    The following will do the job

    Sub NewLetterhead_Template()
    Dim wdApp As Object
    Dim wdDoc As Object
    Dim MyRange As Object
        Set wdApp = GetObject(, "Word.Application")
        Set wdDoc = wdApp.Documents.Add(Template:="C:\Users\{my user id}\Documents\good letterhead 2014.dotx")
        Set MyRange = wdDoc.Range        'set the range to the new document
        MyRange.collapse 1        'collapse the range to the start of the document
        ' insert a new line before date (though frankly changing the style _
          ' to include the extra space would be preferable
        MyRange.Text = vbCr
        MyRange.collapse 0        'collapse the range to its end
        ' Selection Example:
        MyRange.InsertDateTime DateTimeFormat:="MMMM dd, yyyy", _
                               InsertAsField:=False
        ' Move the end of the range to the end of the date
        MyRange.End = MyRange.Paragraphs(1).Range.End
        MyRange.collapse 0        'collapse the range to its end
        ' insert two new lines in the new range locaton (after the date)
        MyRange.Text = vbCr & vbCr
        MyRange.collapse 0        'collapse the range to the end of the document
        MyRange.Select
        Set wdApp = Nothing
        Set wdDoc = Nothing
        Set MyRange = Nothing
    End Sub
    If you are running the macro from Word then

    Sub NewLetterhead_Template()
    Dim wdDoc As Document
    Dim MyRange As Range
    
    Set wdDoc = Documents.Add(Template:="C:\Users\{my user id}\Documents\good letterhead 2014.dotx")
    Set MyRange = wdDoc.Range        'set the range to the new document
    MyRange.Collapse 1        'collapse the range to the start of the document
        ' insert a new line before date (though frankly changing the style _
          ' to include the extra space would be preferable
        MyRange.Text = vbCr
        MyRange.Collapse 0        'collapse the range to its end
        ' Selection Example:
        MyRange.InsertDateTime DateTimeFormat:="MMMM dd, yyyy", _
                               InsertAsField:=False
        ' Move the end of the range to the end of the date
        MyRange.End = MyRange.Paragraphs(1).Range.End
        MyRange.Collapse 0        'collapse the range to its end
        ' insert two new lines in the new range locaton (after the date)
        MyRange.Text = vbCr & vbCr
        MyRange.Collapse 0        'collapse the range to the end of the document
        MyRange.Select
        Set wdDoc = Nothing
        Set MyRange = Nothing
    End Sub
    It is worth bearing in mind that you don't need either macro. Simply insert a Createdate field in the document template and format to provide the required space using paragraph formatting (space before/space after) and then when you create a new document the date will be correct.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Thanks for the code Graham - works just fine. And yes you're right about inserting a date field into the template. Only reason why I embarked on this little task to get more familiar with VBA code -- fumbling as I am, I am sort of enjoying learning how you experts go about doing this type of work. Also BTW, i saw your response to my other thread about Outlook help.... I have a question for you on that. I'll update that thread in a little bit. Thanks again.

Posting Permissions

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