From: ade670 on

Simon Lloyd;726861 Wrote:
>
Sure
>
> Attachments.
>
> To upload a document, click reply then add your few words, scroll down
past the submit button and you will see the Manage Attachments button,
this is where you get to add files for upload, if you have any trouble
please use this link or the one at the bottom of the
any page.
>
>
>
>
> Attachments.
>
> To upload a workbook, click reply then add your few words, scroll down
past the submit button and you will see the Manage Attachments button,
this is where you get to add files for upload, if you have any trouble
please use this link or the one at the bottom of the
any page.
>



Guys,

I have attached my document with the macro enabled and a few words (and
the source code) in the main body - any help with this would be
amazing

Ade


+-------------------------------------------------------------------+
|Filename: Exampapercreator-withmacro.doc |
|Download: http://www.thecodecage.com/forumz/attachment.php?attachmentid=590|
+-------------------------------------------------------------------+

--
ade670
------------------------------------------------------------------------
ade670's Profile: http://www.thecodecage.com/forumz/member.php?u=1881
View this thread: http://www.thecodecage.com/forumz/showthread.php?t=203613

http://www.thecodecage.com/forumz

From: Simon Lloyd on

In your example the code below worked fine!



VBA Code:
--------------------



Private Sub CommandButton1_Click()
Dim strFileName As String
Dim strPath As String
Dim oDoc As Document
Dim iCount, jCount As Long
Dim fDialog As FileDialog
Dim oBM As Bookmarks
Dim vBM As Variant
Dim rImage As Range
Dim bExists As Boolean
Set oBM = ActiveDocument.Bookmarks
bExists = False
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
.Title = "Select folder and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User", , _
"List Folder Contents"
Exit Sub
End If
strPath = fDialog.SelectedItems.Item(1)
If Right(strPath, 1) <> "\" _
Then strPath = strPath + "\"
End With
strFileName = Dir$(strPath & "*.gif")
iCount = 0
While Len(strFileName) <> 0
iCount = iCount + 1
strFileName = Dir$()
Wend
iItem = Int((iCount * Rnd) + 1)
strFileName = Dir$(strPath & "*.gif")
jCount = 0
While Len(strFileName) <> 0
jCount = jCount + 1
If jCount = iItem Then
For Each vBM In oBM
If vBM.Name = "Dilbert" & jCount Then
bExists = True
Exit For
End If
Next vBM
If bExists = False Then
Selection.Bookmarks.Add "Dilbert" & jCount
End If
Set rImage = ActiveDocument.Bookmarks("Dilbert" & jCount).Range
rImage.Text = ""
rImage.InlineShapes.AddPicture (strPath & strFileName)
rImage.End = rImage.End + 1
ActiveDocument.Bookmarks.Add "Dilbert" & jCount, rImage
End If
strFileName = Dir$()
Wend
ActiveDocument.PrintOut
End Sub

--------------------





a
d
e
6
7
0
;
7
2
7
4
1
2

W
r
o
t
e
:


>
Guys,

I have attached my document with the macro enabled and a few words (and
the source code) in the main body - any help with this would be amazing

Ade


--
Simon Lloyd

Regards,
Simon Lloyd
'Microsoft Office Help' (http://www.thecodecage.com)
------------------------------------------------------------------------
Simon Lloyd's Profile: http://www.thecodecage.com/forumz/member.php?u=1
View this thread: http://www.thecodecage.com/forumz/showthread.php?t=203613

http://www.thecodecage.com/forumz

From: ade670 on

Simon Lloyd;727472 Wrote:
>
In your example the code below worked fine!
>
>
>



VBA Code:
--------------------
>


>

Private Sub CommandButton1_Click()
> Dim strFileName As String
> Dim strPath As String
> Dim oDoc As Document
> Dim iCount, jCount As Long
> Dim fDialog As FileDialog
> Dim oBM As Bookmarks
> Dim vBM As Variant
> Dim rImage As Range
> Dim bExists As Boolean
> Set oBM = ActiveDocument.Bookmarks
> bExists = False
> Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
> With fDialog
> .Title = "Select folder and click OK"
> .AllowMultiSelect = False
> .InitialView = msoFileDialogViewList
> If .Show <> -1 Then
> MsgBox "Cancelled By User", , _
> "List Folder Contents"
> Exit Sub
> End If
> strPath = fDialog.SelectedItems.Item(1)
> If Right(strPath, 1) <> "\" _
> Then strPath = strPath + "\"
> End With
> strFileName = Dir$(strPath & "*.gif")
> iCount = 0
> While Len(strFileName) <> 0
> iCount = iCount + 1
> strFileName = Dir$()
> Wend
> iItem = Int((iCount * Rnd) + 1)
> strFileName = Dir$(strPath & "*.gif")
> jCount = 0
> While Len(strFileName) <> 0
> jCount = jCount + 1
> If jCount = iItem Then
> For Each vBM In oBM
> If vBM.Name = "Dilbert" & jCount Then
> bExists = True
> Exit For
> End If
> Next vBM
> If bExists = False Then
> Selection.Bookmarks.Add "Dilbert" & jCount
> End If
> Set rImage = ActiveDocument.Bookmarks("Dilbert" & jCount).Range
> rImage.Text = ""
> rImage.InlineShapes.AddPicture (strPath & strFileName)
> rImage.End = rImage.End + 1
> ActiveDocument.Bookmarks.Add "Dilbert" & jCount, rImage
> End If
> strFileName = Dir$()
> Wend
> ActiveDocument.PrintOut
> End Sub

--------------------
>
>





Found out what it was - wait for it---- Inadvertently, I was
deleting the booking "dilbert" from the word document - I have now
worked an example, wherby it adds at the given book marked, anchored to
the document - jeez,

Thanks for your help mate,

Ade


--
ade670
------------------------------------------------------------------------
ade670's Profile: http://www.thecodecage.com/forumz/member.php?u=1881
View this thread: http://www.thecodecage.com/forumz/showthread.php?t=203613

http://www.thecodecage.com/forumz

From: Simon Lloyd on

You would be better off having all the text on an xlVeryHidden
worksheet and pick the text randomly from there. I'm not sure whether
you can just read random words from a text file, i think you have to
GET all the text into excel then perfom a randomisation, so cut out the
middle man and store them in your workbook.

When a worksheet is xlVeryHidden via VBA it can only be made visible
via VBA and not through the menubar.


a
d
e
6
7
0
;
7
2
7
6
4
5

W
r
o
t
e
:


>
Found out what it was - wait for it---- Inadvertently, I was deleting
the booking "dilbert" from the word document - I have now worked an
example, wherby it adds at the given book marked, anchored to the
document - jeez,

Thanks for your help mate,

Ade

*------------------------------- posted before receiving a reply after
1 23 minutes of original post -------------------------------*

Hi Simon,

Thanks for your help on this - with bookmarks in place and using
multiple command button this work.

One final query, is it possible to alter the code so tha it displays a
random word from a .txt document at a bookmark.

- I am a music teacher and being able to generate a simple test which
removes the headache of manually creating a new one each time is a real
treat.

Ade


--
Simon Lloyd

Regards,
Simon Lloyd
'Microsoft Office Help' (http://www.thecodecage.com)
------------------------------------------------------------------------
Simon Lloyd's Profile: http://www.thecodecage.com/forumz/member.php?u=1
View this thread: http://www.thecodecage.com/forumz/showthread.php?t=203613

http://www.thecodecage.com/forumz