none
Generate table at marker position in e-mail RRS feed

  • Question

  • I have macro for Outlook which opens up a form, lets the user populate some fields and then creates a table in the e-mail based on the input in the form. The macro is used while already typing an e-mail. My problem is that the new table replaces everything in the e-mail. But I want to keep everything already written and just add the table where the marker is. I'm relatively new to VBA programming in Outlook and I'm struggling because you can't record a macro in Outlook. I'm using Outlook 2010. This is my code:

    Sub insertTable()
        If TypeName(ActiveWindow) = "Inspector" Then
            If ActiveInspector.IsWordMail And ActiveInspector.EditorType = olEditorWord Then
                With ActiveInspector.WordEditor.Application.activedocument
                    .tables.Add Range:=.Range, NumRows:=nItems + 2, NumColumns:=5, _
                    DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
                    With .tables(1)
                        If .Style <> "Table Grid" Then
                            .Style = "Table Grid"
                        End If
                        .ApplyStyleHeadingRows = True
                        .ApplyStyleLastRow = False
                        .ApplyStyleFirstColumn = True
                        .ApplyStyleLastColumn = False
                        .ApplyStyleRowBands = True
                        .ApplyStyleColumnBands = False
                        .AllowAutoFit = False
                        End With
                    End With
                End If
            End If
    End Sub

    Appreciate any help!

    Friday, October 3, 2014 10:58 AM

Answers

  • You need to set a range where you want the table e.g. as follows. I have no idea what nItems is supposed to represent. You mention a 'marker' but don't explain that. The example puts the table at the start of the message.

    Sub insertTable()
    Dim oRng As Object
    Dim wdDoc As Object
    Const nItems As Long = 2
        If TypeName(ActiveWindow) = "Inspector" Then
            If ActiveInspector.IsWordMail And ActiveInspector.EditorType = olEditorWord Then
                Set wdDoc = ActiveInspector.WordEditor
                With wdDoc
                    Set oRng = wdDoc.Range
                    oRng.collapse 1
                    .Tables.Add Range:=oRng, NumRows:=nItems + 2, NumColumns:=5, _
                                DefaultTableBehavior:=1, AutoFitBehavior:=0
                    With .Tables(1)
                        If .Style <> "Table Grid" Then
                            .Style = "Table Grid"
                        End If
                        .ApplyStyleHeadingRows = True
                        .ApplyStyleLastRow = False
                        .ApplyStyleFirstColumn = True
                        .ApplyStyleLastColumn = False
                        .ApplyStyleRowBands = True
                        .ApplyStyleColumnBands = False
                        .AllowAutoFit = False
                    End With
                End With
            End If
        End If
    End Sub


    Graham Mayor - Word MVP
    www.gmayor.com

    Friday, October 3, 2014 2:41 PM
  • Ok, I figured it out. I added the following lines:

    Dim objSel As Word.Selection
    Set objSel = wdDoc.Windows(1).Selection
    Set oRng = objSel.Range

    I skipped the collapse method. I also had to add the Microsoft Word 14.0 Object Library under tools - > references to get it to work.

    • Marked as answer by lars.lind83 Thursday, November 20, 2014 12:23 PM
    Thursday, November 20, 2014 12:23 PM

All replies

  • You need to set a range where you want the table e.g. as follows. I have no idea what nItems is supposed to represent. You mention a 'marker' but don't explain that. The example puts the table at the start of the message.

    Sub insertTable()
    Dim oRng As Object
    Dim wdDoc As Object
    Const nItems As Long = 2
        If TypeName(ActiveWindow) = "Inspector" Then
            If ActiveInspector.IsWordMail And ActiveInspector.EditorType = olEditorWord Then
                Set wdDoc = ActiveInspector.WordEditor
                With wdDoc
                    Set oRng = wdDoc.Range
                    oRng.collapse 1
                    .Tables.Add Range:=oRng, NumRows:=nItems + 2, NumColumns:=5, _
                                DefaultTableBehavior:=1, AutoFitBehavior:=0
                    With .Tables(1)
                        If .Style <> "Table Grid" Then
                            .Style = "Table Grid"
                        End If
                        .ApplyStyleHeadingRows = True
                        .ApplyStyleLastRow = False
                        .ApplyStyleFirstColumn = True
                        .ApplyStyleLastColumn = False
                        .ApplyStyleRowBands = True
                        .ApplyStyleColumnBands = False
                        .AllowAutoFit = False
                    End With
                End With
            End If
        End If
    End Sub


    Graham Mayor - Word MVP
    www.gmayor.com

    Friday, October 3, 2014 2:41 PM
  • Hi Grayam

    Thanks for your help! Sorry for late reply, I set up an alert for this thread but didn't get the alert for some reason.

    Anyway, nitems comes from the user input in the form. (nItems = Me.listItem.ListCount)

    This code works great and it no longer replaces the text already written in the message, but - as you said - puts it at the beginning.

    What I mean by marker is that I would ideally have this table appear where the user is currently typing. Any suggestions?

    //Lars

    • Marked as answer by lars.lind83 Tuesday, October 21, 2014 1:40 PM
    • Unmarked as answer by lars.lind83 Tuesday, October 21, 2014 1:40 PM
    Tuesday, October 21, 2014 1:39 PM
  • Ok, I figured it out. I added the following lines:

    Dim objSel As Word.Selection
    Set objSel = wdDoc.Windows(1).Selection
    Set oRng = objSel.Range

    I skipped the collapse method. I also had to add the Microsoft Word 14.0 Object Library under tools - > references to get it to work.

    • Marked as answer by lars.lind83 Thursday, November 20, 2014 12:23 PM
    Thursday, November 20, 2014 12:23 PM
  • Sorry I missed your question of the 21st.

    Change

    Dim objSel As Word.Selection

    to

    Dim objSel As Object

    if you want to avoid the reference to the Word object library.


    Graham Mayor - Word MVP
    www.gmayor.com

    Thursday, November 20, 2014 1:56 PM