PDA

View Full Version : Inserting ini file into Word with formatting



adenjago
09-29-2015, 11:30 PM
Hi, :hi:

Im very new to VBA, and most of my code is from Snippets and recording Marco's, so please be gentle.

I have some VBA code which works and inserts a File into a specific bookmark within word, but I'm struggling with a small bit portion in regards to the format the Text from the file that gets inserted, either to using the format of the bookmark or specifying the format on insert.


working code



File_purchase_inv_def = HarvDir + "\v1live" + "\Purchase_Invoices.def"

Selection.Range.InsertFile FileName:=(File_purchase_inv_def)
oRng = ActiveDocument.Bookmarks("BK_Puchase_Invioces").Range
oRng.Select
Selection.Range.InsertFile FileName:=(File_purchase_inv_def)


The format i'm trying to use is "Verdana" and Font size "7" I have tried formating the area of the bookmark and also the below code.


**Not working Example1**


File_purchase_inv_def = HarvDir + "\v1live" + "\Purchase_Invoices.def"

Selection.Range.InsertFile FileName:=(File_purchase_inv_def)
oRng = ActiveDocument.Bookmarks("BK_Puchase_Invioces").Range
oRng.Select
Selection.Range.InsertFile FileName:=(File_purchase_inv_def)
With .Styles(Normal).Font
.Name = "Verdana"
.Size = 7
End with




**Not working example 2**


File_purchase_inv_def = HarvDir + "\v1live" + "\Purchase_Invoices.def"

Selection.Range.InsertFile FileName:=(File_purchase_inv_def)
oRng = ActiveDocument.Bookmarks("BK_Puchase_Invioces").Range
oRng.Select
Selection.Range.InsertFile FileName:=(File_purchase_inv_def)
Selection.Font.Name = "Verdana"
Selection.Font.Size = 7

gmayor
09-30-2015, 04:36 AM
If you insert the text file at the bookmark, it is inserted AFTER the bookmark and not IN it. You need to have the text IN the bookmark in order for formatting of the bookmark range to be effective. The variable HarvDir is not defined?

As the ini file is a text file, it is a simple matter to read that text file into a string variable then insert that string into the bookmark. You can then format the bookmark range e.g. as follows. I assume your incorrect spellings of "Purchase" & 'Invoices' are how you have named the bookmark? This method preserves the bookmark and can be run again to replace the text with another text file.


Option Explicit

Sub InsertAndFormat()
Dim File_purchase_inv_def As String
File_purchase_inv_def = HarvDir + "\v1live" + "\Purchase_Invoices.def"
FillBM "BK_Puchase_Invioces", GetIniFile(File_purchase_inv_def)
With ActiveDocument.Bookmarks("BK_Puchase_Invioces").Range
.Font.name = "Verdana"
.Font.Size = 7
End With
lbl_Exit:
Exit Sub
End Sub

Private Sub FillBM(strBMName As String, strValue As String)
'Graham Mayor
Dim oRng As Range
With ActiveDocument
On Error GoTo lbl_Exit
Set oRng = .Bookmarks(strBMName).Range
oRng.Text = strValue
oRng.Bookmarks.Add strBMName
End With
lbl_Exit:
Set oRng = Nothing
Exit Sub
End Sub


Private Function GetIniFile(strFilename As String) As String
'Graham Mayor
Dim iFile As Integer
iFile = FreeFile
Open strFilename For Input As #iFile
GetIniFile = Input(LOF(iFile), iFile)
Close #iFile
lbl_Exit:
Exit Function
End Function

adenjago
10-03-2015, 04:46 AM
Hi Graham

Thanks for the reply, sorry for not getting back to you earlier, I have got it working following messing about and think I just need to put .clearformatting and just insert the that I just need to insert it into the bookmark, an will it take on the Font\size of the bookmark.

However I have got multiple Files to Insert and Multiple file replacements, is there any way of performing this inserts\replaces more efficiently ? (still very new to VB\VBA)

As you can see below there is a lot of duplicated code but it works and understand what it is doing,



' DBServerName - Search and Replace With Selection.Find
.ClearFormatting
.Text = "<DBServerName>"
.Replacement.ClearFormatting
.Replacement.Text = DBServerName
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With

' DBServerIP - Search and Replace
With Selection.Find
.ClearFormatting
.Text = "<DBServerIP>"
.Replacement.ClearFormatting
.Replacement.Text = DBServerIP
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With

' APPServerName - Search and Replace
With Selection.Find
.ClearFormatting
.Text = "<AppServerName>"
.Replacement.ClearFormatting
.Replacement.Text = APPServerIP
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With

' APPServerIP - Search and Replace
With Selection.Find
.ClearFormatting
.Text = "<APPServerIP>"
.Replacement.ClearFormatting
.Replacement.Text = APPServerName
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With


And the File inserts...


ActiveDocument.Bookmarks("BK_Generic_DLC_Version").Range.Text = _ CreateObject("scripting.filesystemobject").opentextfile(DLC_Path + "\version").ReadAll()


ActiveDocument.Bookmarks("BK_Live_OA_ver").Range.Text = _
CreateObject("scripting.filesystemobject").opentextfile(OA_Live_Path + "\openacc.ver").ReadAll()


ActiveDocument.Bookmarks("BK_Live_oaservices_ini").Range.Text = _
CreateObject("scripting.filesystemobject").opentextfile(OA_Live_Path + "\oaservices.ini").ReadAll()


ActiveDocument.Bookmarks("BK_Generic_start_oa_bat").Range.Text = _
CreateObject("scripting.filesystemobject").opentextfile(Script_Path + "\start_oa.bat").ReadAll()


ActiveDocument.Bookmarks("BK_Generic_stop_oa_bat").Range.Text = _
CreateObject("scripting.filesystemobject").opentextfile(Script_Path + "\stop_oa.bat").ReadAll()


ActiveDocument.Bookmarks("BK_Generic_Backupcontrol_bat").Range.Text = _
CreateObject("scripting.filesystemobject").opentextfile(Script_Path + "\BackupScripts\BackupControl.txt").ReadAll()


ActiveDocument.Bookmarks("BK_Generic_conmgr_properties").Range.Text = _
CreateObject("scripting.filesystemobject").opentextfile(DLC_Path + "\Properties\conmgr.properties").ReadAll()


ActiveDocument.Bookmarks("BK_Generic_Ubroker_properties").Range.Text = _
CreateObject("scripting.filesystemobject").opentextfile(DLC_Path + "\Properties\Ubroker.properties").ReadAll()


ActiveDocument.Bookmarks("BK_Live_openacc_ini").Range.Text = _
CreateObject("scripting.filesystemobject").opentextfile(OA_Live_Path + "\openacc.ini").ReadAll()


ActiveDocument.Bookmarks("BK_Live_openrun_ini").Range.Text = _
CreateObject("scripting.filesystemobject").opentextfile(OA_Live_Path + "\openrun.ini").ReadAll()


thanks Ade

gmayor
10-03-2015, 05:31 AM
Obviously I can't test it without your data files, but the following should be close:
Option Explicit
'The Names of the servers etc
Private Const DBServerIP As String = "DBServerIP_Name"
Private Const AppServerName As String = "AppServer_Name"
Private Const APPServerIP As String = "APPServerIP_Name"
'The actual paths used
Private Const DLC_Path As String = "C:\DLCPath\"
Private Const OA_Live_Path As String = "C:\OA_Live_Path\"
Private Const Script_Path As String = "C:\Script_Path\"

Sub ProcessTextFiles()
InsertAndFormat "BK_Generic_DLC_Version", DLC_Path + "version"
InsertAndFormat "BK_Live_OA_ver", OA_Live_Path + "openacc.ver"
InsertAndFormat "BK_Live_oaservices_ini", OA_Live_Path + "oaservices.ini"
InsertAndFormat "BK_Generic_start_oa_bat", Script_Path + "start_oa.bat"
InsertAndFormat "BK_Generic_stop_oa_bat", Script_Path + "stop_oa.bat"
InsertAndFormat "BK_Generic_Backupcontrol_bat", Script_Path + "BackupScripts\BackupControl.txt"
InsertAndFormat "BK_Generic_conmgr_properties", DLC_Path + "Properties\conmgr.properties"
InsertAndFormat "BK_Generic_Ubroker_properties", DLC_Path + "Properties\Ubroker.properties"
InsertAndFormat "BK_Live_openacc_ini", OA_Live_Path + "\openacc.ini"
InsertAndFormat "BK_Live_openrun_ini", OA_Live_Path + "\openrun.ini"
lbl_Exit:
Exit Sub
End Sub

Sub InsertAndFormat(strBookmark As String, strTextFile As String)
FillBM strBookmark, GetIniFile(strTextFile)
With ActiveDocument.Bookmarks(strBookmark).Range
.Font.name = "Verdana"
.Font.Size = 7
'The replacements
.Text = Replace(.Text, "<DBServerIP>", DBServerIP)
.Text = Replace(.Text, "<AppServerName>", AppServerName)
.Text = Replace(.Text, "<APPServerIP>", APPServerIP)
End With
lbl_Exit:
Exit Sub
End Sub

Private Sub FillBM(strBMName As String, strValue As String)
'Graham Mayor
Dim oRng As Range
With ActiveDocument
On Error GoTo lbl_Exit
Set oRng = .Bookmarks(strBMName).Range
oRng.Text = strValue
oRng.Bookmarks.Add strBMName
End With
lbl_Exit:
Set oRng = Nothing
Exit Sub
End Sub

Private Function GetIniFile(strFilename As String) As String
'Graham Mayor
Dim iFile As Integer
iFile = FreeFile
Open strFilename For Input As #iFile
GetIniFile = Input(LOF(iFile), iFile)
Close #iFile
lbl_Exit:
Exit Function
End Function