Quantcast
Channel: Word for Developers forum
Viewing all articles
Browse latest Browse all 4350

Best Resources For Advanced Word VBA

$
0
0

Hello:

Can anyone recommend an ADVANCED VBA book for Microsoft Word 2010?

I am primarily an Excel developer, but recently have a client that wants the results of Excel placed in Word without doing copy and paste.  I have been able to insert tables from Excel into Word and populate the table from Excel's cell contents, use the GoTo method to locate bookmarks, and some other features.

Note:  I will be doing all the Word manipulation through Excel using Word's Object Library from within Excel.

Word's macro recorder prevents the developer from using mouse actions, so there are many actions that Word prevents me from recording.  I am hoping there is an advanced book that explains such VBA objectives as:

(1) Format a table
(2) Change the tab positions, font attributes and other formatting commands
(3) Insert text into a textbox with a particular font and size
(4) Explain the GoToObject Method
(5) Delete Text
(6) And much, much more

Here's a tiny sample of some of the testing I have done.  This sample should show you where I am on the Word VBA learning curve... I've only been at it for about 2 days.

Option Explicit
Dim strFilePathWord As String
Dim dlgOpenFile As FileDialog

Public Sub WriteToExistingWordDoc()
Dim intNoOfRows
Dim intNoOfColumns
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objRange As Word.Range
Dim objTable As Word.Table
Dim shp As Word.Shape
Dim i As Integer
Dim j As Integer
Dim strMyData As String

' *******************************************************
' Get An Existing Word Doc File Name and Path
' *******************************************************
Call SelectWordDocumentToOpen

If strFilePathWord = "" Then
    MsgBox ("No File Selected")
    Exit Sub
End If

' *******************************************************
' Set Up A String To Insert Into A Word Textbox
' *******************************************************
strMyData = "Hello Rich" & vbTab & "Next Column" & Chr(11) & _"Hello Rich" & vbTab & "Next Column" & Chr(11) & _"Hello Rich" & vbTab & "Next Column" & Chr(11) & _"Hello Rich" & vbTab & "Next Column" & Chr(11)

' *******************************************************
' Create The Word Objects from Excel And Open An
' Existing Word Document
' *******************************************************
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Open(strFilePathWord)

' *******************************************************
' Insert Text Into A Textbox
' *******************************************************
For Each shp In ActiveDocument.Shapes
    MsgBox ("My name is " & shp.Name)
    shp.TextFrame.TextRange.Text = strMyData
Next

' *******************************************************
' Locate A BookMark And Insert A Table After The Bookmark
' *******************************************************
intNoOfRows = 26
intNoOfColumns = 2
Set objRange = objDoc.GoTo(What:=wdGoToBookmark, Name:="FirstBookmark")
objDoc.Tables.Add objRange, intNoOfRows, intNoOfColumns
Set objTable = objDoc.Tables(1)
objTable.Borders.Enable = False

intNoOfRows = 26
intNoOfColumns = 2

For i = 1 To intNoOfRows
     For j = 1 To intNoOfColumns
        objTable.Cell(i, j).Range.Text = ActiveSheet.Cells(i, j).Value
     Next j
Next i
End Sub

Private Sub SelectWordDocumentToOpen()

strFilePathWord = ""
Set dlgOpenFile = Application.FileDialog(msoFileDialogOpen)
With dlgOpenFile
    .AllowMultiSelect = False
    .Filters.Clear
    .Filters.Add "Word Document", "*.docx"
    .Filters.Add "2003 Word Document", "*.doc"
    .FilterIndex = 1
    .Show
    If .SelectedItems.Count < 1 Then
        Exit Sub
    End If
    strFilePathWord = .SelectedItems(1)
End With

End Sub

Thanks!


Rich Locus, Logicwurks, LLC

http://www.logicwurks.com



Viewing all articles
Browse latest Browse all 4350

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>