Stuggling to get my docs to open after a loop

Desmondo

Board Regular
Joined
Feb 27, 2013
Messages
70
I have a problem that i am trying to resolve. I have scenario where i have a directory with 16 sub directories which contain various word templates. 1 of the directories is generic letters which apply to all cases the rest are unique to the type of case. 95% of the fields will be exactly the same from case to case. I have created a menu userform which has all 16 categories as buttons which launch the sub forms for each case type. My plan was to create bookmarks in each template and hopefully populate & then save each template in their own unique folder in a directory with some sort of reference. I have only recently started VBA and from what i have read in books & web i cannot find anything that detailed for my needs but i cannot get the docs to open nor populate. Ideally i would like to share the code between all the userforms. Any help or pointers would be appreciated before i lose all my hair.

My code thus far

Private Sub CommandButton2_Click()
Dim WdApp As Object
Dim i As Long
Dim Pth As String, fType As String
Dim Arr
fType = """C:\Users\desmo\Desktop\Work docs\Templates\Dup clmt\*.doc*"""
Pth = "C:\Users\desmo\Desktop\Work docs\Templates\Dup clmt"


Arr = Filter(Split(CreateObject("wscript.shell").exec("cmd /c Dir " & fType & " /b /a-d").stdout.readall, vbCrLf), ".")
On Error Resume Next
Set WdApp = GetObject(, "Word.Application")
If Err.Number = 429 Then
Err.Clear
'Create a Word application if Word is not already open.
Set WdApp = CreateObject("Word.Application")
End If
On Error GoTo 0
WdApp.Visible = True


For i = LBound(Arr) To UBound(Arr)
Call DoStuff(Pth & Arr(i))
Next i

WdApp.Quit
Set WdApp = Nothing


End Sub






Private Sub DoStuff(strDocName As Variant)
'Declare obj variables for the word application and document.
Dim wddoc As Object
'On Error statement if Word is not already open.
On Error Resume Next
'Activate Word if it is already open.
'Define the strDocName String variable.
'Activate the Word application.
WdApp.Activate
'Set the Object variable for the Word document's full name and folder path.
Set wddoc = WdApp.Documents(strDocName)
'If the Word document is not already open, then open it.
If wddoc Is Nothing Then Set wddoc = WdApp.Documents.Open(strDocName)
'The document is open, so activate it.
wddoc.Activate
'Do things to Word Document here
ActiveDocument.Bookmarks("Title").Range.Text = txtTitle.Value
ActiveDocument.Bookmarks("Name").Range.Text = txtName.Value
ActiveDocument.Bookmarks("Ref").Range.Text = txtRef.Value
ActiveDocument.Bookmarks("Address").Range.Text = txtAddress.Value
ActiveDocument.Bookmarks("From").Range.Text = txtFrom.Value
ActiveDocument.Bookmarks("To").Range.Text = txtTo.Value
ActiveDocument.Bookmarks("Amount").Range.Text = txtAmount.Value


'Close and Save changes
wddoc.Close True
'Release system memory that was reserved for the two Object variables.
Set wddoc = Nothing
End Sub


Private Sub cmdClear_Click()
optGreeting1.Value = True
txtRecipientName.Value = Null
txtRecipientAddress.Value = Null
txtSalutation.Value = Null
txtPosition.Value = Null
cboInterviewLocation.Value = Null
cboInterviewDay.Value = Null
txtInterviewDate.Value = Null
txtInterviewTime.Value = Null
cboInterviewDuration.Value = Null
txtSenderName.Value = Null
txtSenderPosition.Value = Null
cboSenderAddress.Value = Null
End Sub


Private Sub CommandButton3_Click()




End Sub












'========================================================================================
'- COMMAND BUTTON : EXCEL USERFORM TEXT BOXES TO NEW WORD DOCUMENT
'- In Excel VBA Editor Tools\References... 'Microsoft Word xx Object Library'
'========================================================================================
Private Sub CommandButton6_Click()
Dim WordApp As Object
'------------------------------------------------------------------------------------
'- Try to activate Word application already open
On Error Resume Next
Set WordApp = GetObject(, "Word.Application")
'------------------------------------------------------------------------------------
'- if error then Word is not open. open a new instance
If Err.Number <> 0 Then
Err.Clear
Set WordApp = CreateObject("Word.Application")
End If
On Error GoTo 0
'====================================================================================
'- W O R D C O D E
'====================================================================================
With WordApp
'- DATA TO WORD APPLICATION
.Visible = True
.Documents.Add
.Selection.TypeText Text:=TextBox1.Value
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.TypeText Text:=TextBox2.Value
.Selection.TypeParagraph
'--------------------------------------------------------------------------------
'- PRINT
.ActiveDocument.PrintPreview ' preview only to test
'.ActiveDocument.PrintOut ' document to printer
End With
'====================================================================================
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Forum statistics

Threads
1,215,051
Messages
6,122,871
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top