PDA

View Full Version : [SOLVED:] Server not avaiable ... sometimes ???



ksor
02-04-2018, 04:45 AM
From Access I want to create a huge Word document with infos from the database.

At a point I want to get some pictures with a short text and put them into the document too - it works ... sometimes !

Often I get an error that the server is not avaiable - I can't see the pattern in this failing !

Here is my code for inserting the pictures - and when "the server is there" it works nicely:



Public Sub imageInsert(imgPath As String, txt As String, bD As Word.Document)
With Word.Application.Selection
.EndKey Unit:=wdStory
.InlineShapes.AddPicture imgPath, False, True
.MoveRight wdCharacter, 1
.TypeParagraph
.TypeText txt
.TypeParagraph
.TypeParagraph
.MoveUp wdLine, 3, wdExtend
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.EndKey wdStory
End With
End Sub


I must admit that i DON'T use my bD parameter which should be a reference to my huge document ..... I couldn't get it to work :banghead::banghead:

I used the "Word.Application,Selection" instead - but exactly THAT sometimes is failing !

I've tried to find some ".Selection" somewhere in my bD parameter but CAN'T :crying::crying:

How can I get the code to work with my bD parameter ( = big document) ?

gmayor
02-04-2018, 10:13 PM
If you include a non-optional reference to bD, you need to use it. I assume that you are running this code from Access? That being the case you need to tell Access to use Word and which document to process (hence bD). If you open a document in Word that is already open the open version will take focus e.g.


Sub InsertImageExample()
Dim wdapp As Object
Dim wdDoc As Object
Set wdapp = GetObject(, "Word.Application")
If Err Then
Set wdapp = CreateObject("Word.Application")
End If
wdapp.Visible = True
Set wdDoc = wdapp.documents.Open("C:\Path\Big Document.docx")
imageInsert imgPath:="C:\image path\image name.jpg", _
txt:="This is the text to insert", _
bD:=wdDoc
Set wdDoc = Nothing
Set wdapp = Nothing
End Sub

Public Sub imageInsert(imgPath As String, txt As String, bD As Object)
Dim oRng As Object
Const wdAlignParagraphCenter As Long = 1
Const wdAlignParagraphLeft As Long = 0
bD.Range.insertparagraphafter 'ensure that the image will go on a new line
Set oRng = bD.Range 'set a range to the document
With oRng
.collapse 0 'collapse the range to its end
.InlineShapes.AddPicture imgPath, False, True, oRng 'insert the image
.Paragraphs(1).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter 'align the image
.End = bD.Range.End ' move the end of the range after the image
.collapse 0 'collapse the range
.Text = vbCr & txt & vbCr & vbCr 'type the text after the image (paragraph spacing would be better than empty paragraphs)
.collapse 0 ' collapse the range
.Paragraphs(1).Range.ParagraphFormat.Alignment = wdAlignParagraphLeft 'format the last paragraph
.Select 'select the end of the document
End With
Set oRng = Nothing
End Sub

ksor
02-04-2018, 10:14 PM
Thx gmayor - I know I didn't do it right when I didn't use the reference to bD - but I couldn't fiure it out !

Now I'll try to analyze your code and see if I can understand what goes on - thx