Local copy of word document loaded from shared drive not working with VBA saveas PDF method. Doesn't save the document - vba

I have a macro that works fine in the shared drive when it is only accessed by one/first person. When it is accessed by the second person while the first person still has it open, it says it's already open. When I click "create a local copy and merge changes later" the macro will work up until it saves the file.
The macro basically invokes a userform to collect information, fill in the document, and then it should save the document as a PDF to the desktop. The PDF isn't saving in the local copy version for some reason. When the "Follow hyperlink" comes up, it says "Error 4198, command failed". I check my desktop and the file isn't there, leading me to believe that this error is in relation to the file not being created....
I just need the macro to allow the document to be saved to their desktop as a pdf, whether they're in the normal version, or the local copy created as a byproduct of the shared drive rules.
Main_Form.hide
enviro = CStr(Environ("USERPROFILE"))
sName = Format(Date, "mm-dd-yyyy") & " Denial Letter - Invoice " & Invoice_Text.Value & ".pdf"
sPath = enviro & "\Desktop\"
ThisDocument.SaveAs2 FileName:=sPath & sName, fileformat:=wdFormatPDF
fullName = sPath & sName
ThisDocument.FollowHyperlink fullName
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

Use a template (dotm), not a document. Create new documents from the template, that way there will be no document "lock".
Also, don't use ThisDocumentas that means specifically the document in which the VBA code is located, use ActiveDocument, instead.

Related

Saving a file to a restricted drive

I've been able to successfully save a document to a shared drive/folder, using ActiveDocument.SaveAs, giving the path and file name. I've been tasked to save a document to a folder in a drive that I do not have access to... nor will the users sending and saving this document. My question is.. is this possible?
This works...
' Gather information for file name
Dim fname As String
fname = "CMA Payment & Refund Investigation Form"
Dim fname1 As String
fname1 = ActiveDocument.FormFields("PCID").Result
Dim fname2 As String
fname2 = Date$
' Save document
ActiveDocument.SaveAs "C:\Test Forms\CMA Payment and Refund\" & fname & " - " & fname1 & "-" & fname2 & ".docm"
However, when I switch the drive and folder names it doesn't.
I receive Run-time error '5152'
This is not a valid file name. ( It is)
*Check the path to make sure it was typed correctly. (I did)
*Select a file from a list of files and folders. (I don't have access to the files, but was given a screenshot of the path.)

Change Web Image via Excel Macro

I am using Excel macros to drive image changes for a webpage. If I want an image to change on the page, I execute the macro, which grabs the desired image and replaces the file the webpage is using, and the webpage refreshes automatically.
It works perfectly in a local machine, but the intent is for remote users to pull up the webpage and see the new image as I control the macro.
Here is the excel macro:
Sub update_image()
newName = "C:\Cases\Nominal\new_image_to_show.png"
oldName = "C:\Cases\Images\updated_image.png"
FileCopy newName, oldName
End Sub
When I replace the file locations with the web locations, the macro stops working. The web space is an internal location within my organization, and two options I tried were:
newName = "https:://location_of_content\Cases\Nominal\new_image_to_show.png"
oldName = "https:://location_of_content\Cases\Images\updated_image.png"
and
newName = "file://///location_of_content\Cases\Nominal\new_image_to_show.png"
oldName = "file://///location_of_content\Cases\Images\updated_image.png"
I also tried relative path nomenclature,
newName = ThisWorkbook.Path & "\Cases\Nominal\new_image_to_show.png"
but in both cases I get
Run Time Error '52'
Any ideas on how I can rename the paths? Or alternatives to Excel Macros?

Use a generic pathway as default

I have a small program that creates a load of files and saves to a folder specified by the user. Currently the top of the userform looks like the following when initialised:
I'd prefer if when the userform opens and also when the dialog for choosing a appears (via the Destination button) that a general default is already shown:
Can the program find the pathway to a user's desktop?
To get the path to the users desktop, use:
PathToDesktop = Environ("USERPROFILE") & "\Desktop"
If you are doing this in excel-vba, the following code would save the current workbook as a copy to the user's desktop... you can probably figure out what you're doing from there.
Dim DesktopPath As String
DesktopPath = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator
ActiveWorkbook.SaveAs DesktopPath & "filename.xls"

Visio 300+ pages, split into 300+ separate docs, or print to pdf, export to jpg/gif

I created 300+ page Visio drawing by importing data to create 300+ "org charts".
I say "org charts' bc they are actually drawings of a Crystal Report as 'CEO' and its datasource tables and fields as "employees".
I now have one huge Visio file with 300+ pages, each page is one report and its datasources. But my goal is to get 300+ separate documents each named appropriately. Using vba i can rename each page as desired or obtain desired name from page.
Ideally i want to have pdf files, not visio files but in pinch would do, and last desirable would be as jpg/gif.
I have tried:
exporting/saving each page as jpg/gif/html but get 920 error.. i am totally stumped after exhaustive search on how to modify resolution for each page to avoid 920 error, i mean what is the 'perfect' resolution required? very vague documentation available.
Sub ExportPagesAsFiles()
Dim PagsObj As Visio.Pages
Dim PagObj As Visio.Page
Dim ExportName As String
Dim ExportPath As String
Set PagsObj = ActiveDocument.Pages
'Open "C:\temp\exportLog.txt" For Output Shared As #1
For Each PagObj In PagsObj
ExportPath = "c:\Report_Visio\"
ExportName = ExportPath & PagObj.Name & ".jpg"
' ".gif"
' ".wmf"
' ".html"
'Print #1, ExportName
PagObj.Export ExportName
Next PagObj
'Close #1
End Sub
printing to pdf printer but hit pdf software dialogue (CutePDF). I have no admin rights to modify system registry and I am on Windows 7 machine and sendkeys is outlawed.
vba to create new doc, copy/paste page drawing into it, name new doc, save it with name. But some Visio quirk only copy paste page objects but not the captions and data behind them so they are blank shapes. Copying entire page contents is not clear to me after exhaustive search.
vba to save copy of 300+ doc with name of first page, then delete rest of pages. Open original 2nd time, save as second page, then delete rest of pages, and repeat 300+ times. I quit this after 10 or so pages and 3 hours.
Seems every possible solution path hits pitfalls, swamps, cliffs ..
As a general note, there is either very spartan, or obtusely technical, Visio information available on internet. With any other Office product, there are loads of info, examples, etc in forums etc. But Visio its crickets.
So wonder if any Visio developers can help me choose and navigate these solution paths!
Thanks.
Edit to add: I am using Visio Standard 2003 version
Edit to add Visio export to file code used
You can try to use PDFCreator (its open source).
It's a virtual printer, that prints to PDF, JPG, BMP, PNG (very useful for charts) and many other formats.
It has auto save option, configurable filenames and it can save printed document pages as separate files.
Some screenshots from application that configures virtual printer (this is PDFCreator 0.9.6).
Printing each page to separate file
Auto-save options:
I've decided to take a different approach.
I used VBA and Access to create each Visio page separately, instead of creating a 300+ page Visio doc. Visio's Org Chart import data wizard is powerful but leaves one stuck for options to get individual pages out, as I outlined above.
So, in Access, I looped through 300+ report records, selecting each report's data, one at a time, creating Visio drawing for that report, naming Visio file as report name and then saving and closing it.
Then I ended up with 300+ Visio files, each one showing a report's datasource tables and fields. Good enough.
Using VBA to create chart from data 'orgwiz' http://office.microsoft.com/en-ca/visio-help/make-visio-organization-charts-from-personnel-files-HA001077464.aspx
The code is
Set objVisio = CreateObject("Visio.Application")
Set objAddOn = objVisio.Addons.ItemU("OrgCWiz")
strCommand = "/DATASOURCE=c:\temp\MyDatabase.mdb, " _
& " TABLE=MyVisioDataSource, " _
& " DBQUALIFIER=Microsoft.Jet.OLEDB.4.0 " _
& " /NAME-FIELD=Data_Object_Name " _
& " /UNIQUEID-FIELD=Data_Object_ID " _
& " /MANAGER-FIELD=Data_Object_Parent_ID " _
& " /DISPLAY-FIELDS=" & strDisplayFields _
& " /CUSTOM-PROPERTY-FIELDS=" & strPropertyFields _
& " /SYNC-ACROSS-PAGES " _
& " /HYPERLINK-ACROSS-PAGES " _
& " /SHAPE-FIELD=MASTER_SHAPE " _
& " /PAGES=" & strReportName
objAddOn.Run ("/S-INIT")
Dim cmdArray, i
cmdArray = Split(strCommand, "/")
For i = LBound(cmdArray) To UBound(cmdArray)
objAddOn.Run ("/S-ARGSTR /" + cmdArray(i))
Next
objAddOn.Run ("/S-RUN ")

Can't KILL file using Word 2007 VBA if it's DOC format

The following code works for DOCX files, but gives "Access Denied" on DOC files:
Public Sub SaveGraded()
oldnamepath = ActiveDocument.FullName
oldname = GetFileName(ActiveDocument.Name)
oldExtension = getextension(ActiveDocument.FullName)
newname = oldname & "_GRADED" & "." & oldExtension
ActiveDocument.SaveAs filename:=newname
Kill oldnamepath
End Sub
Indeed, it's one of those weird things with Word, it'll hold a .doc file open exclusively much longer than a .docx.
You can use a loop to continually test delete until it works - don't use Kill for this since you can't trap it, use VBScript FileSystemObject.DeleteFile (str Name, bool Force), which is more reliable anyway - or you can use ActiveDocument.Close and reopen it. However I've noticed that even this doesn't always work and you sometimes have to follow that by Application.Quit; goodbye script execution unless you're automating it from an external application, which is what I do.